updated the branch

This commit is contained in:
NaBian committed 2019-06-28 23:57:30 +08:00
1 parent 147464d199
commit f53ef87a5d
3 files changed
+112 -52

No files matched your search

@@ -1,28 +1,28 @@
<hc:BlurWindow x:Class="HandyControlDemo.MainWindow" <hc:BlurWindow x:Class="HandyControlDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:userControl="clr-namespace:HandyControlDemo.UserControl" xmlns:userControl="clr-namespace:HandyControlDemo.UserControl"
xmlns:data="clr-namespace:HandyControlDemo.Data" xmlns:data="clr-namespace:HandyControlDemo.Data"
mc:Ignorable="d" mc:Ignorable="d"
BorderThickness="1" BorderThickness="1"
BorderBrush="#262e2f" BorderBrush="#262e2f"
NonClientAreaBackground="#262e2f" NonClientAreaBackground="#262e2f"
OtherButtonHoverBackground="#888580" OtherButtonHoverBackground="#888580"
CloseButtonForeground="White" CloseButtonForeground="White"
OtherButtonHoverForeground="White" OtherButtonHoverForeground="White"
OtherButtonForeground="White" OtherButtonForeground="White"
Style="{StaticResource WindowBlur}" Style="{StaticResource WindowBlur}"
Title="MainWindow" Title="MainWindow"
ResizeMode="CanResizeWithGrip" ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen" WindowStartupLocation="CenterScreen"
Name="MyWindow" hc:GlobalShortcut.Host="True"
DataContext="{Binding Main,Source={StaticResource Locator}}" DataContext="{Binding Main,Source={StaticResource Locator}}"
hc:Dialog.Token="{x:Static data:MessageToken.MainWindow}" hc:Dialog.Token="{x:Static data:MessageToken.MainWindow}"
d:DesignHeight="800" d:DesignHeight="800"
d:DesignWidth="1400"> d:DesignWidth="1400">
<hc:GlobalShortcut.KeyBindings> <hc:GlobalShortcut.KeyBindings>
<KeyBinding Modifiers="Control+Alt" Key="I" Command="{Binding Main.GlobalShortcutInfoCmd, Source={StaticResource Locator}}"/> <KeyBinding Modifiers="Control+Alt" Key="I" Command="{Binding Main.GlobalShortcutInfoCmd, Source={StaticResource Locator}}"/>
<KeyBinding Modifiers="Control+Alt" Key="E" Command="{Binding Main.GlobalShortcutWarningCmd, Source={StaticResource Locator}}"/> <KeyBinding Modifiers="Control+Alt" Key="E" Command="{Binding Main.GlobalShortcutWarningCmd, Source={StaticResource Locator}}"/>
@@ -2,7 +2,6 @@
using System.ComponentModel; using System.ComponentModel;
using GalaSoft.MvvmLight.Messaging; using GalaSoft.MvvmLight.Messaging;
using HandyControl.Controls; using HandyControl.Controls;
using HandyControl.Tools;
using HandyControlDemo.Data; using HandyControlDemo.Data;
using HandyControlDemo.Tools; using HandyControlDemo.Tools;
@@ -22,8 +21,6 @@ namespace HandyControlDemo
Messenger.Default.Send<object>(null, MessageToken.ClearLeftSelected); Messenger.Default.Send<object>(null, MessageToken.ClearLeftSelected);
Messenger.Default.Send(true, MessageToken.FullSwitch); Messenger.Default.Send(true, MessageToken.FullSwitch);
Messenger.Default.Send(AssemblyHelper.CreateInternalInstance($"UserControl.{MessageToken.OverView}"), MessageToken.LoadShowContent); Messenger.Default.Send(AssemblyHelper.CreateInternalInstance($"UserControl.{MessageToken.OverView}"), MessageToken.LoadShowContent);
GlobalShortcut.Init(this);
} }
protected override void OnClosing(CancelEventArgs e) protected override void OnClosing(CancelEventArgs e)
@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using HandyControl.Data; using HandyControl.Data;
@@ -9,28 +10,39 @@ namespace HandyControl.Tools
{ {
public class GlobalShortcut public class GlobalShortcut
{ {
private static readonly HashSet<InputBindingCollection> InputBindingCollectionSet = new HashSet<InputBindingCollection>(); private static ObservableCollection<KeyBinding> KeyBindingCollection;
private static readonly Dictionary<string, KeyBinding> CommandDic = new Dictionary<string, KeyBinding>();
private static void KeyboardHook_KeyDown(object sender, KeyboardHookEventArgs e) => HitTest(e.Key);
static GlobalShortcut() static GlobalShortcut()
{ {
KeyboardHook.KeyDown += KeyboardHook_KeyDown;
KeyboardHook.Start();
}
private static void KeyboardHook_KeyDown(object sender, KeyboardHookEventArgs e) => HitTest(e.Key); KeyboardHook.KeyDown += KeyboardHook_KeyDown;
}
private static void HitTest(Key key) private static void HitTest(Key key)
{ {
if (IsModifierKey(key)) return; if (IsModifierKey(key)) return;
var keyBindings = InputBindingCollectionSet var modifierKeys = Keyboard.Modifiers;
.SelectMany(inputBindingCollection => inputBindingCollection.OfType<KeyBinding>()) var keyStr = modifierKeys != ModifierKeys.None ? $"{modifierKeys.ToString()}, {key.ToString()}" : key.ToString();
.Where(keyBinding => keyBinding.Key != Key.None && keyBinding.Modifiers == Keyboard.Modifiers && keyBinding.Key == key); ExecuteCommand(keyStr);
keyBindings.ToList().ForEach(keyBinding => }
private static void ExecuteCommand(string key)
{
if (string.IsNullOrEmpty(key)) return;
if (CommandDic.ContainsKey(key))
{ {
if (keyBinding.Command?.CanExecute(keyBinding.CommandParameter) == true) var keyBinding = CommandDic[key];
keyBinding.Command?.Execute(keyBinding.CommandParameter); var command = keyBinding.Command;
}); if (command != null && command.CanExecute(keyBinding.CommandParameter))
{
command.Execute(keyBinding.CommandParameter);
}
}
} }
private static bool IsModifierKey(Key key) private static bool IsModifierKey(Key key)
@@ -39,25 +51,76 @@ 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] [Obsolete("pls use Host attach property instead of Init method")]
public static void Init(DependencyObject host) public static void Init(DependencyObject host)
{ {
CommandDic.Clear();
if (host == null) return;
KeyBindingCollection = GetKeyBindings(host);
if (KeyBindingCollection == null || KeyBindingCollection.Count == 0) return;
AddKeyBindings(KeyBindingCollection);
KeyboardHook.Start();
} }
private static void AddKeyBindings(IEnumerable<KeyBinding> keyBindings)
{
foreach (var 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;
}
}
}
public static readonly DependencyProperty HostProperty = DependencyProperty.RegisterAttached(
"Host", typeof(bool), typeof(GlobalShortcut), new PropertyMetadata(ValueBoxes.FalseBox, OnHostChanged));
private static void OnHostChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (DesignerHelper.IsInDesignMode) return;
if (KeyBindingCollection != null)
{
KeyBindingCollection.CollectionChanged -= KeyBindingCollection_CollectionChanged;
}
KeyBindingCollection = GetKeyBindings(d);
if (KeyBindingCollection != null)
{
KeyBindingCollection.CollectionChanged += KeyBindingCollection_CollectionChanged;
}
}
private static void KeyBindingCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
AddKeyBindings(KeyBindingCollection);
KeyboardHook.Start();
}
public static void SetHost(DependencyObject element, bool value)
=> element.SetValue(HostProperty, value);
public static bool GetHost(DependencyObject element)
=> (bool) element.GetValue(HostProperty);
public static readonly DependencyProperty KeyBindingsProperty = DependencyProperty.RegisterAttached( public static readonly DependencyProperty KeyBindingsProperty = DependencyProperty.RegisterAttached(
"KeyBindings", typeof(InputBindingCollection), typeof(GlobalShortcut), new PropertyMetadata(new InputBindingCollection()), OnValidateValue); "KeyBindings", typeof(ObservableCollection<KeyBinding>), typeof(GlobalShortcut), new PropertyMetadata(new ObservableCollection<KeyBinding>()));
private static bool OnValidateValue(object value) public static void SetKeyBindings(DependencyObject element, ObservableCollection<KeyBinding> 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); => element.SetValue(KeyBindingsProperty, value);
public static InputBindingCollection GetKeyBindings(DependencyObject element) public static ObservableCollection<KeyBinding> GetKeyBindings(DependencyObject element)
=> (InputBindingCollection)element.GetValue(KeyBindingsProperty); => (ObservableCollection<KeyBinding>) element.GetValue(KeyBindingsProperty);
} }
} }