Enregistrer la touche de raccourci déjà utilisée

Contexte:

Je veux écouter une séquence de Ctrl+Alt+Left ( Ctrl+Alt+Left ) globalement, donc j’utilise:

 [DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); 

Cela fonctionne très bien avec de nombreuses autres séquences de Ctrl+Alt+PageUp de Ctrl+Alt+PageUp , telles que Ctrl+Alt+PageUp , Ctrl+Alt+PageDown , etc. Mais un problème survient avec Ctrl+Alt+Left , en particulier.


Problème:

Sur un ordinateur, cela fonctionne très bien, comme toute autre séquence de touches de raccourci, mais sur un autre ordinateur, où Ctrl+Alt+Arrow est utilisé pour faire pivoter l’écran, il n’enregistre pas la touche de raccourci (c.-à-d. Qu’il renvoie zéro et ne renvoie pas obtenir des rappels à la poignée de la fenêtre).

MSDN indique: RegisterHotKey échoue si les séquences de touches spécifiées pour la touche de raccourci ont déjà été enregistrées par une autre touche de raccourci.

J’aimerais pouvoir enregistrer cette séquence de touches de raccourci quoi qu’il arrive et, si nécessaire, la remplacer. Je voudrais certainement que l’écran rest sans rotation, du moins aussi longtemps que mon programme est en cours d’exécution.

La modification de la séquence de raccourcis n’est pas vraiment une option, car d’autres ordinateurs peuvent avoir d’autres séquences de raccourcis pouvant également provoquer une panne.


Des questions:

Quelle est la différence entre Ctrl+Alt+Left tant que raccourci clavier en rotation d’écran et un Ctrl+S tant que raccourci clavier de sauvegarde: l’un provoque l’échec mais pas l’autre? (Peut-être est-ce parce que l’un est un raccourci clavier global et le second est contextuel?)

Est-il possible de remplacer entièrement les raccourcis clavier? est-ce une bonne idée?

Plus important encore, comment puis-je assurer que mon raccourci clavier sera enregistré?

Je viens de changer mon approche des raccourcis clavier aux crochets. J’écoute n’importe quel événement de presse de clavier de bas niveau et j’obtiens les états des modificateurs quand un tel événement se déclenche même avec winAPI. Ensuite, j’ai toutes les informations sur la séquence actuellement pressée.

