Merge pull request #87 from HandyOrg/add-Keyboard-Hook
add KeyboardHook
This commit is contained in:
7 files changed
+81
-5
No files matched your search
@@ -6,7 +6,6 @@ using System.Windows.Input;
|
|||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using HandyControl.Data;
|
using HandyControl.Data;
|
||||||
using HandyControl.Interactivity;
|
using HandyControl.Interactivity;
|
||||||
using HandyControl.Tools;
|
|
||||||
|
|
||||||
|
|
||||||
namespace HandyControl.Controls
|
namespace HandyControl.Controls
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Windows.Input;
|
||||||
|
|
||||||
|
namespace HandyControl.Data
|
||||||
|
{
|
||||||
|
internal class KeyboardHookEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
public Key Key { get; }
|
||||||
|
|
||||||
|
public KeyboardHookEventArgs(int virtualKey)
|
||||||
|
{
|
||||||
|
Key = KeyInterop.KeyFromVirtualKey(virtualKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -113,6 +113,7 @@
|
|||||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Window\Window.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Controls\Window\Window.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\CancelRoutedEventArgs.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\CancelRoutedEventArgs.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\FunctionEventArgs!1.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\FunctionEventArgs!1.cs" />
|
||||||
|
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\KeyboardHookEventArgs.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\MouseHookEventArgs.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\MouseHookEventArgs.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Data\ColorRange.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Data\ColorRange.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\BadgeStatus.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\BadgeStatus.cs" />
|
||||||
@@ -246,6 +247,7 @@
|
|||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\SingleOpenHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\SingleOpenHelper.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\ValidateHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\ValidateHelper.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\VisualHelper.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\VisualHelper.cs" />
|
||||||
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Hook\KeyboardHook.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Hook\MouseHook.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Hook\MouseHook.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\BitmapHandle.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\BitmapHandle.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\BITMAPINFO.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\BITMAPINFO.cs" />
|
||||||
@@ -261,7 +263,7 @@
|
|||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\UnsafeNativeMethods.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\UnsafeNativeMethods.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\WNDCLASS.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\WNDCLASS.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\WpfSafeHandle.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\WpfSafeHandle.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\MouseDragElementBehaviorEx.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\MouseDragElementBehaviorEx.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\RegularJudgment.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\RegularJudgment.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\RegularPatterns.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\RegularPatterns.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Tools\StyleSelector\RadioGroupItemStyleSelector.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Tools\StyleSelector\RadioGroupItemStyleSelector.cs" />
|
||||||
|
|||||||
+2
-2
@@ -2,9 +2,9 @@
|
|||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using HandyControl.Data;
|
using HandyControl.Data;
|
||||||
using HandyControl.Interactivity;
|
using HandyControl.Tools;
|
||||||
|
|
||||||
namespace HandyControl.Tools
|
namespace HandyControl.Interactivity
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 鼠标拖动行为,该类是反编译微软的System.Windows.Interactivity程序集得到的,并对其做了些扩展
|
/// 鼠标拖动行为,该类是反编译微软的System.Windows.Interactivity程序集得到的,并对其做了些扩展
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using HandyControl.Data;
|
||||||
|
using HandyControl.Tools.Interop;
|
||||||
|
|
||||||
|
namespace HandyControl.Tools
|
||||||
|
{
|
||||||
|
internal class KeyboardHook
|
||||||
|
{
|
||||||
|
public static event EventHandler<KeyboardHookEventArgs> KeyDown;
|
||||||
|
|
||||||
|
public static event EventHandler<KeyboardHookEventArgs> KeyUp;
|
||||||
|
|
||||||
|
private static IntPtr HookId = IntPtr.Zero;
|
||||||
|
|
||||||
|
private static readonly UnsafeNativeMethods.HookProc Proc = HookCallback;
|
||||||
|
|
||||||
|
public static void Start() => HookId = SetHook(Proc);
|
||||||
|
|
||||||
|
public static void Stop() => UnsafeNativeMethods.UnhookWindowsHookEx(HookId);
|
||||||
|
|
||||||
|
private static IntPtr SetHook(UnsafeNativeMethods.HookProc proc)
|
||||||
|
{
|
||||||
|
using (var curProcess = Process.GetCurrentProcess())
|
||||||
|
using (var curModule = curProcess.MainModule)
|
||||||
|
{
|
||||||
|
if (curModule != null)
|
||||||
|
{
|
||||||
|
return UnsafeNativeMethods.SetWindowsHookEx((int)UnsafeNativeMethods.HookType.WH_KEYBOARD_LL, proc,
|
||||||
|
UnsafeNativeMethods.GetModuleHandle(curModule.ModuleName), 0);
|
||||||
|
}
|
||||||
|
return IntPtr.Zero;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
|
||||||
|
{
|
||||||
|
if (nCode >= 0)
|
||||||
|
{
|
||||||
|
if (wParam == (IntPtr)NativeMethods.WM_KEYDOWN)
|
||||||
|
{
|
||||||
|
var virtualKey = Marshal.ReadInt32(lParam);
|
||||||
|
|
||||||
|
KeyDown?.Invoke(null, new KeyboardHookEventArgs(virtualKey));
|
||||||
|
}
|
||||||
|
else if (wParam == (IntPtr)NativeMethods.WM_KEYUP)
|
||||||
|
{
|
||||||
|
var virtualKey = Marshal.ReadInt32(lParam);
|
||||||
|
|
||||||
|
KeyUp?.Invoke(null, new KeyboardHookEventArgs(virtualKey));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return UnsafeNativeMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,6 +31,8 @@ namespace HandyControl.Tools.Interop
|
|||||||
NIIF_INFO = 0x00000001,
|
NIIF_INFO = 0x00000001,
|
||||||
NIIF_WARNING = 0x00000002,
|
NIIF_WARNING = 0x00000002,
|
||||||
NIIF_ERROR = 0x00000003,
|
NIIF_ERROR = 0x00000003,
|
||||||
|
WM_KEYDOWN = 0x0100,
|
||||||
|
WM_KEYUP = 0x0101,
|
||||||
WM_MOUSEMOVE = 0x0200,
|
WM_MOUSEMOVE = 0x0200,
|
||||||
WM_LBUTTONDOWN = 0x0201,
|
WM_LBUTTONDOWN = 0x0201,
|
||||||
WM_LBUTTONUP = 0x0202,
|
WM_LBUTTONUP = 0x0202,
|
||||||
|
|||||||
@@ -186,7 +186,8 @@ namespace HandyControl.Tools.Interop
|
|||||||
|
|
||||||
public enum HookType
|
public enum HookType
|
||||||
{
|
{
|
||||||
WH_MOUSE_LL = 14,
|
WH_KEYBOARD_LL = 13,
|
||||||
|
WH_MOUSE_LL = 14
|
||||||
}
|
}
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
|||||||
Reference in new issue
Block a user