Fix issue that key modifiers are parsed wrong

(registering the hotkey might have failed)
Simplified hot key pressed events
Remove obsolete class KeyPressedEventArgs
This commit is contained in:
Peter Kirmeier 2023-05-22 23:57:54 +02:00
parent f8c9913933
commit f55af05bc3
5 changed files with 51 additions and 132 deletions

View file

@ -30,18 +30,18 @@ namespace SystemTrayMenu.Handler
hook.Dispose(); hook.Dispose();
} }
internal bool RegisterHotKey(string hotKey) internal bool RegisterHotKey(string hotKeyString)
{ {
if (!string.IsNullOrEmpty(hotKey)) if (!string.IsNullOrEmpty(hotKeyString))
{ {
try try
{ {
hook.RegisterHotKey(); hook.RegisterHotKey(hotKeyString);
hook.KeyPressed += (sender, e) => HotKeyPressed?.Invoke(); hook.KeyPressed += (_, _) => HotKeyPressed?.Invoke();
} }
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
{ {
Log.Warn($"Hotkey cannot be set: '{hotKey}'", ex); Log.Warn($"Hotkey cannot be set: '{hotKeyString}'", ex);
return false; return false;
} }
} }

View file

@ -1,27 +0,0 @@
// <copyright file="KeyPressedEventArgs.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Helpers
{
using System;
using System.Windows.Input;
/// <summary>
/// Event Args for the event that is fired after the hot key has been pressed.
/// </summary>
internal class KeyPressedEventArgs : EventArgs
{
private readonly Key key;
internal KeyPressedEventArgs(KeyboardHookModifierKeys modifier, Key key)
{
Modifier = modifier;
this.key = key;
}
internal KeyboardHookModifierKeys Modifier { get; }
internal Key Key => key;
}
}

View file

@ -11,19 +11,6 @@ namespace SystemTrayMenu.Helpers
using SystemTrayMenu.Utilities; using SystemTrayMenu.Utilities;
using static SystemTrayMenu.Utilities.FormsExtensions; using static SystemTrayMenu.Utilities.FormsExtensions;
/// <summary>
/// The enumeration of possible modifiers.
/// </summary>
[Flags]
public enum KeyboardHookModifierKeys : uint
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Win = 8,
}
public sealed class KeyboardHook : IDisposable public sealed class KeyboardHook : IDisposable
{ {
private readonly Window window = new(); private readonly Window window = new();
@ -32,66 +19,34 @@ namespace SystemTrayMenu.Helpers
public KeyboardHook() public KeyboardHook()
{ {
// register the event of the inner native window. // register the event of the inner native window.
window.KeyPressed += Window_KeyPressed; window.KeyPressed += (key, modifiers) => KeyPressed?.Invoke(key, modifiers);
} }
/// <summary> /// <summary>
/// A hot key has been pressed. /// A hot key has been pressed.
/// </summary> /// </summary>
internal event EventHandler<KeyPressedEventArgs>? KeyPressed; internal event Action<Key, ModifierKeys>? KeyPressed;
public void Dispose() public void Dispose()
{ {
// unregister all the registered hot keys. // unregister all the registered hot keys.
for (int i = currentId; i > 0; i--) for (int i = currentId; i > 0; i--)
{ {
DllImports.NativeMethods.User32UnregisterHotKey(window.Handle, i); NativeMethods.User32UnregisterHotKey(window.Handle, i);
} }
// dispose the inner native window. // dispose the inner native window.
window.KeyPressed -= Window_KeyPressed;
window.Dispose(); window.Dispose();
} }
internal void RegisterHotKey() internal void RegisterHotKey(string hotKeyString)
{ {
KeyboardHookModifierKeys modifiers = KeyboardHookModifierKeys.None; // TODO: Replace old code of v1 with HotKeyControl methods like here
string modifiersString = Properties.Settings.Default.HotKey; // as there is a bug in the body of this function (missing "+" when checking for modifiers)
if (!string.IsNullOrEmpty(modifiersString)) ModifierKeys modifiers = HotkeyControl.HotkeyModifiersFromString(hotKeyString);
{ Key hotkey = HotkeyControl.HotkeyFromString(hotKeyString);
if (modifiersString.ToUpperInvariant().Contains("ALT", StringComparison.InvariantCulture))
{
modifiers |= KeyboardHookModifierKeys.Alt;
}
if (modifiersString.ToUpperInvariant().Contains("CTRL", StringComparison.InvariantCulture) || RegisterHotKey((uint)modifiers, hotkey);
modifiersString.ToUpperInvariant().Contains("STRG", StringComparison.InvariantCulture))
{
modifiers |= KeyboardHookModifierKeys.Control;
}
if (modifiersString.ToUpperInvariant().Contains("SHIFT", StringComparison.InvariantCulture))
{
modifiers |= KeyboardHookModifierKeys.Shift;
}
if (modifiersString.ToUpperInvariant().Contains("WIN", StringComparison.InvariantCulture))
{
modifiers |= KeyboardHookModifierKeys.Win;
}
}
RegisterHotKey(modifiers, HotkeyControl.HotkeyFromString(Properties.Settings.Default.HotKey));
}
/// <summary>
/// Registers a hot key in the system.
/// </summary>
/// <param name="key">The key itself that is associated with the hot key.</param>
internal void RegisterHotKey(Key key)
{
uint keyModifiersNone = 0;
RegisterHotKey(keyModifiersNone, key);
} }
/// <summary> /// <summary>
@ -99,16 +54,6 @@ namespace SystemTrayMenu.Helpers
/// </summary> /// </summary>
/// <param name="modifier">The modifiers that are associated with the hot key.</param> /// <param name="modifier">The modifiers that are associated with the hot key.</param>
/// <param name="key">The key itself that is associated with the hot key.</param> /// <param name="key">The key itself that is associated with the hot key.</param>
internal void RegisterHotKey(KeyboardHookModifierKeys modifier, Key key)
{
RegisterHotKey((uint)modifier, key);
}
private void Window_KeyPressed(object? sender, KeyPressedEventArgs e)
{
KeyPressed?.Invoke(this, e);
}
private void RegisterHotKey(uint modifier, Key key) private void RegisterHotKey(uint modifier, Key key)
{ {
currentId += 1; currentId += 1;
@ -127,7 +72,7 @@ namespace SystemTrayMenu.Helpers
{ {
private const int WmHotkey = 0x0312; private const int WmHotkey = 0x0312;
public event EventHandler<KeyPressedEventArgs>? KeyPressed; public event Action<Key, ModifierKeys>? KeyPressed;
/// <summary> /// <summary>
/// Overridden to get the notifications. /// Overridden to get the notifications.
@ -139,10 +84,10 @@ namespace SystemTrayMenu.Helpers
{ {
// get the keys. // get the keys.
Key key = (Key)(((int)lParam >> 16) & 0xFFFF); Key key = (Key)(((int)lParam >> 16) & 0xFFFF);
KeyboardHookModifierKeys modifier = (KeyboardHookModifierKeys)((int)lParam & 0xFFFF); ModifierKeys modifiers = (ModifierKeys)((int)lParam & 0xFFFF);
// invoke the event to notify the parent. // invoke the event to notify the parent.
KeyPressed?.Invoke(this, new KeyPressedEventArgs(modifier, key)); KeyPressed?.Invoke(key, modifiers);
} }
handled = false; handled = false;

View file

@ -1,18 +1,18 @@
// <copyright file="HotKey.cs" company="PlaceholderCompany"> // <copyright file="HotKey.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved. // Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright> // </copyright>
namespace SystemTrayMenu.DllImports namespace SystemTrayMenu.DllImports
{ {
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using System.Text; using System.Text;
/// <summary> /// <summary>
/// wraps the methodcalls to native windows dll's. /// wraps the methodcalls to native windows dll's.
/// </summary> /// </summary>
public static partial class NativeMethods public static partial class NativeMethods
{ {
internal static string GetLastErrorHint() internal static string GetLastErrorHint()
{ {
@ -26,26 +26,26 @@ namespace SystemTrayMenu.DllImports
}; };
} }
[SupportedOSPlatform("windows")] [SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] [DllImport("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool User32RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKeyCode); internal static extern bool User32RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKeyCode);
[SupportedOSPlatform("windows")] [SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "UnregisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] [DllImport("user32.dll", EntryPoint = "UnregisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern bool User32UnregisterHotKey(IntPtr hWnd, int id); internal static extern bool User32UnregisterHotKey(IntPtr hWnd, int id);
[SupportedOSPlatform("windows")] [SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "MapVirtualKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] [DllImport("user32.dll", EntryPoint = "MapVirtualKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern uint User32MapVirtualKey(uint uCode, uint uMapType); internal static extern uint User32MapVirtualKey(uint uCode, uint uMapType);
[SupportedOSPlatform("windows")] [SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "GetKeyNameText", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] [DllImport("user32.dll", EntryPoint = "GetKeyNameText", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)] [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern int User32GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize); internal static extern int User32GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize);
} }
} }

View file

@ -24,8 +24,9 @@ namespace SystemTrayMenu.Utilities
{ {
if (Properties.Settings.Default.SendHotkeyInsteadKillOtherInstances) if (Properties.Settings.Default.SendHotkeyInsteadKillOtherInstances)
{ {
ModifierKeys modifiers = HotkeyControl.HotkeyModifiersFromString(Properties.Settings.Default.HotKey); string hotKeyString = Properties.Settings.Default.HotKey;
Key hotkey = HotkeyControl.HotkeyFromString(Properties.Settings.Default.HotKey); ModifierKeys modifiers = HotkeyControl.HotkeyModifiersFromString(hotKeyString);
Key hotkey = HotkeyControl.HotkeyFromString(hotKeyString);
try try
{ {
@ -62,7 +63,7 @@ namespace SystemTrayMenu.Utilities
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Warn($"Send hoktey {Properties.Settings.Default.HotKey} to other instance failed", ex); Log.Warn($"Send hoktey {hotKeyString} to other instance failed", ex);
} }
} }
else else