forked from canneverbe/Ketarin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hotkey.cs
159 lines (137 loc) · 5.77 KB
/
Hotkey.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CDBurnerXP;
namespace Ketarin
{
/// <summary>
/// Represents an action, that can be invoked through a keyboard shortcut.
/// </summary>
internal class Hotkey
{
public static readonly Keys[] EndKeys = { Keys.A, Keys.B, Keys.C, Keys.D, Keys.E, Keys.F, Keys.G, Keys.H, Keys.I, Keys.J, Keys.K, Keys.L, Keys.M, Keys.N, Keys.O, Keys.P, Keys.Q, Keys.R, Keys.S, Keys.T, Keys.U, Keys.V, Keys.W, Keys.X, Keys.Y, Keys.Z, Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9, Keys.D0, Keys.F1, Keys.F2, Keys.F3, Keys.F4, Keys.F5, Keys.F6, Keys.F7, Keys.F8, Keys.F9, Keys.F10, Keys.F11, Keys.F12 };
#region Properties
/// <summary>
/// Gets the human readable representation of the key combination required
/// to invoke this hotkey.
/// </summary>
public string Shortcut { get; private set; }
/// <summary>
/// Gets or sets the name of the hotkey action.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the display name of the hotkey action.
/// </summary>
public string DisplayName { get; set; }
#endregion
/// <summary>
/// Creates a new instance of the hotkey class.
/// </summary>
/// <param name="name">Name of the hotkey</param>
/// <param name="displayName">Display name of the hotkey</param>
public Hotkey(string name, string displayName)
{
this.Name = name;
this.DisplayName = displayName;
}
/// <summary>
/// Gets the human readable representation of the given keys.
/// </summary>
/// <param name="keys">Pressed keys</param>
/// <returns>Keys separated by plus sign</returns>
public static string GetShortcutString(Keys keys)
{
if (keys == Keys.None)
{
return string.Empty;
}
List<string> singleKeys = new List<string>();
if ((keys & Keys.Control) == Keys.Control)
{
singleKeys.Add("Control");
keys = keys ^ Keys.Control;
}
if ((keys & Keys.Alt) == Keys.Alt)
{
singleKeys.Add("Alt");
keys = keys ^ Keys.Alt;
}
if ((keys & Keys.Shift) == Keys.Shift)
{
singleKeys.Add("Shift");
keys = keys ^ Keys.Shift;
}
singleKeys.AddRange(from key in EndKeys where keys == key select new KeysConverter().ConvertTo(key, typeof (string)) as string);
return String.Join("+", singleKeys.ToArray());
}
/// <summary>
/// Gets the human readable representation of the given keys + double click action.
/// </summary>
/// <param name="keys">Pressed keys</param>
/// <returns>Keys separated by plus sign</returns>
private string GetDoubleClickShortcutString(Keys keys)
{
return (GetShortcutString(keys) + "+Double-click").TrimStart('+');
}
/// <summary>
/// Sets the key combination for this hotkey.
/// </summary>
/// <param name="keys">Keys required to press</param>
public void SetKeyShortcut(Keys keys)
{
this.Shortcut = GetShortcutString(keys);
}
/// <summary>
/// Sets the key/doubleclick combination for this hotkey.
/// </summary>
/// <param name="keys">Keys to hold, when executing a double click</param>
public void SetDoubleclickShortcut(Keys keys)
{
this.Shortcut = this.GetDoubleClickShortcutString(keys);
}
/// <summary>
/// Gets a list of all available hotkeys.
/// </summary>
/// <returns></returns>
public static List<Hotkey> GetHotkeys()
{
List<Hotkey> hotkeys = new List<Hotkey>
{
new Hotkey("OpenWebsite", "Open website"),
new Hotkey("Edit", "Edit application"),
new Hotkey("Update", "Update application"),
new Hotkey("ForceDownload", "Force download"),
new Hotkey("InstallSelected", "Install selected applications"),
new Hotkey("OpenFile", "Open file"),
new Hotkey("OpenFolder", "Open folder"),
new Hotkey("CheckUpdate", "Check for update"),
new Hotkey("UpdateAndInstall", "Update and install")
};
foreach (Hotkey hotkey in hotkeys)
{
hotkey.Shortcut = Settings.GetValue("Hotkey: " + hotkey.Name, string.Empty) as string;
}
return hotkeys;
}
/// <summary>
/// Determines whether or not the hotkey is a match for the given key combination.
/// </summary>
/// <param name="keys">Keys pressed by user</param>
public bool IsMatch(Keys keys)
{
if (string.IsNullOrEmpty(this.Shortcut)) return false;
return (GetShortcutString(keys) == this.Shortcut);
}
/// <summary>
/// Determines whether or not the hotkey is a match for the given key combination plus double click.
/// </summary>
/// <param name="keys">Keys pressed by user</param>
public bool IsDoubleClickMatch(Keys keys)
{
if (string.IsNullOrEmpty(this.Shortcut)) return false;
return (GetDoubleClickShortcutString(keys) == this.Shortcut);
}
}
}