当前位置: 首页 > 知识库问答 >
问题:

c++ - 我要在哪里调用 UnregisterHotKey 卸载已经注册的全局热键?

尉迟卓
2024-01-25

我的c++控制台程序中使用 RegisterHotKey 注册全局热键,我应该在哪里使用 UnregisterHotKey 解除热键?是在 while 后面 return 前面吗?

// RegisterHotKey.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。// 2024年01月25日 10:35:21 调节系统音量的工具,ctrl+小键盘加号   增加10音量,ctrl+小键盘减号   减少10音量// 注册了全局热键#include <iostream>#include <Windows.h>#include "SetWinSound.h"using namespace std;int main(){    cout << "hello!!!" << endl;    /*     * MOD_CONTROL:ctrl     * VK_ADD:小键盘的加号     * VK_SUBTRACT:小键盘减号     */    if (0 == RegisterHotKey(NULL, 1, MOD_CONTROL | MOD_WIN, VK_ADD))        cout << "RegisterHotKey Ctrl+F1 error : " << GetLastError() << endl;    else        cout << "Ctrl+Win+ADD 增加音量10%" <<endl;    if (0 == RegisterHotKey(NULL, 2, MOD_CONTROL| MOD_WIN, VK_SUBTRACT))         cout << "RegisterHotKey Ctrl+F2 error : " << GetLastError() << endl;    else        cout << "Ctrl+Win+SUBSTRACT 减少音量10%" << endl;    SetWinSound sws;    // 消息循环    MSG msg = { 0 };    while (GetMessage(&msg, NULL, 0, 0)) {        switch (msg.message) {        case WM_HOTKEY:        {            if (1 == msg.wParam) {                //sws.SetWindowsSound(50);                sws.AddWindowsSound(10);                cout << "sound add 10" << endl;            }            else if (2 == msg.wParam) {                //sws.SetWindowsSound(10);                sws.AddWindowsSound(-10);                cout << "sound substract 10" << endl;            }            break;        }        default:            break;        }    }    cout << "finished." << endl;    return 0;}

共有1个答案

武峻熙
2024-01-25

你应该在主函数结束前,也就是在消息循环结束后调用 UnregisterHotKey

在你的代码中,主函数结束时并没有调用 UnregisterHotKey 来解除热键的注册。如果你想在程序结束时取消热键的注册,你可以在 return 0; 之前添加以下代码:

UnregisterHotKey(NULL, 1);UnregisterHotKey(NULL, 2);

这样,当程序结束时,就会自动解除热键的注册。

完整的代码可能如下:

// RegisterHotKey.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。// 2024年01月25日 10:35:21 调节系统音量的工具,ctrl+小键盘加号   增加10音量,ctrl+小键盘减号   减少10音量// 注册了全局热键#include <iostream>#include <Windows.h>#include "SetWinSound.h"using namespace std;int main(){ cout << "hello!!!" << endl; /* * MOD_CONTROL:ctrl * VK_ADD:小键盘的加号 * VK_SUBTRACT:小键盘减号 */ if (0 == RegisterHotKey(NULL, 1, MOD_CONTROL | MOD_WIN, VK_ADD)) cout << "RegisterHotKey Ctrl+F1 error : " << GetLastError() << endl; else cout << "Ctrl+Win+ADD 增加音量10%" <<endl; if (0 == RegisterHotKey(NULL, 2, MOD_CONTROL| MOD_WIN, VK_SUBTRACT))  cout << "RegisterHotKey Ctrl+F2 error : " << GetLastError() << endl; else cout << "Ctrl+Win+SUBSTRACT 减少音量10%" << endl; SetWinSound sws; // 消息循环 MSG msg = { 0 }; while (GetMessage(&msg, NULL, 0, 0)) { switch (msg.message) { case WM_HOTKEY: { if (1 == msg.wParam) { //sws.SetWindowsSound(50); sws.AddWindowsSound(10); cout << "sound add 10" << endl; } else if (2 == msg.wParam) { //sws.SetWindowsSound(10); sws.AddWindowsSound(-10); cout << "sound substract 10" << endl; } break; } default: break; } } UnregisterHotKey(NULL, 1); UnregisterHotKey(NULL, 2); cout << "finished." << endl; return 0;}
 类似资料:
  • 问题内容: 我已经通过npm安装了cordova,并且是在全球范围内安装的,现在我想将其删除以将其重新安装在单个应用程序文件夹中 关于如何安全地做任何线索? 问题答案: 尝试全局卸载它,然后转到本地应用程序目录后不带- g标志

  • 问题内容: Linux中是否存在C ++文档?我想要类似C的手册页的内容。例如,有关string,stl,iostream,ifstream等的文档? 问题答案: 如果您使用g 随附的“标准” libstdc ,则可在此处在线获取其文档。 大多数Linux发行版都将它作为特定的软件包脱机使用。例如,对于Debian发行版,它是(例如,在我已安装的Ubuntu计算机上)。通常,文档将放在。 这是关于

  • 我已经通过这种方式安装了webpack: 在包含的目录下运行后:

  • 注意: 自 PHP 5.4.0 开始,register_globals 选项已经被移除并不再使用。这是在提醒你如果你正在升级旧的应用程序的话,你需要注意这一点。 当 register_globals 选项被开启时,它会使许多类型的变量(包括 $_POST, $_GET 和 $_REQUEST)被注册为全局变量。这将很容易使你的程序无法有效地判断数据的来源并导致安全问题。 例如:$_GET['foo

  • EasySwoole有四个全局事件,全部位于框架安装后生成的EasySwooleEvent.php中。 frameInitialize 框架初始化事件 mainServerCreate 主服务创建事件 onRequest Http请求事件 afterAction Http响应后事件 frameInitialize mainServerCreate onRequest afterAction

  • 蜻蜓映射账号注册非常简单,在蜻蜓映射官网进行注册: 以下是相关操作: 打开蜻蜓映射官网,如图所示,在网页最右端有注册链接 点击注册按钮,进入注册页面。 步骤如下: 输入用户名、密码、手机号码 点击【发送验证码】 输入验证码 点击注册