winapistatehotkeysnum-lock

Using NUM/CAPS/SCROLL LOCK as RegisterHotKey Modifier in WinAPI


I have the very well-known WinAPI function:

BOOL WINAPI RegisterHotKey(
  __in_opt  HWND hWnd,
  __in      int id,
  __in      UINT fsModifiers,
  __in      UINT vk
);

I see that I supposedly can only use the MOD_ALT, MOD_CONTROL, MOD_SHIFT, MOD_WIN keys, as well as MOD_NOREPEAT.

So I can use the following to create a system-wide hotkey for, say, the A key for the Shift+A combination:

RegisterHotKey(NULL,1,MOD_SHIFT,0x41);

Now the important question:

How can I use the state of either the NUM LOCK / SCROLL LOCK / CAPS LOCK (just like MOD_SHIFT and the other ones) as modifiers of a system-wide hotkey that I shall register, such that the registered key for the key combination behaves as a hotkey when any of such "LOCK" keys are turned on, and also behaves as a normal, non-trapped key when either of the "LOCK" keys are turned off?

I think that a timer and monitoring with something like GetKeyState(VK_NUMLOCK)&0xFFFF to register/unregister hotkeys would be too sloppy of a solution, maybe would slow down or interfere with the system performance, and it sounds like an incomplete way to proceed since it has to do with system-wide key behavior.

What better ways are there?


Solution

  • Depending on the situation I would probably just check for the state of a "LOCK" key at the beginning of your event code.

    Otherwise if you really wanted to you can use SetWindowsHookEx to create a keyboard hook to do your monitoring.