Mercurial > projects > dynamin
annotate dynamin/gui/key.d @ 103:73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 22:06:02 -0500 |
parents | 63ea570c8d7c |
children | acdbb30fee7e |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
71
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
71
diff
changeset
|
5 * This Source Code Form is subject to the terms of the Mozilla Public |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
71
diff
changeset
|
6 * License, v. 2.0. If a copy of the MPL was not distributed with this |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
71
diff
changeset
|
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.gui.key; | |
12 | |
13 import dynamin.core.string; | |
14 | |
15 /** | |
16 * No Menu key is included because Shift+F10 is used instead. | |
17 * Note: Windows does not send KeyDown events for PrintScreen, only KeyUp. | |
18 */ | |
19 enum Key { | |
20 None, | |
21 Escape, | |
22 Tab, | |
23 Backspace, | |
24 Enter, | |
25 Space, | |
26 | |
27 Left, | |
28 Right, | |
29 Up, | |
30 Down, | |
31 | |
32 Insert, | |
33 Delete, | |
34 Home, | |
35 End, | |
36 PageUp, | |
37 PageDown, | |
38 | |
39 PrintScreen, | |
40 Pause, | |
41 | |
42 CapsLock, | |
43 NumLock, | |
44 ScrollLock, | |
45 | |
46 NumPad0, | |
47 NumPad1, | |
48 NumPad2, | |
49 NumPad3, | |
50 NumPad4, | |
51 NumPad5, | |
52 NumPad6, | |
53 NumPad7, | |
54 NumPad8, | |
55 NumPad9, | |
56 NumPadDivide, | |
57 NumPadMultiply, | |
58 NumPadSubtract, | |
59 NumPadAdd, | |
60 NumPadDecimal, // TODO: NumPadPoint? | |
61 | |
62 Backquote, | |
63 Minus, | |
64 Equals, | |
65 OpenBracket, | |
66 CloseBracket, | |
67 Backslash, | |
68 Semicolon, | |
69 Quote, | |
70 Comma, | |
71 Period, | |
72 Slash, | |
73 | |
74 /// Windows sends these messages when the Menu key is pressed and released: | |
75 /// Menu pressed, Menu released, Shift pressed, F10 pressed, F10 released, Shift released | |
76 /// So if any program responds to either Menu or Shift+F10, it will work right. | |
77 Menu, // right of spacebar, between WinKey and Ctrl | |
78 | |
79 // digits | |
80 D0 = 0x30, D1, D2, D3, D4, D5, D6, D7, D8, D9 = 0x39, | |
81 A = 0x41, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z = 0x5A, | |
82 F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, | |
83 | |
84 VolumeUp, | |
85 VolumeDown, | |
86 VolumeMute, | |
87 | |
88 PlayPause, | |
89 Stop, | |
90 NextTrack, | |
91 PrevTrack, | |
92 | |
93 Shift = 0x10000, | |
94 Control = 0x20000, | |
95 Alt = 0x40000 | |
96 } | |
97 | |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
98 const string[] keyStringTable = [ |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
99 "None", "Escape", "Tab", "Backspace", "Enter", "Space", |
0 | 100 "Left Arrow", "Right Arrow", "Up Arrow", "Down Arrow", |
101 "Insert", "Delete", "Home", "End", "Page Up", "Page Down", | |
102 "Print Screen", "Pause", | |
103 "Caps Lock", "Num Lock", "Scroll Lock", | |
104 "NumPad0", "NumPad1", "NumPad2", "NumPad3", "NumPad4", | |
105 "NumPad5", "NumPad6", "NumPad7", "NumPad8", "NumPad9", | |
106 "NumPad/", "NumPad*", "NumPad-", "NumPad+", "NumPad.", | |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
107 "`", "-", "=", "[", "]", "\\", ";", "'", ",", ".", "/", "Menu", |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
108 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
109 "", "", "", "", "", "", "", |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
110 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
111 "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
112 "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
113 "Volume Up", "Volume Down", "Volume Mute", |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
114 "Play/Pause", "Stop", "Next Track", "Previous Track" |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
115 ]; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
116 string keyToString(Key key) { |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
117 if(key == Key.Shift) |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
118 return "Shift"; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
119 else if(key == Key.Control) |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
120 return "Ctrl"; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
121 else if(key == Key.Alt) |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
122 return "Alt"; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
123 return keyStringTable[key]; |
0 | 124 } |
125 Key toKey(string str) { | |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
126 if(!str) |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
127 return Key.None; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
128 foreach(i, s; keyStringTable) |
0 | 129 if(s == str) |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
130 return cast(Key)i; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
131 return Key.None; |
0 | 132 } |
133 unittest { | |
134 assert(keyToString(Key.D0) == "0"); | |
135 assert(keyToString(Key.A) == "A"); | |
136 assert(keyToString(Key.N) == "N"); | |
137 assert(keyToString(Key.F1) == "F1"); | |
138 assert(keyToString(Key.Escape) == "Escape"); | |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
139 assert(keyToString(Key.Left) == "Left Arrow"); |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
140 assert(keyToString(Key.Up) == "Up Arrow"); |
0 | 141 assert(keyToString(Key.Insert) == "Insert"); |
142 assert(keyToString(Key.PrintScreen) == "Print Screen"); | |
143 assert(keyToString(Key.Pause) == "Pause"); | |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
144 assert(keyToString(Key.CapsLock) == "Caps Lock"); |
0 | 145 assert(keyToString(Key.NumPad0) == "NumPad0"); |
146 assert(keyToString(Key.NumPad5) == "NumPad5"); | |
147 assert(keyToString(Key.NumPadDivide) == "NumPad/"); | |
148 assert(keyToString(Key.Backquote) == "`"); | |
149 assert(keyToString(Key.Control) == "Ctrl"); | |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
150 } |
0 | 151 |