Merge pull request #113 from HandyOrg/pr/112

Pr/112
This commit is contained in:
NaBian authored and GitHub committed 2019-06-28 23:58:04 +08:00
commit e13c37570c
3 files changed
+55 -19

No files matched your search

@@ -18,7 +18,7 @@
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"
@@ -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,4 +1,7 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
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;
@@ -7,15 +10,18 @@ namespace HandyControl.Tools
{ {
public class GlobalShortcut public class GlobalShortcut
{ {
private static ObservableCollection<KeyBinding> KeyBindingCollection;
private static readonly Dictionary<string, KeyBinding> CommandDic = new Dictionary<string, KeyBinding>(); 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.KeyDown += KeyboardHook_KeyDown;
} }
private static void KeyboardHook_KeyDown(object sender, KeyboardHookEventArgs e) => HitTest(e.Key);
private static void HitTest(Key key) private static void HitTest(Key key)
{ {
if (IsModifierKey(key)) return; if (IsModifierKey(key)) return;
@@ -45,22 +51,27 @@ 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("pls use Host attach property instead of Init method")]
public static void Init(DependencyObject host) public static void Init(DependencyObject host)
{ {
CommandDic.Clear(); CommandDic.Clear();
if (host == null) return; if (host == null) return;
var keyBindings = GetKeyBindings(host); KeyBindingCollection = GetKeyBindings(host);
if (keyBindings == null || keyBindings.Count == 0) return; if (KeyBindingCollection == null || KeyBindingCollection.Count == 0) return;
foreach (KeyBinding item in keyBindings) AddKeyBindings(KeyBindingCollection);
{
if (item.Key == Key.None) KeyboardHook.Start();
{
continue;
} }
private static void AddKeyBindings(IEnumerable<KeyBinding> keyBindings)
{
foreach (var item in keyBindings)
{
if (item.Key == Key.None) continue;
if (item.Modifiers == ModifierKeys.None) if (item.Modifiers == ModifierKeys.None)
{ {
CommandDic[item.Key.ToString()] = item; CommandDic[item.Key.ToString()] = item;
@@ -71,17 +82,45 @@ namespace HandyControl.Tools
CommandDic[keyStr] = item; 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(); KeyboardHook.Start();
} }
public static readonly DependencyProperty KeyBindingsProperty = DependencyProperty.RegisterAttached( public static void SetHost(DependencyObject element, bool value)
"KeyBindings", typeof(InputBindingCollection), typeof(GlobalShortcut), new PropertyMetadata(new InputBindingCollection())); => element.SetValue(HostProperty, value);
public static void SetKeyBindings(DependencyObject element, InputBindingCollection value) public static bool GetHost(DependencyObject element)
=> (bool) element.GetValue(HostProperty);
public static readonly DependencyProperty KeyBindingsProperty = DependencyProperty.RegisterAttached(
"KeyBindings", typeof(ObservableCollection<KeyBinding>), typeof(GlobalShortcut), new PropertyMetadata(new ObservableCollection<KeyBinding>()));
public static void SetKeyBindings(DependencyObject element, ObservableCollection<KeyBinding> 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);
} }
} }