GlobalShortcut.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Collections.Specialized;
  4. using System.Windows;
  5. using System.Windows.Input;
  6. using HandyControl.Data;
  7. namespace HandyControl.Tools;
  8. public class GlobalShortcut
  9. {
  10. private static ObservableCollection<KeyBinding> KeyBindingCollection;
  11. private static readonly Dictionary<string, KeyBinding> CommandDic = new();
  12. private static void KeyboardHook_KeyDown(object sender, KeyboardHookEventArgs e) => HitTest(e.Key);
  13. static GlobalShortcut()
  14. {
  15. KeyboardHook.KeyDown += KeyboardHook_KeyDown;
  16. }
  17. private static void HitTest(Key key)
  18. {
  19. if (IsModifierKey(key)) return;
  20. var modifierKeys = Keyboard.Modifiers;
  21. var keyStr = modifierKeys != ModifierKeys.None ? $"{modifierKeys.ToString()}, {key.ToString()}" : key.ToString();
  22. ExecuteCommand(keyStr);
  23. }
  24. private static void ExecuteCommand(string key)
  25. {
  26. if (string.IsNullOrEmpty(key)) return;
  27. if (CommandDic.ContainsKey(key))
  28. {
  29. var keyBinding = CommandDic[key];
  30. var command = keyBinding.Command;
  31. if (command != null && command.CanExecute(keyBinding.CommandParameter))
  32. {
  33. command.Execute(keyBinding.CommandParameter);
  34. }
  35. }
  36. }
  37. private static bool IsModifierKey(Key key)
  38. {
  39. return key == Key.LeftCtrl || key == Key.LeftAlt || key == Key.LeftShift || key == Key.LWin ||
  40. key == Key.RightCtrl || key == Key.RightAlt || key == Key.RightShift || key == Key.RWin;
  41. }
  42. public static void Init(List<KeyBinding> list)
  43. {
  44. CommandDic.Clear();
  45. if (list == null) return;
  46. KeyBindingCollection = new ObservableCollection<KeyBinding>(list);
  47. if (KeyBindingCollection.Count == 0) return;
  48. AddKeyBindings(KeyBindingCollection);
  49. KeyboardHook.Start();
  50. }
  51. private static void AddKeyBindings(IEnumerable<KeyBinding> keyBindings)
  52. {
  53. foreach (var item in keyBindings)
  54. {
  55. if (item.Key == Key.None) continue;
  56. if (item.Modifiers == ModifierKeys.None)
  57. {
  58. CommandDic[item.Key.ToString()] = item;
  59. }
  60. else
  61. {
  62. var keyStr = $"{item.Modifiers.ToString()}, {item.Key.ToString()}";
  63. CommandDic[keyStr] = item;
  64. }
  65. }
  66. }
  67. public static readonly DependencyProperty HostProperty = DependencyProperty.RegisterAttached(
  68. "Host", typeof(bool), typeof(GlobalShortcut), new PropertyMetadata(ValueBoxes.FalseBox, OnHostChanged));
  69. private static void OnHostChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  70. {
  71. if (DesignerHelper.IsInDesignMode) return;
  72. if (KeyBindingCollection != null)
  73. {
  74. KeyBindingCollection.CollectionChanged -= KeyBindingCollection_CollectionChanged;
  75. }
  76. KeyBindingCollection = GetKeyBindings(d);
  77. if (KeyBindingCollection != null)
  78. {
  79. KeyBindingCollection.CollectionChanged += KeyBindingCollection_CollectionChanged;
  80. }
  81. }
  82. private static void KeyBindingCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  83. {
  84. AddKeyBindings(KeyBindingCollection);
  85. KeyboardHook.Start();
  86. }
  87. public static void SetHost(DependencyObject element, bool value)
  88. => element.SetValue(HostProperty, ValueBoxes.BooleanBox(value));
  89. public static bool GetHost(DependencyObject element)
  90. => (bool) element.GetValue(HostProperty);
  91. public static readonly DependencyProperty KeyBindingsProperty = DependencyProperty.RegisterAttached(
  92. "KeyBindings", typeof(ObservableCollection<KeyBinding>), typeof(GlobalShortcut), new PropertyMetadata(new ObservableCollection<KeyBinding>()));
  93. public static void SetKeyBindings(DependencyObject element, ObservableCollection<KeyBinding> value)
  94. => element.SetValue(KeyBindingsProperty, value);
  95. public static ObservableCollection<KeyBinding> GetKeyBindings(DependencyObject element)
  96. => (ObservableCollection<KeyBinding>) element.GetValue(KeyBindingsProperty);
  97. }