/// <summary> /// Register a new global hot key. This will throw an exception if the hot /// key is already registered. /// </summary> /// <param name="modifiedKey">The modified key.</param> /// <param name="HotKeyCallback">A callback that will be invoked when the HotKey is pressed.</param> public void Register(ModifiedKey modifiedKey, HotKeyCallback hotKeyCallback) { modifiedKey.ThrowIfNull("modifiedKey"); hotKeyCallback.ThrowIfNull("hotKeyCallback"); Register(modifiedKey.Modifiers, modifiedKey.Key, hotKeyCallback); }
/// <summary> /// Creates a GlobalHotKey object. /// </summary> /// <param name="modifier">HotKey modifier keys</param> /// <param name="key">HotKey</param> /// <param name="window">The Window that the HotKey should be registered to</param> /// <param name="callback">The delegate to call when the HotKey is pressed.</param> /// <param name="registerImmediately"> </param> public GlobalHotKey(Modifiers modifier, Keys key, IWin32Window window, HotKeyCallback callback, bool registerImmediately = false) { window.ThrowIfNull("window", "You must provide a form or window to register the HotKey against."); callback.ThrowIfNull("callback", "You must specify a callback in order to do some useful work when your HotKey is pressed."); Modifier = modifier; Key = (int)key; hWnd = window.Handle; Id = GetHashCode(); Callback = callback; if (registerImmediately) { Register(); } }
/// <summary> /// Register a new global hot key. This will throw an exception if the hot /// key is already registered. /// </summary> /// <param name="modifiers">Key modifiers (Shift, Alt etc.) that you want applied.</param> /// <param name="key">The key.</param> /// <param name="HotKeyCallback">A callback that will be invoked when the HotKey is pressed.</param> public void Register(Modifiers modifiers, Keys key, HotKeyCallback hotKeyCallback) { modifiers.ThrowIfNull("modifiers"); key.ThrowIfNull("key"); hotKeyCallback.ThrowIfNull("hotKeyCallback"); string keystr = ModifiedKey.ToString(modifiers, key); log.Debug("RegisterGlobalHotKey, Hot key = " + keystr); // For multi-threaded apps, we need to be careful to use the form's handle // on the same thread as it was initially created. if (MessageLoopForm.InvokeRequired) { MessageLoopForm.Invoke(new MethodInvoker(delegate { InternalRegister(modifiers, key, hotKeyCallback); })); } else { InternalRegister(modifiers, key, hotKeyCallback); } log.Debug(keystr + " successfully registered"); }