Mercurial > projects > dynamin
annotate dynamin/gui/key.d @ 71:63ea570c8d7c
Add Key to string and string to Key conversion.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 10 Aug 2009 17:04:08 -0500 |
parents | aa4efef0f0b1 |
children | 73060bc3f004 |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2007-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.gui.key; | |
27 | |
28 import dynamin.core.string; | |
29 | |
30 /** | |
31 * No Menu key is included because Shift+F10 is used instead. | |
32 * Note: Windows does not send KeyDown events for PrintScreen, only KeyUp. | |
33 */ | |
34 enum Key { | |
35 None, | |
36 Escape, | |
37 Tab, | |
38 Backspace, | |
39 Enter, | |
40 Space, | |
41 | |
42 Left, | |
43 Right, | |
44 Up, | |
45 Down, | |
46 | |
47 Insert, | |
48 Delete, | |
49 Home, | |
50 End, | |
51 PageUp, | |
52 PageDown, | |
53 | |
54 PrintScreen, | |
55 Pause, | |
56 | |
57 CapsLock, | |
58 NumLock, | |
59 ScrollLock, | |
60 | |
61 NumPad0, | |
62 NumPad1, | |
63 NumPad2, | |
64 NumPad3, | |
65 NumPad4, | |
66 NumPad5, | |
67 NumPad6, | |
68 NumPad7, | |
69 NumPad8, | |
70 NumPad9, | |
71 NumPadDivide, | |
72 NumPadMultiply, | |
73 NumPadSubtract, | |
74 NumPadAdd, | |
75 NumPadDecimal, // TODO: NumPadPoint? | |
76 | |
77 Backquote, | |
78 Minus, | |
79 Equals, | |
80 OpenBracket, | |
81 CloseBracket, | |
82 Backslash, | |
83 Semicolon, | |
84 Quote, | |
85 Comma, | |
86 Period, | |
87 Slash, | |
88 | |
89 /// Windows sends these messages when the Menu key is pressed and released: | |
90 /// Menu pressed, Menu released, Shift pressed, F10 pressed, F10 released, Shift released | |
91 /// So if any program responds to either Menu or Shift+F10, it will work right. | |
92 Menu, // right of spacebar, between WinKey and Ctrl | |
93 | |
94 // digits | |
95 D0 = 0x30, D1, D2, D3, D4, D5, D6, D7, D8, D9 = 0x39, | |
96 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, | |
97 F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, | |
98 | |
99 VolumeUp, | |
100 VolumeDown, | |
101 VolumeMute, | |
102 | |
103 PlayPause, | |
104 Stop, | |
105 NextTrack, | |
106 PrevTrack, | |
107 | |
108 Shift = 0x10000, | |
109 Control = 0x20000, | |
110 Alt = 0x40000 | |
111 } | |
112 | |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
113 const string[] keyStringTable = [ |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
114 "None", "Escape", "Tab", "Backspace", "Enter", "Space", |
0 | 115 "Left Arrow", "Right Arrow", "Up Arrow", "Down Arrow", |
116 "Insert", "Delete", "Home", "End", "Page Up", "Page Down", | |
117 "Print Screen", "Pause", | |
118 "Caps Lock", "Num Lock", "Scroll Lock", | |
119 "NumPad0", "NumPad1", "NumPad2", "NumPad3", "NumPad4", | |
120 "NumPad5", "NumPad6", "NumPad7", "NumPad8", "NumPad9", | |
121 "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
|
122 "`", "-", "=", "[", "]", "\\", ";", "'", ",", ".", "/", "Menu", |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
123 "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
|
124 "", "", "", "", "", "", "", |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
125 "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
|
126 "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
|
127 "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
|
128 "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
|
129 "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
|
130 ]; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
131 string keyToString(Key key) { |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
132 if(key == Key.Shift) |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
133 return "Shift"; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
134 else if(key == Key.Control) |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
135 return "Ctrl"; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
136 else if(key == Key.Alt) |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
137 return "Alt"; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
138 return keyStringTable[key]; |
0 | 139 } |
140 Key toKey(string str) { | |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
141 if(!str) |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
142 return Key.None; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
143 foreach(i, s; keyStringTable) |
0 | 144 if(s == str) |
71
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
145 return cast(Key)i; |
63ea570c8d7c
Add Key to string and string to Key conversion.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
146 return Key.None; |
0 | 147 } |
148 unittest { | |
149 assert(keyToString(Key.D0) == "0"); | |
150 assert(keyToString(Key.A) == "A"); | |
151 assert(keyToString(Key.N) == "N"); | |
152 assert(keyToString(Key.F1) == "F1"); | |
153 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
|
154 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
|
155 assert(keyToString(Key.Up) == "Up Arrow"); |
0 | 156 assert(keyToString(Key.Insert) == "Insert"); |
157 assert(keyToString(Key.PrintScreen) == "Print Screen"); | |
158 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
|
159 assert(keyToString(Key.CapsLock) == "Caps Lock"); |
0 | 160 assert(keyToString(Key.NumPad0) == "NumPad0"); |
161 assert(keyToString(Key.NumPad5) == "NumPad5"); | |
162 assert(keyToString(Key.NumPadDivide) == "NumPad/"); | |
163 assert(keyToString(Key.Backquote) == "`"); | |
164 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
|
165 } |
0 | 166 |