废弃 GlobalShortcut.Init
This commit is contained in:
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.Input;
|
||||
using HandyControl.Data;
|
||||
@@ -7,11 +9,12 @@ namespace HandyControl.Tools
|
||||
{
|
||||
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()
|
||||
{
|
||||
KeyboardHook.KeyDown += KeyboardHook_KeyDown;
|
||||
KeyboardHook.Start();
|
||||
}
|
||||
|
||||
private static void KeyboardHook_KeyDown(object sender, KeyboardHookEventArgs e) => HitTest(e.Key);
|
||||
@@ -20,23 +23,14 @@ namespace HandyControl.Tools
|
||||
{
|
||||
if (IsModifierKey(key)) return;
|
||||
|
||||
var modifierKeys = Keyboard.Modifiers;
|
||||
var keyStr = modifierKeys != ModifierKeys.None ? $"{modifierKeys.ToString()}, {key.ToString()}" : key.ToString();
|
||||
ExecuteCommand(keyStr);
|
||||
}
|
||||
|
||||
private static void ExecuteCommand(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key)) return;
|
||||
if (CommandDic.ContainsKey(key))
|
||||
var keyBindings = InputBindingCollectionSet
|
||||
.SelectMany(inputBindingCollection => inputBindingCollection.OfType<KeyBinding>())
|
||||
.Where(keyBinding => keyBinding.Key != Key.None && keyBinding.Modifiers == Keyboard.Modifiers && keyBinding.Key == key);
|
||||
keyBindings.ToList().ForEach(keyBinding =>
|
||||
{
|
||||
var keyBinding = CommandDic[key];
|
||||
var command = keyBinding.Command;
|
||||
if (command != null && command.CanExecute(keyBinding.CommandParameter))
|
||||
{
|
||||
command.Execute(keyBinding.CommandParameter);
|
||||
}
|
||||
}
|
||||
if (keyBinding.Command?.CanExecute(keyBinding.CommandParameter) == true)
|
||||
keyBinding.Command?.Execute(keyBinding.CommandParameter);
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
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(
|
||||
"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)
|
||||
=> element.SetValue(KeyBindingsProperty, value);
|
||||
|
||||
public static InputBindingCollection GetKeyBindings(DependencyObject element)
|
||||
=> (InputBindingCollection) element.GetValue(KeyBindingsProperty);
|
||||
=> (InputBindingCollection)element.GetValue(KeyBindingsProperty);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user