RegisterHotKey NOREPEAT变通方法c++



我正在尝试捕获输入,并将它们重定向到特定键的另一个窗口,为此,我先RegisterHotKey,然后UnregisterHotkey,而我SendInput,然后再次注册。我的问题是,每次按键我只需要运行一次,而重新注册后,MOD_NOREPEAT并没有帮助,因为它会连续循环。

我该如何防止它重复?

我的代码结构与Fishboy的答案相似,后者有相同的问题:

#include <windows.h>
#include
using namespace std;
int main(int argc, char* argv[]) {
RegisterHotKey(NULL, 1, MOD_NOREPEAT, 0x41); //Register A; Third argument should also be "0" instead of "NULL", so it is not seen as pointer argument
MSG msg = { 0 };
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.wVk = 0x41;                            //The key to be pressed is A.
st
while (GetMessage(&msg, NULL, 0, 0) != 0) {
if (msg.message == WM_HOTKEY) {
UnregisterHotKey(NULL, 1);           //Prevents the loop from caring about the following
ip.ki.dwFlags = 0;                   //Prepares key down
SendInput(1, &ip, sizeof(INPUT));    //Key down
ip.ki.dwFlags = KEYEVENTF_KEYUP;     //Prepares key up
SendInput(1, &ip, sizeof(INPUT));    //Key up
cout << "A";                         //Print A if I pressed it
RegisterHotKey(NULL, 1, 0, 0x41);    //you know...
}
}
UnregisterHotKey(NULL, 1);
return 0;
}

好的,我已经通过检查GetKeyState解决了这个问题

SendInput(1, &ip, sizeof(INPUT));    
cout << "A";                         
RegisterHotKey(NULL, 1, 0, 0x41)
Bool bHeld = true;
while (bHeld == true)
{
PeekMessage(&msg, 0, 0, 0, 0x0001);
if ((GetKeyState(0x41) == 0x0001) || (GetKeyState(0x41) == 0x0000))
{

bHeld = false;
}
else
{
Sleep(20);
}
}

如果没有PeekMessage,它就会陷入一个循环,无法解决问题。

相关内容

最新更新