/// <summary> /// Must unregister registered hot keys before destroying the native driver window. /// </summary> public void Dispose() { if (isDisposed) { return; } if (window != null) { foreach (HotKey key in keys.Values) { KeyInterops.UnregisterHotKey(window.Handle, key.HotId); } } if (keys != null) { keys.Clear(); keys = null; } if (window != null) { window.KeyPressed -= DoKeyPressed; window.Dispose(); window = null; } isDisposed = true; GC.SuppressFinalize(this); }
/// <summary> /// Unregisters the given key sequence as a known hot key. /// </summary> /// <param name="code">The primary keyboard key-code identifier.</param> /// <param name="modifier">The secondary keyboard modifiers bit mask.</param> public void UnregisterHotKey(Keys code, KeyModifier modifier) { HotKey key = keys.Values .FirstOrDefault(p => (p.Code == code) && (p.Modifier == modifier)); if (key != null) { keys.Remove(key.Action); if ((key.Code != Keys.None) && (key.Modifier != KeyModifier.None)) { if (!KeyInterops.UnregisterHotKey(window.Handle, key.HotId)) { MessageWindow.Show(string.Format( Resx.HotKeyNotUnregistered, key.Action, key)); } } } }
//======================================================================================== // Methods //======================================================================================== /// <summary> /// Register a new hot key sequence, associating it with the specified action. /// </summary> /// <param name="code">The primary keyboard key-code identifier.</param> /// <param name="modifier">The secondary keyboard modifiers bit mask.</param> /// <param name="action">The well-known action to associated with the hot key.</param> public void RegisterHotKey(Keys code, KeyModifier modifier, HotKeyAction action) { HotKey key = new HotKey(action, code, modifier); bool ok = true; if (code != Keys.None) { try { ok = KeyInterops.RegisterHotKey(window.Handle, key.HotId, (int)modifier, (int)code); } catch (Exception exc) { Logger.WriteLine("RegisterHotKey", exc); } } if (ok) { keys.Add(key.Action, key); } else { MessageWindow.Show(string.Format(Resx.HotKeyNotRegistered, key.Action, key)); key.Code = Keys.None; key.Modifier = KeyModifier.None; if (keys.ContainsKey(action)) { keys[action] = key; } else { keys.Add(action, key); } } }