1

What is the best way to intercept the Caps Lock button on Windows, for making a program like Launchy?

Currently, I'm setting a low-level hook with SetWindowsHookEx, but that's a bit too low-level for me, since I don't want to intercept other programs that are trying to be active a low level. I'm looking for the highest possible level of interception that can still prevent turning on Caps Lock itself... any better suggestions?

user541686
  • 205,094
  • 128
  • 528
  • 886

2 Answers2

0

You can use RegisterHotKey:

RegisterHotKey(hWnd, 0, 0, VK_CAPITAL);

Your window will receive a WM_HOTKEY message whenever this key is pressed.

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • 2
    This doesn't prevent Caps Lock from toggling its state -- it's too high-level. :( – user541686 Mar 16 '11 at 22:30
  • @Mehrdad: That's true, but you can't "prevent" this without using a hook. You can however process the hotkey and reset the state using [`SetKeyboardState`](http://msdn.microsoft.com/en-us/library/ms646314%28VS.85%29.aspx), but using a hook will be more efficient. – casablanca Mar 16 '11 at 22:53
  • Ah, so there's no other way? Is there any higher-level hook than a low-level keyboard hook that I could use, or is that my last (and only) option? – user541686 Mar 16 '11 at 23:09
  • @Mehrdad: None that I'm aware of. But is there a reason you don't want to use low-level hooks? – casablanca Mar 16 '11 at 23:11
  • Oh, I can definitely do that (and that's what I'm already doing, as I mentioned in my post). The problem is I just don't need this much power -- I don't want my program to intercept the caps lock key before other low-level programs, like virtual machines. But if there's no other options then I'll try this. – user541686 Mar 17 '11 at 00:04
0

Apparently the best way is to use a low-level hook, since RegisterHotkey doesn't intercept the key.

user541686
  • 205,094
  • 128
  • 528
  • 886