0
|
1 using System.Collections.Generic;
|
|
2 using System.Windows.Forms;
|
|
3
|
|
4 namespace ShortcutKeyFinder
|
|
5 {
|
|
6 /// <summary>Provides a means to encode and decode hotkeys in the format expected by the Win32 API</summary>
|
|
7 class Hotkey
|
|
8 {
|
|
9 private List<Keys> _modifierKeys = new List<Keys>() { Keys.ShiftKey, Keys.ControlKey, Keys.Menu };
|
|
10 // Windows API values
|
|
11 public ushort HOTKEYF_SHIFT = 0x01 << 8;
|
|
12 public ushort HOTKEYF_CONTROL = 0x02 << 8;
|
|
13 public ushort HOTKEYF_ALT = 0x04 << 8;
|
|
14 public ushort HOTKEYF_EXT = 0x08 << 8;
|
|
15
|
|
16 /// <summary>Raw hotkey value to use in Win32 functions</summary>
|
|
17 public ushort RawHotkey { get; set; }
|
|
18
|
|
19 /// <summary>Shift key state</summary>
|
|
20 public bool Shift
|
|
21 {
|
|
22 get { return (RawHotkey & HOTKEYF_SHIFT) != 0; }
|
|
23 set { if (value) RawHotkey |= HOTKEYF_SHIFT; else RawHotkey &= (ushort)~HOTKEYF_SHIFT; }
|
|
24 }
|
|
25
|
|
26 /// <summary>Control key state</summary>
|
|
27 public bool Control
|
|
28 {
|
|
29 get { return (RawHotkey & HOTKEYF_CONTROL) != 0; }
|
|
30 set { if (value) RawHotkey |= HOTKEYF_CONTROL; else RawHotkey &= (ushort)~HOTKEYF_CONTROL; }
|
|
31 }
|
|
32
|
|
33 /// <summary>Alt key state</summary>
|
|
34 public bool Alt
|
|
35 {
|
|
36 get { return (RawHotkey & HOTKEYF_ALT) != 0; }
|
|
37 set { if (value) RawHotkey |= HOTKEYF_ALT; else RawHotkey &= (ushort)~HOTKEYF_ALT; }
|
|
38 }
|
|
39
|
|
40 /// <summary>Ext key state</summary>
|
|
41 /// <remarks>Probably useless nowadays</remarks>
|
|
42 public bool Ext
|
|
43 {
|
|
44 get { return (RawHotkey & HOTKEYF_EXT) != 0; }
|
|
45 set { if (value) RawHotkey |= HOTKEYF_EXT; else RawHotkey &= (ushort)~HOTKEYF_EXT; }
|
|
46 }
|
|
47
|
|
48 /// <summary>The key code portion of the hotkey data</summary>
|
|
49 /// <remarks>The key code is stored in the lower byte of the hotkey</remarks>
|
|
50 public byte KeyCode
|
|
51 {
|
|
52 get { return (byte)(RawHotkey & 0x00FF); }
|
|
53 set { RawHotkey = (ushort)((RawHotkey & 0xFF00) | value); }
|
|
54 }
|
|
55
|
|
56 /// <summary>Default constructor</summary>
|
|
57 public Hotkey() { }
|
|
58
|
|
59 /// <summary>Constructor with hotkey initializer</summary>
|
|
60 /// <param name="hotkey">Hotkey value for Win32 API calls</param>
|
|
61 public Hotkey(ushort hotkey)
|
|
62 {
|
|
63 RawHotkey = hotkey;
|
|
64 }
|
|
65
|
|
66 /// <summary>Converts the hotkey data into a friendly string</summary>
|
|
67 public override string ToString()
|
|
68 {
|
|
69 return string.Format("{0}{1}{2}{3}{4}",
|
|
70 Control ? "Ctrl+" : "",
|
|
71 Alt ? "Alt+" : "",
|
|
72 Shift ? "Shift+" : "",
|
|
73 Ext ? "Ext+" : "",
|
|
74 _modifierKeys.Contains((Keys)KeyCode) ? "" : new KeysConverter().ConvertToString((int)KeyCode));
|
|
75 }
|
|
76
|
|
77 /// <summary>Checks if two Hotkey objects have the same key combination</summary>
|
|
78 public override bool Equals(object obj)
|
|
79 {
|
|
80 Hotkey hotkey = obj as Hotkey;
|
|
81 return hotkey != null && hotkey.RawHotkey == RawHotkey;
|
|
82 }
|
|
83
|
|
84 public override int GetHashCode()
|
|
85 {
|
|
86 return RawHotkey.GetHashCode();
|
|
87 }
|
|
88 }
|
|
89 }
|