Completed the main functions
This commit is contained in:
1 parent
54abf3c663
commit
6e3171259e
10 files changed
+195
-14
No files matched your search
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using HandyControl.Data;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
{
|
||||
public class GlobalShortcut
|
||||
{
|
||||
private static ModifierKeys ModifierKeys;
|
||||
|
||||
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)
|
||||
{
|
||||
Console.WriteLine(key.ToString());
|
||||
if (IsModifierKey(key))
|
||||
{
|
||||
ModifierKeys |= GetModifierKey(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
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 ModifierKeys GetModifierKey(Key key)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case Key.LeftCtrl:
|
||||
case Key.RightCtrl:
|
||||
return ModifierKeys.Control;
|
||||
case Key.LeftAlt:
|
||||
case Key.RightAlt:
|
||||
return ModifierKeys.Alt;
|
||||
case Key.LeftShift:
|
||||
case Key.RightShift:
|
||||
return ModifierKeys.Shift;
|
||||
case Key.LWin:
|
||||
case Key.RWin:
|
||||
return ModifierKeys.Windows;
|
||||
default:
|
||||
return ModifierKeys.None;
|
||||
}
|
||||
}
|
||||
|
||||
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,41 @@ 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);
|
||||
if (VirtualKey != virtualKey)
|
||||
{
|
||||
VirtualKey = virtualKey;
|
||||
KeyUp?.Invoke(null, new KeyboardHookEventArgs(virtualKey, false));
|
||||
}
|
||||
}
|
||||
else if (wParam == SyskeyUpIntPtr)
|
||||
{
|
||||
var virtualKey = Marshal.ReadInt32(lParam);
|
||||
if (VirtualKey != virtualKey)
|
||||
{
|
||||
VirtualKey = virtualKey;
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user