tags:

views:

418

answers:

1

I'm looking for a way to programmatically detect hotkeys in the system. My app supports configurable hotkeys to do different things, and I'd like to be able to tell if another app has snagged one already or it's a built-in Windows hotkey (like Win-L to lock the workstation).

And if it is another app that owns the hotkey, I'd like to be able to show that to the user. At least the name of the exe.

I'm aware of three ways to do hotkeys:

  • System hook using standard API's
  • Using the hotkey feature in the properties dialog for a shortcut
  • Polling async key state and responding

I doubt I can detect the third type, but what about the other two? And are there any other situations I need to know about?

+3  A: 

I can think of three ways to do it with Standard API:

  • RegisterHotkey

  • SetWindowsHookEx(WH_KEYBOARD)

  • SetWindowsHookEx(WH_LL_KEYBOARD)

With the first approach, you will get in the return value whether another application already registered the very same hotkey (or whether a shortcut uses this hotkey, or Explorer.exe registered the hotkey because it is Win+E or win+R). You don't get the application name this way, though.

Using Windows Hooks or async key states for "hotkeys": I don't think it is possible to detect hotkeys there, since you might use hotkeys in a context (like replace "t" by "irst" if the last four keystrokes were "Fris") that way. You could inject the hotkey using keybd_event (with your window focused) and test if the event "gets through"; on the other hands, some cases of "hotkeys" that are implemented via hooks do not consume the keystroke so it will still get through.

The approach I would use: First make sure that for entering a shortcut, you have to type that exact shortcut into your shortcut box (if that fails, the user will see which application uses it). Then use RegisterHotkey, so you will notice (in future sessions) if another "well-behaving" application tried to steal this shortcut from you.

mihi