废弃 GlobalShortcut.Init

This commit is contained in:
noctwolf committed 2019-06-28 15:39:55 +08:00
1 parent 4c608c2b84
commit 147464d199
1 file changed
+22 -46
@@ -1,4 +1,6 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using HandyControl.Data; using HandyControl.Data;
@@ -7,11 +9,12 @@ namespace HandyControl.Tools
{ {
public class GlobalShortcut public class GlobalShortcut
{ {
private static readonly Dictionary<string, KeyBinding> CommandDic = new Dictionary<string, KeyBinding>(); private static readonly HashSet<InputBindingCollection> InputBindingCollectionSet = new HashSet<InputBindingCollection>();
static GlobalShortcut() static GlobalShortcut()
{ {
KeyboardHook.KeyDown += KeyboardHook_KeyDown; KeyboardHook.KeyDown += KeyboardHook_KeyDown;
KeyboardHook.Start();
} }
private static void KeyboardHook_KeyDown(object sender, KeyboardHookEventArgs e) => HitTest(e.Key); private static void KeyboardHook_KeyDown(object sender, KeyboardHookEventArgs e) => HitTest(e.Key);
@@ -20,23 +23,14 @@ namespace HandyControl.Tools
{ {
if (IsModifierKey(key)) return; if (IsModifierKey(key)) return;
var modifierKeys = Keyboard.Modifiers; var keyBindings = InputBindingCollectionSet
var keyStr = modifierKeys != ModifierKeys.None ? $"{modifierKeys.ToString()}, {key.ToString()}" : key.ToString(); .SelectMany(inputBindingCollection => inputBindingCollection.OfType<KeyBinding>())
ExecuteCommand(keyStr); .Where(keyBinding => keyBinding.Key != Key.None && keyBinding.Modifiers == Keyboard.Modifiers && keyBinding.Key == key);
} keyBindings.ToList().ForEach(keyBinding =>
private static void ExecuteCommand(string key)
{
if (string.IsNullOrEmpty(key)) return;
if (CommandDic.ContainsKey(key))
{ {
var keyBinding = CommandDic[key]; if (keyBinding.Command?.CanExecute(keyBinding.CommandParameter) == true)
var command = keyBinding.Command; keyBinding.Command?.Execute(keyBinding.CommandParameter);
if (command != null && command.CanExecute(keyBinding.CommandParameter)) });
{
command.Execute(keyBinding.CommandParameter);
}
}
} }
private static bool IsModifierKey(Key key) private static bool IsModifierKey(Key key)
@@ -45,43 +39,25 @@ namespace HandyControl.Tools
key == Key.RightCtrl || key == Key.RightAlt || key == Key.RightShift || key == Key.RWin; key == Key.RightCtrl || key == Key.RightAlt || key == Key.RightShift || key == Key.RWin;
} }
[Obsolete]
public static void Init(DependencyObject host) public static void Init(DependencyObject host)
{ {
CommandDic.Clear();
if (host == null) return;
var keyBindings = GetKeyBindings(host);
if (keyBindings == null || keyBindings.Count == 0) return;
foreach (KeyBinding item in keyBindings)
{
if (item.Key == Key.None)
{
continue;
}
if (item.Modifiers == ModifierKeys.None)
{
CommandDic[item.Key.ToString()] = item;
}
else
{
var keyStr = $"{item.Modifiers.ToString()}, {item.Key.ToString()}";
CommandDic[keyStr] = item;
}
}
KeyboardHook.Start();
} }
public static readonly DependencyProperty KeyBindingsProperty = DependencyProperty.RegisterAttached( public static readonly DependencyProperty KeyBindingsProperty = DependencyProperty.RegisterAttached(
"KeyBindings", typeof(InputBindingCollection), typeof(GlobalShortcut), new PropertyMetadata(new InputBindingCollection())); "KeyBindings", typeof(InputBindingCollection), typeof(GlobalShortcut), new PropertyMetadata(new InputBindingCollection()), OnValidateValue);
private static bool OnValidateValue(object value)
{
if (value is InputBindingCollection inputBindingCollection && !InputBindingCollectionSet.Contains(inputBindingCollection))
InputBindingCollectionSet.Add(inputBindingCollection);
return true;
}
public static void SetKeyBindings(DependencyObject element, InputBindingCollection value) public static void SetKeyBindings(DependencyObject element, InputBindingCollection value)
=> element.SetValue(KeyBindingsProperty, value); => element.SetValue(KeyBindingsProperty, value);
public static InputBindingCollection GetKeyBindings(DependencyObject element) public static InputBindingCollection GetKeyBindings(DependencyObject element)
=> (InputBindingCollection) element.GetValue(KeyBindingsProperty); => (InputBindingCollection)element.GetValue(KeyBindingsProperty);
} }
} }