Skip to content

Commit f55af05

Browse files
committed
Fix issue that key modifiers are parsed wrong
(registering the hotkey might have failed) Simplified hot key pressed events Remove obsolete class KeyPressedEventArgs
1 parent f8c9913 commit f55af05

File tree

5 files changed

+51
-132
lines changed

5 files changed

+51
-132
lines changed

Business/KeyboardInput.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ public void Dispose()
3030
hook.Dispose();
3131
}
3232

33-
internal bool RegisterHotKey(string hotKey)
33+
internal bool RegisterHotKey(string hotKeyString)
3434
{
35-
if (!string.IsNullOrEmpty(hotKey))
35+
if (!string.IsNullOrEmpty(hotKeyString))
3636
{
3737
try
3838
{
39-
hook.RegisterHotKey();
40-
hook.KeyPressed += (sender, e) => HotKeyPressed?.Invoke();
39+
hook.RegisterHotKey(hotKeyString);
40+
hook.KeyPressed += (_, _) => HotKeyPressed?.Invoke();
4141
}
4242
catch (InvalidOperationException ex)
4343
{
44-
Log.Warn($"Hotkey cannot be set: '{hotKey}'", ex);
44+
Log.Warn($"Hotkey cannot be set: '{hotKeyString}'", ex);
4545
return false;
4646
}
4747
}

Helpers/KeyPressedEventArgs.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

Helpers/KeyboardHook.cs

Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@ namespace SystemTrayMenu.Helpers
1111
using SystemTrayMenu.Utilities;
1212
using static SystemTrayMenu.Utilities.FormsExtensions;
1313

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-
2714
public sealed class KeyboardHook : IDisposable
2815
{
2916
private readonly Window window = new();
@@ -32,83 +19,41 @@ public sealed class KeyboardHook : IDisposable
3219
public KeyboardHook()
3320
{
3421
// register the event of the inner native window.
35-
window.KeyPressed += Window_KeyPressed;
22+
window.KeyPressed += (key, modifiers) => KeyPressed?.Invoke(key, modifiers);
3623
}
3724

3825
/// <summary>
3926
/// A hot key has been pressed.
4027
/// </summary>
41-
internal event EventHandler<KeyPressedEventArgs>? KeyPressed;
28+
internal event Action<Key, ModifierKeys>? KeyPressed;
4229

4330
public void Dispose()
4431
{
4532
// unregister all the registered hot keys.
4633
for (int i = currentId; i > 0; i--)
4734
{
48-
DllImports.NativeMethods.User32UnregisterHotKey(window.Handle, i);
35+
NativeMethods.User32UnregisterHotKey(window.Handle, i);
4936
}
5037

5138
// dispose the inner native window.
52-
window.KeyPressed -= Window_KeyPressed;
5339
window.Dispose();
5440
}
5541

56-
internal void RegisterHotKey()
42+
internal void RegisterHotKey(string hotKeyString)
5743
{
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);
6648

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);
9550
}
9651

