如何在使用C ++的软件中禁用特定的Windows热键? [英] How can I disable specific Windows hotkeys from inside a software using C++?

查看:393
本文介绍了如何在使用C ++的软件中禁用特定的Windows热键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在软件中放置必要的代码,以禁用Windows(Xp,Vista,特别是7和配置文件)热键,可以允许用户离开软件。

I want to put inside a software the necessary codes for it to disable Windows (Xp, Vista, specially 7 and sucessors) hotkeys that could allow the user to get away from the software.

详细信息:


  • 命令必须仅在软件运行时有效;当它不运行时,必须启用Windows热键(所以:如果用户启动软件,热键被禁用;如果他关闭了,必须重新启用)。

  • 't想禁用所有的窗口热键(一些关键的热键,如ctrl + alt + del必须仍然可操作),所以解决方案,如某种窗口配置,禁用所有的热键对我来说是无用的。

  • hotkey昵称包含Windows按钮。

  • 代码必须使用C ++或Windows函数。

  • 优先而不要求终止explorer.exe。

  • The commands must be valid only while the software is running; when its not running, Windows hotkeys must be enabled (so: if the user starts the software, the hotkeys are disable; if he closes it, must be re-enabled).
  • I don't want to disable all windows hotkeys (some key hotkeys such as ctrl+alt+del must still be operational), so solutions such as some sort of windows configuration that disable all hotkeys are useless for me. I must be capable of specifically select which hotkeys I want to continue working and which I don't.
  • The "hotkey" nickname includes the Windows button.
  • The code must be either using C++ or Windows functions.
  • Preferetially without requiering to terminate explorer.exe.

我在stackoverflow里看过一个post,有一个非常类似于我的情况(阻止用户通过系统热键退出Windows应用程序),但是据我所理解的解决方案,适用于我的具体情况,我didn

I did look at a post here in stackoverflow that had a very similiar case as mine (Prevent users from quitting a windows application via system hotkeys), but as far as I understood nether of the solutions presented were applicable to my specific situation, and I didn't find anything in the web as well.

推荐答案

好的,我得到了怎么办。该代码能够使用低级键盘钩子创建一个没有DLL的系统级钩子。这里是代码(更好地解释)(使用Qt):

Ok, I got how to do it. The code is able to create a sistem-wide hook without DLL using a low level keyboard hook. Here is the code (better that explaining)(using Qt):

//Installing the hook
SWH_return = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,GetModuleHandle(NULL),0);

if (SWH_return != NULL)
    qDebug() << "Hook true";
else
    qDebug() << "Hook false";

//Uninstalling the hook
bool teste = false;

teste = UnhookWindowsHookEx(SWH_return);

if (teste)
    qDebug() << "Unhook: true";
else
    qDebug() << "Unhook: false";

//The function responsible for detecting the keystrokes
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
    return CallNextHookEx(NULL, nCode, wParam, lParam);

tagKBDLLHOOKSTRUCT *str = (tagKBDLLHOOKSTRUCT *)lParam;

switch(str->flags)
{
    case (LLKHF_ALTDOWN):
        qDebug() << "ALT";
        delete str;
    return 1;
}

if (wParam == WM_KEYDOWN)
{
    switch (str->vkCode)
    {
        case VK_RWIN:
        case VK_LWIN:
        case VK_LCONTROL:
        case VK_RCONTROL:
        case VK_APPS:
        case VK_SLEEP:
        case VK_MENU:
            qDebug() << "SPECIAL PRESS";
            delete str;
        return 1;
    }
}

return CallNextHookEx(NULL, nCode, wParam, lParam);
}

这最后一个函数不需要在.h或.cpp文件。它阻止Ctrl,Windows键和Alt的输入。

This last function don't need any declaration in the .h or in the .cpp file. It blocks inputs of Ctrl, Windows Key and Alt. The other two must be placed respectively in the functions where the user wants to begin the key disabling and when he want it to stop.

感谢您,

Momergil。

这篇关于如何在使用C ++的软件中禁用特定的Windows热键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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