2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="KeyboardHook.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Helper
|
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Windows.Forms;
|
2020-07-08 03:05:19 +12:00
|
|
|
|
using SystemTrayMenu.UserInterface.HotkeyTextboxControl;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using SystemTrayMenu.Utilities;
|
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The enumeration of possible modifiers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Flags]
|
|
|
|
|
internal enum KeyboardHookModifierKeys : uint
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
None = 0,
|
|
|
|
|
Alt = 1,
|
|
|
|
|
Control = 2,
|
|
|
|
|
Shift = 4,
|
|
|
|
|
Win = 8,
|
|
|
|
|
}
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
public sealed class KeyboardHook : IDisposable
|
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
private readonly Window window = new();
|
2020-07-07 09:37:55 +12:00
|
|
|
|
private int currentId;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
|
|
|
|
public KeyboardHook()
|
|
|
|
|
{
|
|
|
|
|
// register the event of the inner native window.
|
2020-07-07 09:37:55 +12:00
|
|
|
|
window.KeyPressed += (sender, args) =>
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
KeyPressed?.Invoke(this, args);
|
2019-07-05 05:04:14 +12:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A hot key has been pressed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal event EventHandler<KeyPressedEventArgs> KeyPressed;
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
// unregister all the registered hot keys.
|
|
|
|
|
for (int i = currentId; i > 0; i--)
|
|
|
|
|
{
|
|
|
|
|
DllImports.NativeMethods.User32UnregisterHotKey(window.Handle, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// dispose the inner native window.
|
|
|
|
|
window.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 23:22:47 +12:00
|
|
|
|
/// <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(Keys key)
|
|
|
|
|
{
|
|
|
|
|
uint keyModifiersNone = 0;
|
|
|
|
|
RegisterHotKey(keyModifiersNone, key);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 08:02:16 +12:00
|
|
|
|
internal void RegisterHotKey()
|
2020-05-05 05:43:47 +12:00
|
|
|
|
{
|
|
|
|
|
KeyboardHookModifierKeys modifiers = KeyboardHookModifierKeys.None;
|
|
|
|
|
string modifiersString = Properties.Settings.Default.HotKey;
|
|
|
|
|
if (!string.IsNullOrEmpty(modifiersString))
|
|
|
|
|
{
|
2020-07-04 08:02:16 +12:00
|
|
|
|
if (modifiersString.ToUpperInvariant().Contains("ALT", StringComparison.InvariantCulture))
|
2020-05-05 05:43:47 +12:00
|
|
|
|
{
|
|
|
|
|
modifiers |= KeyboardHookModifierKeys.Alt;
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-07-07 02:46:47 +12:00
|
|
|
|
if (modifiersString.ToUpperInvariant().Contains("CTRL", StringComparison.InvariantCulture) ||
|
|
|
|
|
modifiersString.ToUpperInvariant().Contains("STRG", StringComparison.InvariantCulture))
|
2020-05-05 05:43:47 +12:00
|
|
|
|
{
|
|
|
|
|
modifiers |= KeyboardHookModifierKeys.Control;
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-07-04 08:02:16 +12:00
|
|
|
|
if (modifiersString.ToUpperInvariant().Contains("SHIFT", StringComparison.InvariantCulture))
|
2020-05-05 05:43:47 +12:00
|
|
|
|
{
|
|
|
|
|
modifiers |= KeyboardHookModifierKeys.Shift;
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-07-04 08:02:16 +12:00
|
|
|
|
if (modifiersString.ToUpperInvariant().Contains("WIN", StringComparison.InvariantCulture))
|
2020-05-05 05:43:47 +12:00
|
|
|
|
{
|
|
|
|
|
modifiers |= KeyboardHookModifierKeys.Win;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
RegisterHotKey(
|
|
|
|
|
modifiers,
|
2020-05-05 05:43:47 +12:00
|
|
|
|
HotkeyControl.HotkeyFromString(
|
|
|
|
|
Properties.Settings.Default.HotKey));
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 05:04:14 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a hot key in the system.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <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>
|
2020-03-30 00:20:18 +13:00
|
|
|
|
internal void RegisterHotKey(KeyboardHookModifierKeys modifier, Keys key)
|
2020-04-30 23:22:47 +12:00
|
|
|
|
{
|
|
|
|
|
RegisterHotKey((uint)modifier, key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RegisterHotKey(uint modifier, Keys key)
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
currentId += 1;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-04-30 23:22:47 +12:00
|
|
|
|
if (!DllImports.NativeMethods.User32RegisterHotKey(
|
2020-07-07 09:37:55 +12:00
|
|
|
|
window.Handle, currentId, modifier, (uint)key))
|
2020-03-24 00:24:16 +13:00
|
|
|
|
{
|
2020-04-30 23:22:47 +12:00
|
|
|
|
throw new InvalidOperationException(
|
2020-05-05 05:43:47 +12:00
|
|
|
|
Translator.GetText("Could not register the hot key."));
|
2020-03-24 00:24:16 +13:00
|
|
|
|
}
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-07-07 09:37:55 +12:00
|
|
|
|
/// Represents the window that is used internally to get the messages.
|
2019-07-05 05:04:14 +12:00
|
|
|
|
/// </summary>
|
2020-07-07 09:37:55 +12:00
|
|
|
|
private class Window : NativeWindow, IDisposable
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
private const int WmHotkey = 0x0312;
|
|
|
|
|
|
|
|
|
|
public Window()
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-07-07 09:37:55 +12:00
|
|
|
|
// create the handle for the window.
|
|
|
|
|
CreateHandle(new CreateParams());
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
public event EventHandler<KeyPressedEventArgs> KeyPressed;
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
DestroyHandle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Overridden to get the notifications.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="m">m.</param>
|
|
|
|
|
protected override void WndProc(ref Message m)
|
|
|
|
|
{
|
|
|
|
|
base.WndProc(ref m);
|
|
|
|
|
|
|
|
|
|
// check if we got a hot key pressed.
|
|
|
|
|
if (m.Msg == WmHotkey)
|
|
|
|
|
{
|
|
|
|
|
// get the keys.
|
|
|
|
|
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
|
|
|
|
|
KeyboardHookModifierKeys modifier = (KeyboardHookModifierKeys)((int)m.LParam & 0xFFFF);
|
|
|
|
|
|
|
|
|
|
// invoke the event to notify the parent.
|
|
|
|
|
KeyPressed?.Invoke(this, new KeyPressedEventArgs(modifier, key));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|