Merge pull request #24 from HandyOrg/master

fix
This commit is contained in:
眾尋 authored and GitHub committed 2019-05-24 13:35:50 +08:00
commit 0fe8df5cb6
10 files changed
+160 -14

No files matched your search

@@ -1,8 +1,5 @@
using System;
using System.Globalization;
using System.Threading;
using System.Windows;
using HandyControl.Controls;
using HandyControl.Data;
using HandyControl.Tools;
using HandyControlDemo.Data;
@@ -23,6 +23,10 @@
hc:Dialog.Token="{x:Static data:MessageToken.MainWindow}"
d:DesignHeight="800"
d:DesignWidth="1400">
<hc:GlobalShortcut.KeyBindings>
<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}}"/>
</hc:GlobalShortcut.KeyBindings>
<hc:Window.NonClientAreaContent>
<userControl:NoUserContent/>
</hc:Window.NonClientAreaContent>
@@ -2,6 +2,7 @@
using System.ComponentModel;
using GalaSoft.MvvmLight.Messaging;
using HandyControl.Controls;
using HandyControl.Tools;
using HandyControlDemo.Data;
using HandyControlDemo.Tools;
@@ -21,6 +22,8 @@ namespace HandyControlDemo
Messenger.Default.Send<object>(null, MessageToken.ClearLeftSelected);
Messenger.Default.Send(true, MessageToken.FullSwitch);
Messenger.Default.Send(AssemblyHelper.CreateInternalInstance($"UserControl.{MessageToken.OverView}"), MessageToken.LoadShowContent);
GlobalShortcut.Init(this);
}
protected override void OnClosing(CancelEventArgs e)
@@ -6,6 +6,7 @@ using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.CommandWpf;
#endif
using GalaSoft.MvvmLight.Messaging;
using HandyControl.Controls;
using HandyControlDemo.Data;
using HandyControlDemo.Service;
using HandyControlDemo.Tools;
@@ -98,9 +99,15 @@ namespace HandyControlDemo.ViewModel
public RelayCommand OpenOverviewCmd => new Lazy<RelayCommand>(() =>
new RelayCommand(OpenOverview)).Value;
#endregion
public RelayCommand GlobalShortcutInfoCmd => new Lazy<RelayCommand>(() =>
new RelayCommand(() => Growl.Info("Global Shortcut Info"))).Value;
#region 方法
public RelayCommand GlobalShortcutWarningCmd => new Lazy<RelayCommand>(() =>
new RelayCommand(() => Growl.Warning("Global Shortcut Warning"))).Value;
#endregion
#region 方法
/// <summary>
/// 切换例子
@@ -5,10 +5,13 @@ namespace HandyControl.Data
{
internal class KeyboardHookEventArgs : EventArgs
{
public bool IsSystemKey { get; }
public Key Key { get; }
public KeyboardHookEventArgs(int virtualKey)
public KeyboardHookEventArgs(int virtualKey, bool isSystemKey)
{
IsSystemKey = isSystemKey;
Key = KeyInterop.KeyFromVirtualKey(virtualKey);
}
}
@@ -19,6 +19,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\LinkElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\PanelElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\PasswordBoxMonitor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\GlobalShortcut.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\StatusSwitchElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\TipElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\TitleElement.cs" />
@@ -0,0 +1,87 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using HandyControl.Data;
namespace HandyControl.Tools
{
public class GlobalShortcut
{
private static readonly Dictionary<string, KeyBinding> CommandDic = new Dictionary<string, KeyBinding>();
static GlobalShortcut()
{
KeyboardHook.KeyDown += KeyboardHook_KeyDown;
}
private static void KeyboardHook_KeyDown(object sender, KeyboardHookEventArgs e) => HitTest(e.Key);
private static void HitTest(Key key)
{
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 keyBinding = CommandDic[key];
var command = keyBinding.Command;
if (command != null && command.CanExecute(keyBinding.CommandParameter))
{
command.Execute(keyBinding.CommandParameter);
}
}
}
private static bool IsModifierKey(Key key)
{
return key == Key.LeftCtrl || key == Key.LeftAlt || key == Key.LeftShift || key == Key.LWin ||
key == Key.RightCtrl || key == Key.RightAlt || key == Key.RightShift || key == Key.RWin;
}
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()));
public static void SetKeyBindings(DependencyObject element, InputBindingCollection value)
=> element.SetValue(KeyBindingsProperty, value);
public static InputBindingCollection GetKeyBindings(DependencyObject element)
=> (InputBindingCollection) element.GetValue(KeyBindingsProperty);
}
}
@@ -16,7 +16,24 @@ namespace HandyControl.Tools
private static readonly UnsafeNativeMethods.HookProc Proc = HookCallback;
public static void Start() => HookId = SetHook(Proc);
private static int VirtualKey;
private static readonly IntPtr KeyDownIntPtr = (IntPtr)NativeMethods.WM_KEYDOWN;
private static readonly IntPtr KeyUpIntPtr = (IntPtr)NativeMethods.WM_KEYUP;
private static readonly IntPtr SyskeyDownIntPtr = (IntPtr)NativeMethods.WM_SYSKEYDOWN;
private static readonly IntPtr SyskeyUpIntPtr = (IntPtr)NativeMethods.WM_SYSKEYUP;
public static void Start()
{
if (HookId != IntPtr.Zero)
{
Stop();
}
HookId = SetHook(Proc);
}
public static void Stop() => UnsafeNativeMethods.UnhookWindowsHookEx(HookId);
@@ -38,17 +55,35 @@ namespace HandyControl.Tools
{
if (nCode >= 0)
{
if (wParam == (IntPtr)NativeMethods.WM_KEYDOWN)
if (wParam == KeyDownIntPtr)
{
var virtualKey = Marshal.ReadInt32(lParam);
KeyDown?.Invoke(null, new KeyboardHookEventArgs(virtualKey));
if (VirtualKey != virtualKey)
{
VirtualKey = virtualKey;
KeyDown?.Invoke(null, new KeyboardHookEventArgs(virtualKey, false));
}
}
else if (wParam == (IntPtr)NativeMethods.WM_KEYUP)
else if (wParam == SyskeyDownIntPtr)
{
var virtualKey = Marshal.ReadInt32(lParam);
KeyUp?.Invoke(null, new KeyboardHookEventArgs(virtualKey));
if (VirtualKey != virtualKey)
{
VirtualKey = virtualKey;
KeyDown?.Invoke(null, new KeyboardHookEventArgs(virtualKey, true));
}
}
else if (wParam == KeyUpIntPtr)
{
var virtualKey = Marshal.ReadInt32(lParam);
VirtualKey = -1;
KeyUp?.Invoke(null, new KeyboardHookEventArgs(virtualKey, false));
}
else if (wParam == SyskeyUpIntPtr)
{
var virtualKey = Marshal.ReadInt32(lParam);
VirtualKey = -1;
KeyUp?.Invoke(null, new KeyboardHookEventArgs(virtualKey, true));
}
}
return UnsafeNativeMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
@@ -14,7 +14,14 @@ namespace HandyControl.Tools
private static readonly UnsafeNativeMethods.HookProc Proc = HookCallback;
public static void Start() => HookId = SetHook(Proc);
public static void Start()
{
if (HookId != IntPtr.Zero)
{
Stop();
}
HookId = SetHook(Proc);
}
public static void Stop() => UnsafeNativeMethods.UnhookWindowsHookEx(HookId);
@@ -33,6 +33,8 @@ namespace HandyControl.Tools.Interop
NIIF_ERROR = 0x00000003,
WM_KEYDOWN = 0x0100,
WM_KEYUP = 0x0101,
WM_SYSKEYDOWN = 0x0104,
WM_SYSKEYUP = 0x0105,
WM_MOUSEMOVE = 0x0200,
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,