/// <summary> /// Registers a hot key in the system. /// </summary> /// <param name="modifier">The modifiers that are associated with the hot key.</param> /// <param name="key">The key itself that is associated with the hot key.</param> public HotkeyRegistration RegisterHotKey(ModifierKeys modifier, Keys key, Action action) { var id = ++_currentId; var registration = new HotkeyRegistration(this, id, modifier, key, action); // register the hot key. if (!RegisterHotKey(_window.Handle, id, (uint)modifier, (uint)key)) { throw new InvalidOperationException("Couldn’t register the hot key."); } registrations.Add(registration); return(registration); }
private void setHotkey(ModifierKeys modifierKeys, Keys key) { List <string> txt = new List <string>(); txt.Add("Hotkey:"); foreach (ModifierKeys mod in Enum.GetValues(typeof(ModifierKeys))) { if ((modifierKeys & mod) == mod) { txt.Add(mod.ToString()); } } txt.Add(key.ToString()); label_hotkey.Text = string.Join(" ", txt); if (_hotkeyRegistration == null || _hotkeyRegistration.Key != key || _hotkeyRegistration.Modifier != modifierKeys) { _hotkeyRegistration?.Unregister(); try { _hotkeyRegistration = hook.RegisterHotKey(modifierKeys, key, Hook_KeyPress); } catch (InvalidOperationException ex) { label_hotkey.Text = "Could not register key"; } } if (Properties.Settings.Default.hotkeyModifiers != modifierKeys || Properties.Settings.Default.hotkeyKey != key) { Properties.Settings.Default.hotkeyModifiers = modifierKeys; Properties.Settings.Default.hotkeyKey = key; Properties.Settings.Default.Save(); } }
public void Unregister(HotkeyRegistration hotkey) { UnregisterHotKey(_window.Handle, hotkey.Id); registrations.Remove(hotkey); }