using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using SystemTrayMenu.Utilities; namespace SystemTrayMenu.UserInterface.Controls { /// /// A simple control that allows the user to select pretty much any valid hotkey combination /// See: http://www.codeproject.com/KB/buttons/hotkeycontrol.aspx /// But is modified to fit in Greenshot, and have localized support /// public sealed class HotkeyControl : TextBox { // Delegates for hooking up events. public delegate void HotKeyHandler(); private static readonly EventDelay EventDelay = new EventDelay(TimeSpan.FromMilliseconds(600).Ticks); private static readonly bool IsWindows7OrOlder = Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 1; // Holds the list of hotkeys private static readonly IDictionary KeyHandlers = new Dictionary(); private static int _hotKeyCounter = 1; private const uint WM_HOTKEY = 0x312; private static IntPtr _hotkeyHwnd; [SuppressMessage("ReSharper", "InconsistentNaming")] public enum Modifiers : uint { NONE = 0, ALT = 1, CTRL = 2, SHIFT = 4, WIN = 8, NO_REPEAT = 0x4000 } [SuppressMessage("ReSharper", "InconsistentNaming")] private enum MapType : uint { MAPVK_VK_TO_VSC = 0, //The uCode parameter is a virtual-key code and is translated into a scan code. If it is a virtual-key code that does not distinguish between left- and right-hand keys, the left-hand scan code is returned. If there is no translation, the function returns 0. MAPVK_VSC_TO_VK = 1, //The uCode parameter is a scan code and is translated into a virtual-key code that does not distinguish between left- and right-hand keys. If there is no translation, the function returns 0. MAPVK_VK_TO_CHAR = 2, //The uCode parameter is a virtual-key code and is translated into an unshifted character value in the low order word of the return value. Dead keys (diacritics) are indicated by setting the top bit of the return value. If there is no translation, the function returns 0. MAPVK_VSC_TO_VK_EX = 3, //The uCode parameter is a scan code and is translated into a virtual-key code that distinguishes between left- and right-hand keys. If there is no translation, the function returns 0. MAPVK_VK_TO_VSC_EX = 4 //The uCode parameter is a virtual-key code and is translated into a scan code. If it is a virtual-key code that does not distinguish between left- and right-hand keys, the left-hand scan code is returned. If the scan code is an extended scan code, the high byte of the uCode value can contain either 0xe0 or 0xe1 to specify the extended scan code. If there is no translation, the function returns 0. } [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKeyCode); [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); [DllImport("user32.dll", SetLastError = true)] private static extern uint MapVirtualKey(uint uCode, uint uMapType); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] private static extern int GetKeyNameText(uint lParam, [Out] StringBuilder lpString, int nSize); // These variables store the current hotkey and modifier(s) private Keys _hotkey = Keys.None; private Keys _modifiers = Keys.None; // ArrayLists used to enforce the use of proper modifiers. // Shift+A isn't a valid hotkey, for instance, as it would screw up when the user is typing. private readonly IList _needNonShiftModifier = new List(); private readonly IList _needNonAltGrModifier = new List(); private readonly ContextMenuStrip _dummy = new ContextMenuStrip(); /// /// Used to make sure that there is no right-click menu available /// public override ContextMenuStrip ContextMenuStrip { get => _dummy; set => base.ContextMenuStrip = _dummy; } /// /// Forces the control to be non-multiline /// public override bool Multiline { get => base.Multiline; set => // Ignore what the user wants; force Multiline to false base.Multiline = false; } /// /// Creates a new HotkeyControl /// public HotkeyControl() { ContextMenuStrip = _dummy; // Disable right-clicking Text = string.Empty; // Handle events that occurs when keys are pressed KeyPress += HotkeyControl_KeyPress; KeyUp += HotkeyControl_KeyUp; KeyDown += HotkeyControl_KeyDown; PopulateModifierLists(); } /// /// Populates the ArrayLists specifying disallowed hotkeys /// such as Shift+A, Ctrl+Alt+4 (would produce a dollar sign) etc /// private void PopulateModifierLists() { // Shift + 0 - 9, A - Z for (Keys k = Keys.D0; k <= Keys.Z; k++) { _needNonShiftModifier.Add((int)k); } // Shift + Numpad keys for (Keys k = Keys.NumPad0; k <= Keys.NumPad9; k++) { _needNonShiftModifier.Add((int)k); } // Shift + Misc (,;<./ etc) for (Keys k = Keys.Oem1; k <= Keys.OemBackslash; k++) { _needNonShiftModifier.Add((int)k); } // Shift + Space, PgUp, PgDn, End, Home for (Keys k = Keys.Space; k <= Keys.Home; k++) { _needNonShiftModifier.Add((int)k); } // Misc keys that we can't loop through _needNonShiftModifier.Add((int)Keys.Insert); _needNonShiftModifier.Add((int)Keys.Help); _needNonShiftModifier.Add((int)Keys.Multiply); _needNonShiftModifier.Add((int)Keys.Add); _needNonShiftModifier.Add((int)Keys.Subtract); _needNonShiftModifier.Add((int)Keys.Divide); _needNonShiftModifier.Add((int)Keys.Decimal); _needNonShiftModifier.Add((int)Keys.Return); _needNonShiftModifier.Add((int)Keys.Escape); _needNonShiftModifier.Add((int)Keys.NumLock); // Ctrl+Alt + 0 - 9 for (Keys k = Keys.D0; k <= Keys.D9; k++) { _needNonAltGrModifier.Add((int)k); } } /// /// Resets this hotkey control to None /// public new void Clear() { Hotkey = Keys.None; HotkeyModifiers = Keys.None; } /// /// Fires when a key is pushed down. Here, we'll want to update the text in the box /// to notify the user what combination is currently pressed. /// private void HotkeyControl_KeyDown(object sender, KeyEventArgs e) { // Clear the current hotkey if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete) { ResetHotkey(); } else { _modifiers = e.Modifiers; _hotkey = e.KeyCode; Redraw(); } } /// /// Fires when all keys are released. If the current hotkey isn't valid, reset it. /// Otherwise, do nothing and keep the text and hotkey as it was. /// private void HotkeyControl_KeyUp(object sender, KeyEventArgs e) { // Somehow the PrintScreen only comes as a keyup, therefore we handle it here. if (e.KeyCode == Keys.PrintScreen) { _modifiers = e.Modifiers; _hotkey = e.KeyCode; Redraw(); } if (_hotkey == Keys.None && ModifierKeys == Keys.None) { ResetHotkey(); } } /// /// Prevents the letter/whatever entered to show up in the TextBox /// Without this, a "A" key press would appear as "aControl, Alt + A" /// private void HotkeyControl_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = true; } /// /// Handles some misc keys, such as Ctrl+Delete and Shift+Insert /// protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Delete || keyData == (Keys.Control | Keys.Delete)) { ResetHotkey(); return true; } // Paste if (keyData == (Keys.Shift | Keys.Insert)) { return true; // Don't allow } // Allow the rest return base.ProcessCmdKey(ref msg, keyData); } /// /// Clears the current hotkey and resets the TextBox /// public void ResetHotkey() { _hotkey = Keys.None; _modifiers = Keys.None; Redraw(); } /// /// Used to get/set the hotkey (e.g. Keys.A) /// public Keys Hotkey { get => _hotkey; set { _hotkey = value; Redraw(true); } } /// /// Used to get/set the hotkey (e.g. Keys.A) /// public void SetHotkey(string hotkey) { _hotkey = HotkeyFromString(hotkey); _modifiers = HotkeyModifiersFromString(hotkey); Redraw(true); } /// /// Used to get/set the modifier keys (e.g. Keys.Alt | Keys.Control) /// public Keys HotkeyModifiers { get => _modifiers; set { _modifiers = value; Redraw(true); } } /// /// Redraws the TextBox when necessary. /// /// Specifies whether this function was called by the Hotkey/HotkeyModifiers properties or by the user. private void Redraw(bool bCalledProgramatically = false) { // No hotkey set if (_hotkey == Keys.None) { Text = ""; return; } // LWin/RWin doesn't work as hotkeys (neither do they work as modifier keys in .NET 2.0) if (_hotkey == Keys.LWin || _hotkey == Keys.RWin) { Text = ""; return; } // Only validate input if it comes from the user if (bCalledProgramatically == false) { // No modifier or shift only, AND a hotkey that needs another modifier if ((_modifiers == Keys.Shift || _modifiers == Keys.None) && _needNonShiftModifier.Contains((int)_hotkey)) { if (_modifiers == Keys.None) { // Set Ctrl+Alt as the modifier unless Ctrl+Alt+ won't work... if (_needNonAltGrModifier.Contains((int)_hotkey) == false) { _modifiers = Keys.Alt | Keys.Control; } else { // ... in that case, use Shift+Alt instead. _modifiers = Keys.Alt | Keys.Shift; } } else { // User pressed Shift and an invalid key (e.g. a letter or a number), // that needs another set of modifier keys _hotkey = Keys.None; Text = ""; return; } } // Check all Ctrl+Alt keys if ((_modifiers == (Keys.Alt | Keys.Control)) && _needNonAltGrModifier.Contains((int)_hotkey)) { // Ctrl+Alt+4 etc won't work; reset hotkey and tell the user _hotkey = Keys.None; Text = ""; return; } } // I have no idea why this is needed, but it is. Without this code, pressing only Ctrl // will show up as "Control + ControlKey", etc. if (_hotkey == Keys.Menu /* Alt */ || _hotkey == Keys.ShiftKey || _hotkey == Keys.ControlKey) { _hotkey = Keys.None; } Text = HotkeyToLocalizedString(_modifiers, _hotkey); } public override string ToString() { return HotkeyToString(HotkeyModifiers, Hotkey); } public static string GetLocalizedHotkeyStringFromString(string hotkeyString) { Keys virtualKeyCode = HotkeyFromString(hotkeyString); Keys modifiers = HotkeyModifiersFromString(hotkeyString); return HotkeyToLocalizedString(modifiers, virtualKeyCode); } public static string HotkeyToString(Keys modifierKeyCode, Keys virtualKeyCode) { return HotkeyModifiersToString(modifierKeyCode) + virtualKeyCode; } public static string HotkeyModifiersToString(Keys modifierKeyCode) { StringBuilder hotkeyString = new StringBuilder(); if ((modifierKeyCode & Keys.Alt) > 0) { hotkeyString.Append("Alt").Append(" + "); } if ((modifierKeyCode & Keys.Control) > 0) { hotkeyString.Append("Ctrl").Append(" + "); } if ((modifierKeyCode & Keys.Shift) > 0) { hotkeyString.Append("Shift").Append(" + "); } if (modifierKeyCode == Keys.LWin || modifierKeyCode == Keys.RWin) { hotkeyString.Append("Win").Append(" + "); } return hotkeyString.ToString(); } public static string HotkeyToLocalizedString(Keys modifierKeyCode, Keys virtualKeyCode) { return HotkeyModifiersToLocalizedString(modifierKeyCode) + GetKeyName(virtualKeyCode); } public static string HotkeyModifiersToLocalizedString(Keys modifierKeyCode) { StringBuilder hotkeyString = new StringBuilder(); if ((modifierKeyCode & Keys.Alt) > 0) { hotkeyString.Append(GetKeyName(Keys.Alt)).Append(" + "); } if ((modifierKeyCode & Keys.Control) > 0) { hotkeyString.Append(GetKeyName(Keys.Control)).Append(" + "); } if ((modifierKeyCode & Keys.Shift) > 0) { hotkeyString.Append(GetKeyName(Keys.Shift)).Append(" + "); } if (modifierKeyCode == Keys.LWin || modifierKeyCode == Keys.RWin) { hotkeyString.Append("Win").Append(" + "); } return hotkeyString.ToString(); } public static Keys HotkeyModifiersFromString(string modifiersString) { Keys modifiers = Keys.None; if (!string.IsNullOrEmpty(modifiersString)) { if (modifiersString.ToUpperInvariant().Contains("ALT", StringComparison.InvariantCulture)) { modifiers |= Keys.Alt; } if (modifiersString.ToUpperInvariant().Contains("CTRL", StringComparison.InvariantCulture)) { modifiers |= Keys.Control; } if (modifiersString.ToUpperInvariant().Contains("SHIFT", StringComparison.InvariantCulture)) { modifiers |= Keys.Shift; } if (modifiersString.ToUpperInvariant().Contains("WIN", StringComparison.InvariantCulture)) { modifiers |= Keys.LWin; } } return modifiers; } public static Keys HotkeyFromString(string hotkey) { Keys key = Keys.None; if (!string.IsNullOrEmpty(hotkey)) { if (hotkey.LastIndexOf('+') > 0) { hotkey = hotkey.Remove(0, hotkey.LastIndexOf('+') + 1).Trim(); } try { hotkey = hotkey. Replace("PgDn", "PageDown", StringComparison.InvariantCulture). Replace("PgUp", "PageUp", StringComparison.InvariantCulture); key = (Keys)Enum.Parse(typeof(Keys), hotkey); } catch (ArgumentException ex) { Log.Warn($"{hotkey} can not be parsed", ex); } } return key; } public static void RegisterHotkeyHwnd(IntPtr hWnd) { _hotkeyHwnd = hWnd; } public static int RegisterHotKey(string hotkey, HotKeyHandler handler) { return RegisterHotKey(HotkeyModifiersFromString(hotkey), HotkeyFromString(hotkey), handler); } /// /// Register a hotkey /// /// The modifier, e.g.: Modifiers.CTRL, Modifiers.NONE or Modifiers.ALT /// The virtual key code /// A HotKeyHandler, this will be called to handle the hotkey press /// the hotkey number, -1 if failed public static int RegisterHotKey(Keys modifierKeyCode, Keys virtualKeyCode, HotKeyHandler handler) { if (virtualKeyCode == Keys.None) { Log.Info("Trying to register a Keys.none hotkey, ignoring"); return 0; } // Convert Modifiers to fit HKM_SETHOTKEY uint modifiers = 0; if ((modifierKeyCode & Keys.Alt) > 0) { modifiers |= (uint)Modifiers.ALT; } if ((modifierKeyCode & Keys.Control) > 0) { modifiers |= (uint)Modifiers.CTRL; } if ((modifierKeyCode & Keys.Shift) > 0) { modifiers |= (uint)Modifiers.SHIFT; } if (modifierKeyCode == Keys.LWin || modifierKeyCode == Keys.RWin) { modifiers |= (uint)Modifiers.WIN; } // Disable repeating hotkey for Windows 7 and beyond, as described in #1559 if (IsWindows7OrOlder) { modifiers |= (uint)Modifiers.NO_REPEAT; } if (RegisterHotKey(_hotkeyHwnd, _hotKeyCounter, modifiers, (uint)virtualKeyCode)) { KeyHandlers.Add(_hotKeyCounter, handler); return _hotKeyCounter++; } else { Log.Info($"Couldn't register hotkey modifier {modifierKeyCode} virtualKeyCode {virtualKeyCode}"); return -1; } } public static void UnregisterHotkeys() { foreach (int hotkey in KeyHandlers.Keys) { UnregisterHotKey(_hotkeyHwnd, hotkey); } // Remove all key handlers KeyHandlers.Clear(); } public static void UnregisterHotkey(int hotkey) { bool removeHotkey = false; foreach (int availableHotkey in KeyHandlers.Keys) { if (availableHotkey == hotkey) { UnregisterHotKey(_hotkeyHwnd, hotkey); removeHotkey = true; } } if (removeHotkey) { // Remove key handler KeyHandlers.Remove(hotkey); } } /// /// Handle WndProc messages for the hotkey /// /// /// true if the message was handled public static bool HandleMessages(ref Message m) { if (m.Msg != WM_HOTKEY) { return false; } // Call handler if (!IsWindows7OrOlder && !EventDelay.Check()) { return true; } if (KeyHandlers.TryGetValue((int)m.WParam, out HotKeyHandler handler)) { handler(); } return true; } public static string GetKeyName(Keys givenKey) { StringBuilder keyName = new StringBuilder(); const uint numpad = 55; Keys virtualKey = givenKey; string keyString; // Make VC's to real keys switch (virtualKey) { case Keys.Alt: virtualKey = Keys.LMenu; break; case Keys.Control: virtualKey = Keys.ControlKey; break; case Keys.Shift: virtualKey = Keys.LShiftKey; break; case Keys.Multiply: GetKeyNameText(numpad << 16, keyName, 100); keyString = keyName.ToString().Replace("*", "").Trim().ToLower(); if (keyString.IndexOf("(", StringComparison.Ordinal) >= 0) { return "* " + keyString; } keyString = keyString.Substring(0, 1).ToUpper() + keyString.Substring(1).ToLower(); return keyString + " *"; case Keys.Divide: GetKeyNameText(numpad << 16, keyName, 100); keyString = keyName.ToString().Replace("*", "").Trim().ToLower(); if (keyString.IndexOf("(", StringComparison.Ordinal) >= 0) { return "/ " + keyString; } keyString = keyString.Substring(0, 1).ToUpper() + keyString.Substring(1).ToLower(); return keyString + " /"; } uint scanCode = MapVirtualKey((uint)virtualKey, (uint)MapType.MAPVK_VK_TO_VSC); // because MapVirtualKey strips the extended bit for some keys switch (virtualKey) { case Keys.Left: case Keys.Up: case Keys.Right: case Keys.Down: // arrow keys case Keys.Prior: case Keys.Next: // page up and page down case Keys.End: case Keys.Home: case Keys.Insert: case Keys.Delete: case Keys.NumLock: //Log.Debug("Modifying Extended bit"); scanCode |= 0x100; // set extended bit break; case Keys.PrintScreen: // PrintScreen scanCode = 311; break; case Keys.Pause: // PrintScreen scanCode = 69; break; } scanCode |= 0x200; if (GetKeyNameText(scanCode << 16, keyName, 100) != 0) { string visibleName = keyName.ToString(); if (visibleName.Length > 1) { visibleName = visibleName.Substring(0, 1) + visibleName.Substring(1).ToLower(); } return visibleName; } else { return givenKey.ToString(); } } } public class EventDelay { private long lastCheck; private readonly long waitTime; public EventDelay(long ticks) { waitTime = ticks; } public bool Check() { lock (this) { long now = DateTime.Now.Ticks; bool isPassed = now - lastCheck > waitTime; lastCheck = now; return isPassed; } } } }