9752
/// <summary>
9853
/// Registers a hot key in the system.
9954
/// </summary>
10055
/// <param name="modifier">The modifiers that are associated with the hot key.</param>
10156
/// <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-
11257
private void RegisterHotKey(uint modifier, Key key)
11358
{
11459
currentId += 1;
@@ -127,7 +72,7 @@ private class Window : NativeWindow, IDisposable
12772
{
12873
private const int WmHotkey = 0x0312;
12974

130-
public event EventHandler<KeyPressedEventArgs>? KeyPressed;
75+
public event Action<Key, ModifierKeys>? KeyPressed;
13176

13277
/// <summary>
13378
/// Overridden to get the notifications.
@@ -139,10 +84,10 @@ protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lP
13984
{
14085
// get the keys.
14186
Key key = (Key)(((int)lParam >> 16) & 0xFFFF);
142-
KeyboardHookModifierKeys modifier = (KeyboardHookModifierKeys)((int)lParam & 0xFFFF);
87+
ModifierKeys modifiers = (ModifierKeys)((int)lParam & 0xFFFF);
14388

14489
// invoke the event to notify the parent.
145-
KeyPressed?.Invoke(this, new KeyPressedEventArgs(modifier, key));
90+
KeyPressed?.Invoke(key, modifiers);
14691
}
14792

14893
handled = false;

NativeDllImport/HotKey.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// <copyright file="HotKey.cs" company="PlaceholderCompany">
2-
// Copyright (c) PlaceholderCompany. All rights reserved.
3-
// </copyright>
4-
5-
namespace SystemTrayMenu.DllImports
6-
{
7-
using System;
1+
// <copyright file="HotKey.cs" company="PlaceholderCompany">
2+
// Copyright (c) PlaceholderCompany. All rights reserved.
3+
// </copyright>
4+
5+
namespace SystemTrayMenu.DllImports
6+
{
7+
using System;
88
using System.Runtime.InteropServices;
99
using System.Runtime.Versioning;
10-
using System.Text;
11-
12-
/// <summary>
13-
/// wraps the methodcalls to native windows dll's.
14-
/// </summary>
15-
public static partial class NativeMethods
10+
using System.Text;
11+
12+
/// <summary>
13+
/// wraps the methodcalls to native windows dll's.
14+
/// </summary>
15+
public static partial class NativeMethods
1616
{
1717
internal static string GetLastErrorHint()
1818
{
@@ -26,26 +26,26 @@ internal static string GetLastErrorHint()
2626
};
2727
}
2828

29-
[SupportedOSPlatform("windows")]
30-
[DllImport("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
31-
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
32-
[return: MarshalAs(UnmanagedType.Bool)]
29+
[SupportedOSPlatform("windows")]
30+
[DllImport("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
31+
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
32+
[return: MarshalAs(UnmanagedType.Bool)]
3333
internal static extern bool User32RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKeyCode);
3434

35-
[SupportedOSPlatform("windows")]
36-
[DllImport("user32.dll", EntryPoint = "UnregisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
37-
[return: MarshalAs(UnmanagedType.Bool)]
38-
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
35+
[SupportedOSPlatform("windows")]
36+
[DllImport("user32.dll", EntryPoint = "UnregisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
37+
[return: MarshalAs(UnmanagedType.Bool)]
38+
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
3939
internal static extern bool User32UnregisterHotKey(IntPtr hWnd, int id);
4040

41-
[SupportedOSPlatform("windows")]
42-
[DllImport("user32.dll", EntryPoint = "MapVirtualKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
43-
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
41+
[SupportedOSPlatform("windows")]
42+
[DllImport("user32.dll", EntryPoint = "MapVirtualKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
43+
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
4444
internal static extern uint User32MapVirtualKey(uint uCode, uint uMapType);
4545

46-
[SupportedOSPlatform("windows")]
47-
[DllImport("user32.dll", EntryPoint = "GetKeyNameText", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
48-
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
49-
internal static extern int User32GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize);
50-
}
51-
}
46+
[SupportedOSPlatform("windows")]
47+
[DllImport("user32.dll", EntryPoint = "GetKeyNameText", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
48+
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
49+
internal static extern int User32GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize);
50+
}
51+
}

Utilities/SingleAppInstance.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ internal static bool Initialize()
2424
{
2525
if (Properties.Settings.Default.SendHotkeyInsteadKillOtherInstances)
2626
{
27-
ModifierKeys modifiers = HotkeyControl.HotkeyModifiersFromString(Properties.Settings.Default.HotKey);
28-
Key hotkey = HotkeyControl.HotkeyFromString(Properties.Settings.Default.HotKey);
27+
string hotKeyString = Properties.Settings.Default.HotKey;
28+
ModifierKeys modifiers = HotkeyControl.HotkeyModifiersFromString(hotKeyString);
29+
Key hotkey = HotkeyControl.HotkeyFromString(hotKeyString);
2930

3031
try
3132
{
@@ -62,7 +63,7 @@ internal static bool Initialize()
6263
}
6364
catch (Exception ex)
6465
{
65-
Log.Warn($"Send hoktey {Properties.Settings.Default.HotKey} to other instance failed", ex);
66+
Log.Warn($"Send hoktey {hotKeyString} to other instance failed", ex);
6667
}
6768
}
6869
else

0 commit comments

Comments
 (0)