在不禁用其密钥的情况下注册全局热键 [英] Registering a Global HotKey without disabling its key

查看:80
本文介绍了在不禁用其密钥的情况下注册全局热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个即使在任何时候都不活动的情况下也可以捕获键盘事件的程序.钩子使我无法正常工作(制作DLL,读取它等)太复杂了,所以我决定继续使用热键.

I want to make a program that can catch keyboard events even if it's not active on any moment. Hooks were too complicated with all the things I have to do to make it to work (making a DLL, reading it, etcetera) so I decided to go on using hotkeys.

但是现在我有一个问题.注册热键会禁用键盘上的键,因此我只能将键发送到该程序,而不能在任何其他程序(例如记事本)上键入.

But now I have a problem. Registering the hotkey disables the key on the keyboard, thus I can only send the key to the program, while I can't type on any other program (e.g. Notepad).

这是我的代码:

#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

是否有解决此问题的简单方法? 谢谢

Is there any simple solution to this problem? Thank you

推荐答案

我会让您的程序模拟一个等于您实际执行的按键.这意味着:

I would let your program simulate a keypress which equals the one you actually performed. That means:

  1. 您按'A'.
  2. 程序捕获了'A'.
  3. 程序模拟按键.

这很简单.唯一的问题是您的程序还会捕获模拟的按键.为了避免这种情况,您可以执行以下操作:

It's quite simple. The only problem would be that your program would also catch the simulated keypress. To avoid it, you can do the following:

  1. 您按'A'.
  2. 程序捕获了'A'.
  3. 程序取消注册热键.
  4. 程序模拟按键.
  5. (程序无法(!)捕获'A'.)
  6. 程序再次注册热键.

这就是整个循环.

现在,要模拟按键,您需要添加一些其他代码.看看这个:

Now, to simulate the keypress, you need to add some additional code. Have a look at this:

#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;
}

我尝试过,并且效果很好. 希望我能帮忙;)

I tried it and it works fine, I guess. Hope I could help ;)

这篇关于在不禁用其密钥的情况下注册全局热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