diff --git a/Business/KeyboardInput.cs b/Business/KeyboardInput.cs
index 89f1d99..bdea183 100644
--- a/Business/KeyboardInput.cs
+++ b/Business/KeyboardInput.cs
@@ -30,18 +30,18 @@ namespace SystemTrayMenu.Handler
hook.Dispose();
}
- internal bool RegisterHotKey(string hotKey)
+ internal bool RegisterHotKey(string hotKeyString)
{
- if (!string.IsNullOrEmpty(hotKey))
+ if (!string.IsNullOrEmpty(hotKeyString))
{
try
{
- hook.RegisterHotKey();
- hook.KeyPressed += (sender, e) => HotKeyPressed?.Invoke();
+ hook.RegisterHotKey(hotKeyString);
+ hook.KeyPressed += (_, _) => HotKeyPressed?.Invoke();
}
catch (InvalidOperationException ex)
{
- Log.Warn($"Hotkey cannot be set: '{hotKey}'", ex);
+ Log.Warn($"Hotkey cannot be set: '{hotKeyString}'", ex);
return false;
}
}
diff --git a/Helpers/KeyPressedEventArgs.cs b/Helpers/KeyPressedEventArgs.cs
deleted file mode 100644
index 8534f0e..0000000
--- a/Helpers/KeyPressedEventArgs.cs
+++ /dev/null
@@ -1,27 +0,0 @@
-//
-// Copyright (c) PlaceholderCompany. All rights reserved.
-//
-
-namespace SystemTrayMenu.Helpers
-{
- using System;
- using System.Windows.Input;
-
- ///
- /// Event Args for the event that is fired after the hot key has been pressed.
- ///
- 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;
- }
-}
diff --git a/Helpers/KeyboardHook.cs b/Helpers/KeyboardHook.cs
index dedf5c2..d10bd52 100644
--- a/Helpers/KeyboardHook.cs
+++ b/Helpers/KeyboardHook.cs
@@ -11,19 +11,6 @@ namespace SystemTrayMenu.Helpers
using SystemTrayMenu.Utilities;
using static SystemTrayMenu.Utilities.FormsExtensions;
- ///
- /// The enumeration of possible modifiers.
- ///
- [Flags]
- public enum KeyboardHookModifierKeys : uint
- {
- None = 0,
- Alt = 1,
- Control = 2,
- Shift = 4,
- Win = 8,
- }
-
public sealed class KeyboardHook : IDisposable
{
private readonly Window window = new();
@@ -32,66 +19,34 @@ namespace SystemTrayMenu.Helpers
public KeyboardHook()
{
// register the event of the inner native window.
- window.KeyPressed += Window_KeyPressed;
+ window.KeyPressed += (key, modifiers) => KeyPressed?.Invoke(key, modifiers);
}
///
/// A hot key has been pressed.
///
- internal event EventHandler? KeyPressed;
+ internal event Action? KeyPressed;
public void Dispose()
{
// unregister all the registered hot keys.
for (int i = currentId; i > 0; i--)
{
- DllImports.NativeMethods.User32UnregisterHotKey(window.Handle, i);
+ NativeMethods.User32UnregisterHotKey(window.Handle, i);
}
// dispose the inner native window.
- window.KeyPressed -= Window_KeyPressed;
window.Dispose();
}
- internal void RegisterHotKey()
+ internal void RegisterHotKey(string hotKeyString)
{
- KeyboardHookModifierKeys modifiers = KeyboardHookModifierKeys.None;
- string modifiersString = Properties.Settings.Default.HotKey;
- if (!string.IsNullOrEmpty(modifiersString))
- {
- if (modifiersString.ToUpperInvariant().Contains("ALT", StringComparison.InvariantCulture))
- {
- modifiers |= KeyboardHookModifierKeys.Alt;
- }
+ // TODO: Replace old code of v1 with HotKeyControl methods like here
+ // as there is a bug in the body of this function (missing "+" when checking for modifiers)
+ ModifierKeys modifiers = HotkeyControl.HotkeyModifiersFromString(hotKeyString);
+ Key hotkey = HotkeyControl.HotkeyFromString(hotKeyString);
- if (modifiersString.ToUpperInvariant().Contains("CTRL", StringComparison.InvariantCulture) ||
- 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));
- }
-
- ///
- /// Registers a hot key in the system.
- ///
- /// The key itself that is associated with the hot key.
- internal void RegisterHotKey(Key key)
- {
- uint keyModifiersNone = 0;
- RegisterHotKey(keyModifiersNone, key);
+ RegisterHotKey((uint)modifiers, hotkey);
}
///
@@ -99,16 +54,6 @@ namespace SystemTrayMenu.Helpers
///
/// The modifiers that are associated with the hot key.
/// The key itself that is associated with the hot key.
- 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)
{
currentId += 1;
@@ -127,7 +72,7 @@ namespace SystemTrayMenu.Helpers
{
private const int WmHotkey = 0x0312;
- public event EventHandler? KeyPressed;
+ public event Action? KeyPressed;
///
/// Overridden to get the notifications.
@@ -139,10 +84,10 @@ namespace SystemTrayMenu.Helpers
{
// get the keys.
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.
- KeyPressed?.Invoke(this, new KeyPressedEventArgs(modifier, key));
+ KeyPressed?.Invoke(key, modifiers);
}
handled = false;
diff --git a/NativeDllImport/HotKey.cs b/NativeDllImport/HotKey.cs
index 6a6ccca..96fbf58 100644
--- a/NativeDllImport/HotKey.cs
+++ b/NativeDllImport/HotKey.cs
@@ -1,18 +1,18 @@
-//
-// Copyright (c) PlaceholderCompany. All rights reserved.
-//
-
-namespace SystemTrayMenu.DllImports
-{
- using System;
+//
+// Copyright (c) PlaceholderCompany. All rights reserved.
+//
+
+namespace SystemTrayMenu.DllImports
+{
+ using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
- using System.Text;
-
- ///
- /// wraps the methodcalls to native windows dll's.
- ///
- public static partial class NativeMethods
+ using System.Text;
+
+ ///
+ /// wraps the methodcalls to native windows dll's.
+ ///
+ public static partial class NativeMethods
{
internal static string GetLastErrorHint()
{
@@ -26,26 +26,26 @@ namespace SystemTrayMenu.DllImports
};
}
- [SupportedOSPlatform("windows")]
- [DllImport("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
- [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
- [return: MarshalAs(UnmanagedType.Bool)]
+ [SupportedOSPlatform("windows")]
+ [DllImport("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
+ [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
+ [return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool User32RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKeyCode);
- [SupportedOSPlatform("windows")]
- [DllImport("user32.dll", EntryPoint = "UnregisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
- [return: MarshalAs(UnmanagedType.Bool)]
- [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
+ [SupportedOSPlatform("windows")]
+ [DllImport("user32.dll", EntryPoint = "UnregisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern bool User32UnregisterHotKey(IntPtr hWnd, int id);
- [SupportedOSPlatform("windows")]
- [DllImport("user32.dll", EntryPoint = "MapVirtualKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
- [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
+ [SupportedOSPlatform("windows")]
+ [DllImport("user32.dll", EntryPoint = "MapVirtualKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
+ [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
internal static extern uint User32MapVirtualKey(uint uCode, uint uMapType);
- [SupportedOSPlatform("windows")]
- [DllImport("user32.dll", EntryPoint = "GetKeyNameText", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
- [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
- internal static extern int User32GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize);
- }
-}
+ [SupportedOSPlatform("windows")]
+ [DllImport("user32.dll", EntryPoint = "GetKeyNameText", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
+ [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
+ internal static extern int User32GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize);
+ }
+}
diff --git a/Utilities/SingleAppInstance.cs b/Utilities/SingleAppInstance.cs
index 2067c66..676ec9f 100644
--- a/Utilities/SingleAppInstance.cs
+++ b/Utilities/SingleAppInstance.cs
@@ -24,8 +24,9 @@ namespace SystemTrayMenu.Utilities
{
if (Properties.Settings.Default.SendHotkeyInsteadKillOtherInstances)
{
- ModifierKeys modifiers = HotkeyControl.HotkeyModifiersFromString(Properties.Settings.Default.HotKey);
- Key hotkey = HotkeyControl.HotkeyFromString(Properties.Settings.Default.HotKey);
+ string hotKeyString = Properties.Settings.Default.HotKey;
+ ModifierKeys modifiers = HotkeyControl.HotkeyModifiersFromString(hotKeyString);
+ Key hotkey = HotkeyControl.HotkeyFromString(hotKeyString);
try
{
@@ -62,7 +63,7 @@ namespace SystemTrayMenu.Utilities
}
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