private void Mainform_Load(object sender, EventArgs e) { Console.WriteLine("Registering"); if (ghk.Register()) { Console.WriteLine("Hotkey Registered"); } else { Console.WriteLine("Hotkey Failed to Register"); } }
public bool SetHotKey(KeyCombination keyCombination) { HotKey?.Dispose(); HotKey = null; if (keyCombination == KeyCombination.None) { return(true); } HotKey = new GlobalHotKey(keyCombination, onHotKeyPressed); if (!HotKey.Register()) { MessageBox.Show("Failed to register global shortcut.\nThis may happen if the defined shortcut is already in use.", "PinWin - Error"); return(false); } return(true); }
public void RegisterHotKey(HotKey hotKey, Action handler) { // Get codes that represent virtual key and modifiers var virtualKey = KeyInterop.VirtualKeyFromKey(hotKey.Key); var modifiers = (int)hotKey.Modifiers; // Register hotkey var registeredHotKey = GlobalHotKey.Register(virtualKey, modifiers, handler); if (registeredHotKey != null) { _registeredHotKeys.Add(registeredHotKey); } else { Debug.WriteLine("Failed to register hotkey."); } }
public void RegisterHotkey() { if (hotkey != null) { hotkey.Unregister(); } Configuration.selectedHotKeyKey = form.comboBox_Key.SelectedIndex; Configuration.selectedHotKeyMod = form.comboBox_KeyMod.SelectedIndex; Configuration.Save(); GlobalHotKey.Modifier mod = (GlobalHotKey.Modifier)form.comboBox_KeyMod.SelectedItem; Keys key = (Keys)form.comboBox_Key.SelectedItem; hotkey = new GlobalHotKey((int)mod, key, form); bool success = hotkey.Register(); form.label_HotKeyInfo.Text = success ? "" : "Could not register Hotkey " + mod + "+" + key; form.label_HotKeyInfo.ForeColor = success ? System.Drawing.Color.Black : System.Drawing.Color.DarkRed; }
/// <summary> /// Sets a new key combination as global shortcut for grabbing the current position. /// </summary> private void setNewHotKey(Keys keys) { hotKey?.Dispose(); Settings.ShortcutKey = keys; if (keys == Keys.None) { return; } KeyCombination combination = (KeyCombination)keys; try { hotKey = new GlobalHotKey(combination, (k) => addPositionToLog()); hotKey.Register(); lblHelp.Text = String.Format("Press {0} to capture a position.", combination); conLog.ShortcutKeyDisplayString = combination.ToString(); } catch (InvalidOperationException) { MessageBox.Show( "Registering global keyboard shortcut failed.\nThis could be caused by another app using the same global shortcut.", "MPos - Error"); } }