2
2
using System . Reflection ;
3
3
using System . Runtime . InteropServices ;
4
4
using System . Windows . Input ;
5
+ using System . Windows . Threading ;
5
6
6
7
namespace Nimble . Helpers
7
8
{
8
9
public partial class KeyboardHook
9
10
{
11
+ private DispatcherTimer _dispatcherTimer ;
12
+
10
13
#region pinvoke details
11
14
12
15
private enum HookType : int
@@ -64,6 +67,27 @@ public KeyboardHook()
64
67
{
65
68
_hookFunction = new HookProc ( HookCallback ) ;
66
69
Install ( ) ;
70
+ var osVersion = Environment . OSVersion . Version ;
71
+ if ( osVersion . Major > 6 || ( osVersion . Major == 6 && osVersion . Minor >= 2 ) )
72
+ {
73
+ _dispatcherTimer = new DispatcherTimer ( ) ;
74
+ _dispatcherTimer . Tick += _dispatcherTimer_Tick ;
75
+ _dispatcherTimer . Interval = new TimeSpan ( 3 , 0 , 0 ) ;
76
+ _dispatcherTimer . Start ( ) ;
77
+ }
78
+ }
79
+
80
+ void CreateKeyHook ( )
81
+ {
82
+ Uninstall ( ) ;
83
+ Install ( ) ;
84
+ }
85
+
86
+ private void _dispatcherTimer_Tick ( object sender , EventArgs e )
87
+ {
88
+ _dispatcherTimer . Stop ( ) ;
89
+ CreateKeyHook ( ) ;
90
+ _dispatcherTimer . Start ( ) ;
67
91
}
68
92
69
93
~ KeyboardHook ( )
@@ -76,11 +100,11 @@ private int HookCallback(int code, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
76
100
if ( code < 0 )
77
101
return CallNextHookEx ( _hookHandle , code , wParam , ref lParam ) ;
78
102
79
- if ( ( lParam . flags & 0x80 ) != 0 && this . KeyUp != null )
80
- this . KeyUp ( this , new HookEventArgs ( lParam . vkCode ) ) ;
103
+ if ( ( lParam . flags & 0x80 ) != 0 && KeyUp != null )
104
+ KeyUp ( this , new HookEventArgs ( lParam . vkCode ) ) ;
81
105
82
- if ( ( lParam . flags & 0x80 ) == 0 && this . KeyDown != null )
83
- this . KeyDown ( this , new HookEventArgs ( lParam . vkCode ) ) ;
106
+ if ( ( lParam . flags & 0x80 ) == 0 && KeyDown != null )
107
+ KeyDown ( this , new HookEventArgs ( lParam . vkCode ) ) ;
84
108
85
109
return CallNextHookEx ( _hookHandle , code , wParam , ref lParam ) ;
86
110
}
@@ -90,7 +114,7 @@ private void Install()
90
114
if ( _hookHandle != IntPtr . Zero )
91
115
return ;
92
116
93
- Module [ ] list = System . Reflection . Assembly . GetExecutingAssembly ( ) . GetModules ( ) ;
117
+ Module [ ] list = Assembly . GetExecutingAssembly ( ) . GetModules ( ) ;
94
118
System . Diagnostics . Debug . Assert ( list != null && list . Length > 0 ) ;
95
119
96
120
_hookHandle = SetWindowsHookEx ( _hookType ,
@@ -114,7 +138,7 @@ public class HookEventArgs : EventArgs
114
138
115
139
public HookEventArgs ( UInt32 keyCode )
116
140
{
117
- this . Key = System . Windows . Input . KeyInterop . KeyFromVirtualKey ( ( int ) keyCode ) ;
141
+ Key = KeyInterop . KeyFromVirtualKey ( ( int ) keyCode ) ;
118
142
}
119
143
}
120
144
0 commit comments