private static void SetupShortcut(IKeyboardHook hook, string shortcutId, string shortcutKeys, ShortcutPressed action) { if (string.IsNullOrEmpty(shortcutKeys)) { hook.UnregisterGlobalShortcut(shortcutId); return; } var keyPress = KeyExtensions.ToKeyPress(shortcutKeys); try { hook.RegisterGlobalShortcut(shortcutId, keyPress.Modifier, keyPress.Key); } catch (Exception e) { MessageBox.Show( Application.Current.MainWindow, string.Format("Could not register global shortcut: {0}", e.Message), "WARNING"); return; } if (hook[shortcutId] == null) { hook[shortcutId] += action; } }
/// <summary> /// determine what should happen if a HotKey got pressed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void HotKeyPressed(object sender, KeyPressedEventArgs e) { Shortcut result; var jamesHotKey = _managerSettings.JamesHotKey.HotKey; if (e.HotKey.Key == jamesHotKey.Key && e.HotKey.Modifiers == jamesHotKey.ModifierKeys) { result = _managerSettings.JamesHotKey; } else { result = _managerSettings.Shortcuts.FirstOrDefault(key => key.HotKey.Key == e.HotKey.Key && key.HotKey.ModifierKeys == e.HotKey.Modifiers); } ShortcutPressed?.Invoke(result, new EventArgs()); }