diff ShortcutKeyFinder/Hotkey.cs @ 0:209d9210c18f default tip

It works.
author Brad Greco <brad@bgreco.net>
date Sat, 25 Jun 2016 13:42:54 +1000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ShortcutKeyFinder/Hotkey.cs	Sat Jun 25 13:42:54 2016 +1000
@@ -0,0 +1,89 @@
+using System.Collections.Generic;
+using System.Windows.Forms;
+
+namespace ShortcutKeyFinder
+{
+    /// <summary>Provides a means to encode and decode hotkeys in the format expected by the Win32 API</summary>
+    class Hotkey
+    {
+        private List<Keys> _modifierKeys = new List<Keys>() { Keys.ShiftKey, Keys.ControlKey, Keys.Menu };
+        // Windows API values
+        public ushort HOTKEYF_SHIFT   = 0x01 << 8;
+        public ushort HOTKEYF_CONTROL = 0x02 << 8;
+        public ushort HOTKEYF_ALT     = 0x04 << 8;
+        public ushort HOTKEYF_EXT     = 0x08 << 8;
+
+        /// <summary>Raw hotkey value to use in Win32 functions</summary>
+        public ushort RawHotkey { get; set; }
+
+        /// <summary>Shift key state</summary>
+        public bool Shift
+        {
+            get { return (RawHotkey & HOTKEYF_SHIFT) != 0; }
+            set { if (value) RawHotkey |= HOTKEYF_SHIFT; else RawHotkey &= (ushort)~HOTKEYF_SHIFT; }
+        }
+
+        /// <summary>Control key state</summary>
+        public bool Control
+        {
+            get { return (RawHotkey & HOTKEYF_CONTROL) != 0; }
+            set { if (value) RawHotkey |= HOTKEYF_CONTROL; else RawHotkey &= (ushort)~HOTKEYF_CONTROL; }
+        }
+
+        /// <summary>Alt key state</summary>
+        public bool Alt
+        {
+            get { return (RawHotkey & HOTKEYF_ALT) != 0; }
+            set { if (value) RawHotkey |= HOTKEYF_ALT; else RawHotkey &= (ushort)~HOTKEYF_ALT; }
+        }
+
+        /// <summary>Ext key state</summary>
+        /// <remarks>Probably useless nowadays</remarks>
+        public bool Ext
+        {
+            get { return (RawHotkey & HOTKEYF_EXT) != 0; }
+            set { if (value) RawHotkey |= HOTKEYF_EXT; else RawHotkey &= (ushort)~HOTKEYF_EXT; }
+        }
+
+        /// <summary>The key code portion of the hotkey data</summary>
+        /// <remarks>The key code is stored in the lower byte of the hotkey</remarks>
+        public byte KeyCode
+        {
+            get { return (byte)(RawHotkey & 0x00FF); }
+            set { RawHotkey = (ushort)((RawHotkey & 0xFF00) | value); }
+        }
+
+        /// <summary>Default constructor</summary>
+        public Hotkey() { }
+
+        /// <summary>Constructor with hotkey initializer</summary>
+        /// <param name="hotkey">Hotkey value for Win32 API calls</param>
+        public Hotkey(ushort hotkey)
+        {
+            RawHotkey = hotkey;
+        }
+
+        /// <summary>Converts the hotkey data into a friendly string</summary>
+        public override string ToString()
+        {
+            return string.Format("{0}{1}{2}{3}{4}",
+                Control ? "Ctrl+" : "",
+                Alt ? "Alt+" : "",
+                Shift ? "Shift+" : "",
+                Ext ? "Ext+" : "",
+                _modifierKeys.Contains((Keys)KeyCode) ? "" : new KeysConverter().ConvertToString((int)KeyCode));
+        }
+
+        /// <summary>Checks if two Hotkey objects have the same key combination</summary>
+        public override bool Equals(object obj)
+        {
+            Hotkey hotkey = obj as Hotkey;
+            return hotkey != null && hotkey.RawHotkey == RawHotkey;
+        }
+
+        public override int GetHashCode()
+        {
+            return RawHotkey.GetHashCode();
+        }
+    }
+}