Click here to Skip to main content
13,048,035 members (81,714 online)
Rate this:
Please Sign up or sign in to vote.
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?

                       VK_HELP, VK_LWIN, VK_RWIN, VK_APPS, VK_SLEEP, VK_F1,
                       VK_VOLUME_MUTE, VK_VOLUME_DOWN, VK_VOLUME_UP,
                       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);
Posted 24-Oct-12 11:27am
PJ Arends85.7K
Sergey Alexandrovich Kryukov 24-Oct-12 18:28pm
My guess is that you code is not helping... (you did not indicate the problem). But generally this question is interesting, I'll voted 4.
PJ Arends 24-Oct-12 18:58pm
The problem (as stated earlier) is that the Sleep and Win+L keys have their default actions taken before my hook procedure is called. So my hook procedure can not filter them out and catch them, thus blocking their default actions.
Sergey Alexandrovich Kryukov 24-Oct-12 19:18pm
Thank you for this note. As far as I understand, indeed, filtering is impossible, but it would be good to know the way to disable such buttons. Sorry, at this moment I just don't know the way.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
PJ Arends 25-Oct-12 16:59pm
Thanks for the info, I may end up doing it this way. Although I was hoping to be able to disable these keys only when my app is running. Your solution involves editing the registry and rebooting the machine. Not quite what I had in mind.
PJ Arends 25-Oct-12 19:36pm
The Control Panel has a setting (Power Options - System Settings) where one can choose what the Sleep button does. I can change it to 'Do nothing' and the Sleep button becomes disabled.
It does not require a reboot to do it this way, so it must be either a registry setting or a power management function (see that is used. Not sure which one.

I followed your link and was able to disable the Windows key, but I was unable to disable the sleep key via key mapping. The only problem is the windows key functionality is now totally disabled, not quite what I was looking for.
Jochen Arndt 26-Oct-12 3:42am
When writing my answer I forgot that changing the key mapping in the registry requires a reboot. So it is of course an uncomfortable solution.

The power button is not a keyboard key. So it is coherent that it can't be mapped. I assume that the virtual key code is send by the power management according to the selected action. There should be a method to change the setting programmatically without the need of rebooting using WMI or functions from your link (PowerGetActiveScheme, modify, PowerSetActiveScheme).
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 29 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100