SmR*_*Guy 3 c++ winapi hotkeys
我想制作一个程序,即使它在任何时候都不活动,也可以捕获键盘事件。Hooks 太复杂了,我需要做很多事情才能使其正常工作(制作 DLL、读取它等等),所以我决定继续使用热键。
但现在我有一个问题。注册热键会禁用键盘上的按键,因此我只能将按键发送到程序,而无法在任何其他程序(例如记事本)上键入。
这是我的代码:
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char* argv[]) {
RegisterHotKey(NULL, 1, NULL, 0x41); //Register A
MSG msg = {0};
while (GetMessageA(&msg, NULL, 0, 0) != 0) {
if (msg.message == WM_HOTKEY) {
cout << "A"; //Print A if I pressed it
}
}
UnregisterHotKey(NULL, 1);
return 0;
}
// and now I can't type A's
Run Code Online (Sandbox Code Playgroud)
这个问题有什么简单的解决办法吗?谢谢
小智 5
我会让你的程序模拟一个与你实际执行的按键相同的按键。这意味着:
这很简单。唯一的问题是您的程序也会捕获模拟的按键。为了避免这种情况,您可以执行以下操作:
这就是整个循环。
现在,要模拟按键,您需要添加一些额外的代码。看看这个:
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char* argv[]) {
RegisterHotKey(NULL, 1, 0, 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.
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;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了一下,效果很好,我想。希望我能帮忙;)
| 归档时间: |
|
| 查看次数: |
4283 次 |
| 最近记录: |