C’est un code très long et moche, mais il est finalement facile à utiliser.

 ///  /// A class that manages a global low level keyboard hook ///  class GlobalKeyboardHook { #region Constant, Structure and Delegate Definitions ///  /// defines the callback type for the hook ///  public delegate int KeyboardHookProc(int code, int wParam, ref KeyboardHookStruct lParam); public struct KeyboardHookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } private const int WH_KEYBOARD_LL = 13; private const int WM_KEYDOWN = 0x100; private const int WM_KEYUP = 0x101; private const int WM_SYSKEYDOWN = 0x104; private const int WM_SYSKEYUP = 0x105; #endregion ///  /// The collections of keys to watch for ///  public List HookedKeys = new List(); ///  /// Handle to the hook, need this to unhook and call the next hook ///  private IntPtr _hhook = IntPtr.Zero; ///  /// Initializes a new instance of the  class and installs the keyboard hook. ///  public GlobalKeyboardHook() { this.Hook(); } ///  /// Releases unmanaged resources and performs other cleanup operations before the ///  is reclaimed by garbage collection and uninstalls the keyboard hook. ///  ~GlobalKeyboardHook() { this.Unhook(); } ///  /// Installs the global hook ///  public void Hook() { IntPtr hInstance = LoadLibrary("User32"); this._hhook = SetWindowsHookEx(WH_KEYBOARD_LL, this.HookProc, hInstance, 0); } ///  /// Uninstalls the global hook ///  public void Unhook() { UnhookWindowsHookEx(this._hhook); } ///  /// The callback for the keyboard hook ///  /// The hook code, if it isn't >= 0, the function shouldn't do anyting /// The event type /// The keyhook event information ///  private int HookProc(int code, int wParam, ref KeyboardHookStruct lParam) { if (code >= 0) { var key = (Keys) lParam.vkCode; if (this.HookedKeys.Contains(key)) { var handler = this.KeyPressed; if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (handler != null)) { ModifierKeys mods = 0; if (Keyboard.IsKeyDown(Keys.Control) || Keyboard.IsKeyDown(Keys.ControlKey) || Keyboard.IsKeyDown(Keys.LControlKey) || Keyboard.IsKeyDown(Keys.RControlKey)) { mods |= ModifierKeys.Control; } if (Keyboard.IsKeyDown(Keys.Shift) || Keyboard.IsKeyDown(Keys.ShiftKey) || Keyboard.IsKeyDown(Keys.LShiftKey) || Keyboard.IsKeyDown(Keys.RShiftKey)) { mods |= ModifierKeys.Shift; } if (Keyboard.IsKeyDown(Keys.LWin) || Keyboard.IsKeyDown(Keys.RWin)) { mods |= ModifierKeys.Win; } if (Keyboard.IsKeyDown(Keys.Alt)) { mods |= ModifierKeys.Alt; } handler(this, new KeyPressedEventArgs(mods, key)); } } } return CallNextHookEx(this._hhook, code, wParam, ref lParam); } public event EventHandler KeyPressed; #region DLL imports ///  /// Sets the windows hook, do the desired event, one of hInstance or threadId must be non-null ///  /// The id of the event you want to hook /// The callback. /// The handle you want to attach the event to, can be null /// The thread you want to attach the event to, can be null /// a handle to the desired hook [DllImport("user32.dll")] private static extern IntPtr SetWindowsHookEx(int idHook, KeyboardHookProc callback, IntPtr hInstance, uint threadId); ///  /// Unhooks the windows hook. ///  /// The hook handle that was returned from SetWindowsHookEx /// True if successful, false otherwise [DllImport("user32.dll")] private static extern bool UnhookWindowsHookEx(IntPtr hInstance); ///  /// Calls the next hook. ///  /// The hook id /// The hook code /// The wparam. /// The lparam. ///  [DllImport("user32.dll")] private static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref KeyboardHookStruct lParam); ///  /// Loads the library. ///  /// Name of the library /// A handle to the library [DllImport("kernel32.dll")] private static extern IntPtr LoadLibrary(ssortingng lpFileName); #endregion } static class Keyboard { [Flags] private enum KeyStates { None = 0, Down = 1, Toggled = 2 } [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] private static extern short GetKeyState(int keyCode); private static KeyStates GetKeyState(Keys key) { KeyStates state = KeyStates.None; short retVal = GetKeyState((int)key); //If the high-order bit is 1, the key is down //otherwise, it is up. if ((retVal & 0x8000) == 0x8000) state |= KeyStates.Down; //If the low-order bit is 1, the key is toggled. if ((retVal & 1) == 1) state |= KeyStates.Toggled; return state; } public static bool IsKeyDown(Keys key) { return KeyStates.Down == (GetKeyState(key) & KeyStates.Down); } public static bool IsKeyToggled(Keys key) { return KeyStates.Toggled == (GetKeyState(key) & KeyStates.Toggled); } } ///  /// Event Args for the event that is fired after the hot key has been pressed. ///  class KeyPressedEventArgs : EventArgs { internal KeyPressedEventArgs(ModifierKeys modifier, Keys key) { this.Modifier = modifier; this.Key = key; this.Ctrl = (modifier & ModifierKeys.Control) != 0; this.Shift = (modifier & ModifierKeys.Shift) != 0; this.Win = (modifier & ModifierKeys.Win) != 0; this.Alt = (modifier & ModifierKeys.Alt) != 0; } public ModifierKeys Modifier { get; private set; } public Keys Key { get; private set; } public readonly bool Ctrl; public readonly bool Shift; public readonly bool Win; public readonly bool Alt; } ///  /// The enumeration of possible modifiers. ///  [Flags] public enum ModifierKeys : uint { Alt = 1, Control = 2, Shift = 4, Win = 8 }