@@ -11,19 +11,6 @@ namespace SystemTrayMenu.Helpers
11
11
using SystemTrayMenu . Utilities ;
12
12
using static SystemTrayMenu . Utilities . FormsExtensions ;
13
13
14
- /// <summary>
15
- /// The enumeration of possible modifiers.
16
- /// </summary>
17
- [ Flags ]
18
- public enum KeyboardHookModifierKeys : uint
19
- {
20
- None = 0 ,
21
- Alt = 1 ,
22
- Control = 2 ,
23
- Shift = 4 ,
24
- Win = 8 ,
25
- }
26
-
27
14
public sealed class KeyboardHook : IDisposable
28
15
{
29
16
private readonly Window window = new ( ) ;
@@ -32,83 +19,41 @@ public sealed class KeyboardHook : IDisposable
32
19
public KeyboardHook ( )
33
20
{
34
21
// register the event of the inner native window.
35
- window . KeyPressed += Window_KeyPressed ;
22
+ window . KeyPressed += ( key , modifiers ) => KeyPressed ? . Invoke ( key , modifiers ) ;
36
23
}
37
24
38
25
/// <summary>
39
26
/// A hot key has been pressed.
40
27
/// </summary>
41
- internal event EventHandler < KeyPressedEventArgs > ? KeyPressed ;
28
+ internal event Action < Key , ModifierKeys > ? KeyPressed ;
42
29
43
30
public void Dispose ( )
44
31
{
45
32
// unregister all the registered hot keys.
46
33
for ( int i = currentId ; i > 0 ; i -- )
47
34
{
48
- DllImports . NativeMethods . User32UnregisterHotKey ( window . Handle , i ) ;
35
+ NativeMethods . User32UnregisterHotKey ( window . Handle , i ) ;
49
36
}
50
37
51
38
// dispose the inner native window.
52
- window . KeyPressed -= Window_KeyPressed ;
53
39
window . Dispose ( ) ;
54
40
}
55
41
56
- internal void RegisterHotKey ( )
42
+ internal void RegisterHotKey ( string hotKeyString )
57
43
{
58
- KeyboardHookModifierKeys modifiers = KeyboardHookModifierKeys . None ;
59
- string modifiersString = Properties . Settings . Default . HotKey ;
60
- if ( ! string . IsNullOrEmpty ( modifiersString ) )
61
- {
62
- if ( modifiersString . ToUpperInvariant ( ) . Contains ( "ALT" , StringComparison . InvariantCulture ) )
63
- {
64
- modifiers |= KeyboardHookModifierKeys . Alt ;
65
- }
44
+ // TODO: Replace old code of v1 with HotKeyControl methods like here
45
+ // as there is a bug in the body of this function (missing "+" when checking for modifiers)
46
+ ModifierKeys modifiers = HotkeyControl . HotkeyModifiersFromString ( hotKeyString ) ;
47
+ Key hotkey = HotkeyControl . HotkeyFromString ( hotKeyString ) ;
66
48
67
- if ( modifiersString . ToUpperInvariant ( ) . Contains ( "CTRL" , StringComparison . InvariantCulture ) ||
68
- modifiersString . ToUpperInvariant ( ) . Contains ( "STRG" , StringComparison . InvariantCulture ) )
69
- {
70
- modifiers |= KeyboardHookModifierKeys . Control ;
71
- }
72
-
73
- if ( modifiersString . ToUpperInvariant ( ) . Contains ( "SHIFT" , StringComparison . InvariantCulture ) )
74
- {
75
- modifiers |= KeyboardHookModifierKeys . Shift ;
76
- }
77
-
78
- if ( modifiersString . ToUpperInvariant ( ) . Contains ( "WIN" , StringComparison . InvariantCulture ) )
79
- {
80
- modifiers |= KeyboardHookModifierKeys . Win ;
81
- }
82
- }
83
-
84
- RegisterHotKey ( modifiers , HotkeyControl . HotkeyFromString ( Properties . Settings . Default . HotKey ) ) ;
85
- }
86
-
87
- /// <summary>
88
- /// Registers a hot key in the system.
89
- /// </summary>
90
- /// <param name="key">The key itself that is associated with the hot key.</param>
91
- internal void RegisterHotKey ( Key key )
92
- {
93
- uint keyModifiersNone = 0 ;
94
- RegisterHotKey ( keyModifiersNone , key ) ;
49
+ RegisterHotKey ( ( uint ) modifiers , hotkey ) ;
95
50
}
96
51
97
52
/// <summary>
98
53
/// Registers a hot key in the system.
99
54
/// </summary>
100
55
/// <param name="modifier">The modifiers that are associated with the hot key.</param>
101
56
/// <param name="key">The key itself that is associated with the hot key.</param>
102
- internal void RegisterHotKey ( KeyboardHookModifierKeys modifier , Key key )
103
- {
104
- RegisterHotKey ( ( uint ) modifier , key ) ;
105
- }
106
-
107
- private void Window_KeyPressed ( object ? sender , KeyPressedEventArgs e )
108
- {
109
- KeyPressed ? . Invoke ( this , e ) ;
110
- }
111
-
112
57
private void RegisterHotKey ( uint modifier , Key key )
113
58
{
114
59
currentId += 1 ;
@@ -127,7 +72,7 @@ private class Window : NativeWindow, IDisposable
127
72
{
128
73
private const int WmHotkey = 0x0312 ;
129
74
130
- public event EventHandler < KeyPressedEventArgs > ? KeyPressed ;
75
+ public event Action < Key , ModifierKeys > ? KeyPressed ;
131
76
132
77
/// <summary>
133
78
/// Overridden to get the notifications.
@@ -139,10 +84,10 @@ protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lP
139
84
{
140
85
// get the keys.
141
86
Key key = ( Key ) ( ( ( int ) lParam >> 16 ) & 0xFFFF ) ;
142
- KeyboardHookModifierKeys modifier = ( KeyboardHookModifierKeys ) ( ( int ) lParam & 0xFFFF ) ;
87
+ ModifierKeys modifiers = ( ModifierKeys ) ( ( int ) lParam & 0xFFFF ) ;
143
88
144
89
// invoke the event to notify the parent.
145
- KeyPressed ? . Invoke ( this , new KeyPressedEventArgs ( modifier , key ) ) ;
90
+ KeyPressed ? . Invoke ( key , modifiers ) ;
146
91
}
147
92
148
93
handled = false ;
0 commit comments