捕捉并禁用Sleep和Win L按钮 [英] Catch and disable Sleep and Win L buttons

查看:134
本文介绍了捕捉并禁用Sleep和Win L按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个低级键盘钩子来过滤掉任何不需要的键,这些键允许用户(在这种情况下是我两岁的女儿)切换到另一个应用程序或者能够离开当前的应用程序。到目前为止唯一的问题是睡眠(VK_SLEEP)和Win +''L''(注销)键。这些似乎都在我的钩子函数调用之前就已经采取了默认操作。



有人知道如何在我的应用程序运行时禁用这两个键吗?



I have set up a low level keyboard hook to filter out any unwanted keys that would allow a user (in this case my two year old daughter) from switching to another app or otherwise being able to leave the current app. The only problems so far are the sleep (VK_SLEEP) and Win + ''L'' (Log Off) keys. These both seem to have their default actions taken even before my hook function is called.

Does anyone have any idea how to disable these two keys while my app is running?

DWORD BadVKCodes[] = { VK_MENU, VK_PAUSE, VK_PRINT, VK_EXECUTE, VK_SNAPSHOT,
                       VK_HELP, VK_LWIN, VK_RWIN, VK_APPS, VK_SLEEP, VK_F1,
                       VK_BROWSER_SEARCH, VK_BROWSER_FAVORITES, VK_BROWSER_HOME,
                       VK_VOLUME_MUTE, VK_VOLUME_DOWN, VK_VOLUME_UP,
                       VK_MEDIA_NEXT_TRACK, VK_MEDIA_PREV_TRACK, VK_MEDIA_STOP,
                       VK_LAUNCH_MAIL, VK_LAUNCH_MEDIA_SELECT, VK_LAUNCH_APP1, VK_LAUNCH_APP2,
                       VK_ICO_HELP, VK_PROCESSKEY, VK_PACKET };

LRESULT CALLBACK LowLevelKeyboardHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode < 0 || g_hWnd == NULL) // g_hWnd is my global window handle
    {
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    }

    KBDLLHOOKSTRUCT *khs = reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);
    const int VKCodesCount = _countof(BadVKCodes);

    if ((khs->flags & LLKHF_ALTDOWN) == LLKHF_ALTDOWN)
    {
        // ALT key is down, do not want system messages, eat it
        return 1;
    }

    if (std::find(BadVKCodes, BadVKCodes + VKCodesCount, khs->vkCode) != BadVKCodes + VKCodesCount)
    {
        // vkCode is a bad code, eat it
        return 1;
    }

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

推荐答案

您可以禁用Windows键(或使用没有此键的键盘)。这应该可以防止执行注销和其他命令。请参阅知识库文章如何禁用键盘Windows键 [ ^ ]。其他键 - 如键盘上的睡眠键 - 可以添加到注册表值中。结构是:

You may disable the Windows key (or use a keyboard without this key). This should prevent the excution of the logoff and other commands. See the KB article How to disable the keyboard Windows key[^]. Additional keys - like a sleep key on your keyboard - can be added to the registry value. The structure is:
DWORD HeaderVersionInfo: 0
DWORD HeaderFlags: 0
DWORD NumberOfMappings: including end marker (2 with one mapping)
Mappings (NumberOfMappings - 1 entries)
 WORD ScanCode
 WORD Replacement: 0 to disable key, or scan code of other key
DWORD EndMarker: 0



要使用电源按钮禁用睡眠/待机,请使用电源管理控制面板。


To disable sleep/standby using the power button, use the power management control panel.


我解决了睡眠按钮问题。请参阅我的提示:如何禁用睡眠按钮你的代码正在运行 [ ^ ]



不确定为什么删除此解决方案但我已重新提交。
I solved the Sleep button issue. See my tip: How to disable the Sleep button while your code is running[^]

Not sure why this solution was deleted but I have resubmitted it.


这篇关于捕捉并禁用Sleep和Win L按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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