Background:
I want to listen to a hot key sequence (Ctrl+Alt+Left
) globally, so I'm using:
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
This works great with many other hot key sequences, such as Ctrl+Alt+PageUp
, Ctrl+Alt+PageDown
, etc... But a problem occurs with Ctrl+Alt+Left
, specifically.
Problem:
On one computer, it works just fine, like any other hot key sequence, but on a different computer, where Ctrl+Alt+Arrow
is used to rotate the screen, it fails to register the hot key (i.e returns zero and doesn't get callbacks to the window's handle).
MSDN says: RegisterHotKey fails if the keystrokes specified for the hot key have already been registered by another hot key.
I would like to be able to register that hot key sequence no matter what, and if needed, override it. I would certainly want the screen to remain unrotated, at least for as long as my program is running.
Changing the hotkey sequence isn't really an option, since other computers might have other hotkey sequences that may cause failure as well.
Questions:
What is the difference between Ctrl+Alt+Left
as a screen-rotating hotkey and a Ctrl+S
as a saving hotkey, the causes one to fail but not the other? (maybe it is because one is a global hotkey and the second is contextual?)
Is it possible to override hotkeys entirely? Is that a good idea?
Most importantly, how can I assure that my hotkey will be registered?
I've just changed my approach from hotkeys to hooks. I'm listening to any low-level keyboard press event, and getting the modifiers' states when such even fires using winAPI. Then I have full information about currently pressed sequence.
It's very long and ugly code to do all that, but eventually it is easy to work with.