Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KeyName.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 // Ooh, a file of keynames. Fun.
19 
20 #include "precompiled.h"
21 
22 #include "KeyName.h"
23 #include "CStr.h"
25 
26 #include <map>
27 
28 static std::map<CStr,int> keymap;
29 
31 {
32  int keycode;
33  const char* keyname;
34  const char* altkeyname;
35 };
36 
37 // You can use either key name in the config file...
38 
40 {
41  /* Just a tad friendlier than SDL_GetKeyName's name */
42  { SDLK_BACKSPACE, "Backspace", "BkSp" },
43  { SDLK_TAB, "Tab", 0 },
44  { SDLK_CLEAR, "Clear", 0 }, // ?
45  { SDLK_RETURN, "Return", "Ret" },
46  { SDLK_PAUSE, "Pause", 0 }, // ?
47  { SDLK_ESCAPE, "Escape", "Esc" },
48  { SDLK_SPACE, "Space", "Spc" },
49  { SDLK_EXCLAIM, "!", "Exclaim" },
50  { SDLK_QUOTEDBL, "\"", "DoubleQuote" },
51  { SDLK_HASH, "#", "Hash" },
52  { SDLK_DOLLAR, "$", "Dollar" },
53  { SDLK_AMPERSAND, "&", "Ampersand" },
54  { SDLK_QUOTE, "'", "SingleQuote" },
55  { SDLK_LEFTPAREN, "(", "LeftParen" },
56  { SDLK_RIGHTPAREN, ")", "RightParen" },
57  { SDLK_ASTERISK, "*", "Asterisk" },
58  { SDLK_PLUS, "+", "Plus" },
59  { SDLK_COMMA, ",", "Comma" },
60  { SDLK_MINUS, "-", "Minus" },
61  { SDLK_PERIOD, ".", "Period" },
62  { SDLK_SLASH, "/", "ForwardSlash" },
63  { SDLK_0, "0", 0 },
64  { SDLK_1, "1", 0 },
65  { SDLK_2, "2", 0 },
66  { SDLK_3, "3", 0 },
67  { SDLK_4, "4", 0 },
68  { SDLK_5, "5", 0 },
69  { SDLK_6, "6", 0 },
70  { SDLK_7, "7", 0 },
71  { SDLK_8, "8", 0 },
72  { SDLK_9, "9", 0 },
73  { SDLK_COLON, ":", "Colon" },
74  { SDLK_SEMICOLON, ";", "Semicolon" },
75  { SDLK_LESS, "<", "LessThan" },
76  { SDLK_EQUALS, "=", "Equals" },
77  { SDLK_GREATER, ">", "GreaterThan" },
78  { SDLK_QUESTION, "?", "Question" },
79  { SDLK_AT, "@", "At" },
80  { SDLK_LEFTBRACKET, "[", "LeftBracket" },
81  { SDLK_BACKSLASH, "\\", "BackSlash" },
82  { SDLK_RIGHTBRACKET, "]", "RightBracket" },
83  { SDLK_CARET, "^", "Caret", },
84  { SDLK_UNDERSCORE, "_", "Underscore" },
85  { SDLK_BACKQUOTE, "`", "BackQuote" },
86  { SDLK_a, "A", 0 },
87  { SDLK_b, "B", 0 },
88  { SDLK_c, "C", 0 },
89  { SDLK_d, "D", 0 },
90  { SDLK_e, "E", 0 },
91  { SDLK_f, "F", 0 },
92  { SDLK_g, "G", 0 },
93  { SDLK_h, "H", 0 },
94  { SDLK_i, "I", 0 },
95  { SDLK_j, "J", 0 },
96  { SDLK_k, "K", 0 },
97  { SDLK_l, "L", 0 },
98  { SDLK_m, "M", 0 },
99  { SDLK_n, "N", 0 },
100  { SDLK_o, "O", 0 },
101  { SDLK_p, "P", 0 },
102  { SDLK_q, "Q", 0 },
103  { SDLK_r, "R", 0 },
104  { SDLK_s, "S", 0 },
105  { SDLK_t, "T", 0 },
106  { SDLK_u, "U", 0 },
107  { SDLK_v, "V", 0 },
108  { SDLK_w, "W", 0 },
109  { SDLK_x, "X", 0 },
110  { SDLK_y, "Y", 0 },
111  { SDLK_z, "Z", 0 },
112  { SDLK_DELETE, "Delete", "Del" },
113 
114 #if SDL_VERSION_ATLEAST(2, 0, 0)
115  { SDLK_KP_0, "Numpad 0", "Num0" },
116  { SDLK_KP_1, "Numpad 1", "Num1" },
117  { SDLK_KP_2, "Numpad 2", "Num2" },
118  { SDLK_KP_3, "Numpad 3", "Num3" },
119  { SDLK_KP_4, "Numpad 4", "Num4" },
120  { SDLK_KP_5, "Numpad 5", "Num5" },
121  { SDLK_KP_6, "Numpad 6", "Num6" },
122  { SDLK_KP_7, "Numpad 7", "Num7" },
123  { SDLK_KP_8, "Numpad 8", "Num8" },
124  { SDLK_KP_9, "Numpad 9", "Num9" },
125 #else
126  { SDLK_WORLD_0, "World0", "W0" },
127  { SDLK_WORLD_1, "World1", "W1" },
128  { SDLK_WORLD_2, "World2", "W2" },
129  { SDLK_WORLD_3, "World3", "W3" },
130  { SDLK_WORLD_4, "World4", "W4" },
131  { SDLK_WORLD_5, "World5", "W5" },
132  { SDLK_WORLD_6, "World6", "W6" },
133  { SDLK_WORLD_7, "World7", "W7" },
134  { SDLK_WORLD_8, "World8", "W8" },
135  { SDLK_WORLD_9, "World9", "W9" },
136  { SDLK_WORLD_10, "World10", "W10" },
137  { SDLK_WORLD_11, "World11", "W11" },
138  { SDLK_WORLD_12, "World12", "W12" },
139  { SDLK_WORLD_13, "World13", "W13" },
140  { SDLK_WORLD_14, "World14", "W14" },
141  { SDLK_WORLD_15, "World15", "W15" },
142  { SDLK_WORLD_16, "World16", "W16" },
143  { SDLK_WORLD_17, "World17", "W17" },
144  { SDLK_WORLD_18, "World18", "W18" },
145  { SDLK_WORLD_19, "World19", "W19" },
146  { SDLK_WORLD_20, "World20", "W20" },
147  { SDLK_WORLD_21, "World21", "W21" },
148  { SDLK_WORLD_22, "World22", "W22" },
149  { SDLK_WORLD_23, "World23", "W23" },
150  { SDLK_WORLD_24, "World24", "W24" },
151  { SDLK_WORLD_25, "World25", "W25" },
152  { SDLK_WORLD_26, "World26", "W26" },
153  { SDLK_WORLD_27, "World27", "W27" },
154  { SDLK_WORLD_28, "World28", "W28" },
155  { SDLK_WORLD_29, "World29", "W29" },
156  { SDLK_WORLD_30, "World30", "W30" },
157  { SDLK_WORLD_31, "World31", "W31" },
158  { SDLK_WORLD_32, "World32", "W32" },
159  { SDLK_WORLD_33, "World33", "W33" },
160  { SDLK_WORLD_34, "World34", "W34" },
161  { SDLK_WORLD_35, "World35", "W35" },
162  { SDLK_WORLD_36, "World36", "W36" },
163  { SDLK_WORLD_37, "World37", "W37" },
164  { SDLK_WORLD_38, "World38", "W38" },
165  { SDLK_WORLD_39, "World39", "W39" },
166  { SDLK_WORLD_40, "World40", "W40" },
167  { SDLK_WORLD_41, "World41", "W41" },
168  { SDLK_WORLD_42, "World42", "W42" },
169  { SDLK_WORLD_43, "World43", "W43" },
170  { SDLK_WORLD_44, "World44", "W44" },
171  { SDLK_WORLD_45, "World45", "W45" },
172  { SDLK_WORLD_46, "World46", "W46" },
173  { SDLK_WORLD_47, "World47", "W47" },
174  { SDLK_WORLD_48, "World48", "W48" },
175  { SDLK_WORLD_49, "World49", "W49" },
176  { SDLK_WORLD_50, "World50", "W50" },
177  { SDLK_WORLD_51, "World51", "W51" },
178  { SDLK_WORLD_52, "World52", "W52" },
179  { SDLK_WORLD_53, "World53", "W53" },
180  { SDLK_WORLD_54, "World54", "W54" },
181  { SDLK_WORLD_55, "World55", "W55" },
182  { SDLK_WORLD_56, "World56", "W56" },
183  { SDLK_WORLD_57, "World57", "W57" },
184  { SDLK_WORLD_58, "World58", "W58" },
185  { SDLK_WORLD_59, "World59", "W59" },
186  { SDLK_WORLD_60, "World60", "W60" },
187  { SDLK_WORLD_61, "World61", "W61" },
188  { SDLK_WORLD_62, "World62", "W62" },
189  { SDLK_WORLD_63, "World63", "W63" },
190  { SDLK_WORLD_64, "World64", "W64" },
191  { SDLK_WORLD_65, "World65", "W65" },
192  { SDLK_WORLD_66, "World66", "W66" },
193  { SDLK_WORLD_67, "World67", "W67" },
194  { SDLK_WORLD_68, "World68", "W68" },
195  { SDLK_WORLD_69, "World69", "W69" },
196  { SDLK_WORLD_70, "World70", "W70" },
197  { SDLK_WORLD_71, "World71", "W71" },
198  { SDLK_WORLD_72, "World72", "W72" },
199  { SDLK_WORLD_73, "World73", "W73" },
200  { SDLK_WORLD_74, "World74", "W74" },
201  { SDLK_WORLD_75, "World75", "W75" },
202  { SDLK_WORLD_76, "World76", "W76" },
203  { SDLK_WORLD_77, "World77", "W77" },
204  { SDLK_WORLD_78, "World78", "W78" },
205  { SDLK_WORLD_79, "World79", "W79" },
206  { SDLK_WORLD_80, "World80", "W80" },
207  { SDLK_WORLD_81, "World81", "W81" },
208  { SDLK_WORLD_82, "World82", "W82" },
209  { SDLK_WORLD_83, "World83", "W83" },
210  { SDLK_WORLD_84, "World84", "W84" },
211  { SDLK_WORLD_85, "World85", "W85" },
212  { SDLK_WORLD_86, "World86", "W86" },
213  { SDLK_WORLD_87, "World87", "W87" },
214  { SDLK_WORLD_88, "World88", "W88" },
215  { SDLK_WORLD_89, "World89", "W89" },
216  { SDLK_WORLD_90, "World90", "W90" },
217  { SDLK_WORLD_91, "World91", "W91" },
218  { SDLK_WORLD_92, "World92", "W92" },
219  { SDLK_WORLD_93, "World93", "W93" },
220  { SDLK_WORLD_94, "World94", "W94" },
221  { SDLK_WORLD_95, "World95", "W95" },
222 
223  { SDLK_KP0, "Numpad 0", "Num0" },
224  { SDLK_KP1, "Numpad 1", "Num1" },
225  { SDLK_KP2, "Numpad 2", "Num2" },
226  { SDLK_KP3, "Numpad 3", "Num3" },
227  { SDLK_KP4, "Numpad 4", "Num4" },
228  { SDLK_KP5, "Numpad 5", "Num5" },
229  { SDLK_KP6, "Numpad 6", "Num6" },
230  { SDLK_KP7, "Numpad 7", "Num7" },
231  { SDLK_KP8, "Numpad 8", "Num8" },
232  { SDLK_KP9, "Numpad 9", "Num9" },
233 #endif
234 
235  { SDLK_KP_PERIOD, "Numpad .", "NumPoint" },
236  { SDLK_KP_DIVIDE, "Numpad /", "NumDivide" },
237  { SDLK_KP_MULTIPLY, "Numpad *", "NumMultiply" },
238  { SDLK_KP_MINUS, "Numpad -", "NumMinus" },
239  { SDLK_KP_PLUS, "Numpad +", "NumPlus" },
240  { SDLK_KP_ENTER, "Numpad Enter", "NumEnter" },
241  { SDLK_KP_EQUALS, "Numpad =", "NumEquals" }, //?
242 
243  { SDLK_UP, "Arrow Up", "UpArrow" },
244  { SDLK_DOWN, "Arrow Down", "DownArrow" },
245  { SDLK_RIGHT, "Arrow Right", "RightArrow" },
246  { SDLK_LEFT, "Arrow Left", "LeftArrow" },
247  { SDLK_INSERT, "Insert", "Ins" },
248  { SDLK_HOME, "Home", 0 },
249  { SDLK_END, "End", 0 },
250  { SDLK_PAGEUP, "Page Up", "PgUp" },
251  { SDLK_PAGEDOWN, "Page Down", "PgDn" },
252 
253  { SDLK_F1, "F1", 0 },
254  { SDLK_F2, "F2", 0 },
255  { SDLK_F3, "F3", 0 },
256  { SDLK_F4, "F4", 0 },
257  { SDLK_F5, "F5", 0 },
258  { SDLK_F6, "F6", 0 },
259  { SDLK_F7, "F7", 0 },
260  { SDLK_F8, "F8", 0 },
261  { SDLK_F9, "F9", 0 },
262  { SDLK_F10, "F10", 0 },
263  { SDLK_F11, "F11", 0 },
264  { SDLK_F12, "F12", 0 },
265  { SDLK_F13, "F13", 0 },
266  { SDLK_F14, "F14", 0 },
267  { SDLK_F15, "F15", 0 },
268 
269 #if SDL_VERSION_ATLEAST(2, 0, 0)
270  { SDLK_NUMLOCKCLEAR, "Num Lock", "NumLock" },
271 #else
272  { SDLK_NUMLOCK, "Num Lock", "NumLock" },
273 #endif
274 
275  { SDLK_CAPSLOCK, "Caps Lock", "CapsLock" },
276 
277 #if SDL_VERSION_ATLEAST(2, 0, 0)
278  { SDLK_SCROLLLOCK, "Scroll Lock", "ScrlLock" },
279 #else
280  { SDLK_SCROLLOCK, "Scroll Lock", "ScrlLock" },
281 #endif
282 
283  { SDLK_RSHIFT, "Right Shift", "RightShift" },
284  { SDLK_LSHIFT, "Left Shift", "LeftShift" },
285  { SDLK_RCTRL, "Right Ctrl", "RightCtrl" },
286  { SDLK_LCTRL, "Left Ctrl", "LeftCtrl" },
287  { SDLK_RALT, "Right Alt", "RightAlt" },
288  { SDLK_LALT, "Left Alt", "LeftAlt" },
289 
290 #if SDL_VERSION_ATLEAST(2, 0, 0)
291  { SDLK_LGUI, "Left Super", "LeftWin" }, /* "Windows" keys */
292  { SDLK_RGUI, "Right Super", "RightWin" },
293 #else
294  { SDLK_LSUPER, "Left Super", "LeftWin" }, /* "Windows" keys */
295  { SDLK_RSUPER, "Right Super", "RightWin" },
296 #endif
297 
298  { SDLK_MODE, "Alt Gr", "AltGr" },
299 
300  { SDLK_HELP, "Help", 0 }, // ?
301 
302 #if SDL_VERSION_ATLEAST(2, 0, 0)
303  { SDLK_PRINTSCREEN, "Print Screen", "PrtSc" },
304 #else
305  { SDLK_PRINT, "Print Screen", "PrtSc" },
306 #endif
307 
308  { SDLK_SYSREQ, "SysRq", 0 },
309 
310 #if SDL_VERSION_ATLEAST(2, 0, 0)
311  { SDLK_STOP, "Break", 0 },
312 #else
313  { SDLK_BREAK, "Break", 0 },
314 #endif
315 
316  { SDLK_MENU, "Menu", 0 }, // ?
317  { SDLK_POWER, "Power", 0 }, // ?
318  { SDLK_UNDO, "Undo", 0 }, // ?
319  { MOUSE_LEFT, "Left Mouse Button", "MouseLeft" },
320  { MOUSE_RIGHT, "Right Mouse Button", "MouseRight" },
321  { MOUSE_MIDDLE, "Middle Mouse Button", "MouseMiddle" },
322  { MOUSE_WHEELUP, "Mouse Wheel Up", "WheelUp" },
323  { MOUSE_WHEELDOWN, "Mouse Wheel Down", "WheelDown" },
324  { MOUSE_X1, "Mouse X1", "MouseX1" },
325  { MOUSE_X2, "Mouse X2", "MouseX2" },
326  { UNIFIED_SHIFT, "Shift", "AnyShift" },
327  { UNIFIED_CTRL, "Ctrl", "AnyCtrl" },
328  { UNIFIED_ALT, "Alt", "AnyAlt" },
329  { UNIFIED_SUPER, "Super", "AnyWindows" },
330  { 0, 0, 0 },
331 };
332 
334 {
336  while( it->keycode != 0 )
337  {
338  keymap.insert( std::pair<CStr,int>( CStr( it->keyname ).LowerCase(), it->keycode ) );
339  if( it->altkeyname )
340  keymap.insert( std::pair<CStr,int>( CStr( it->altkeyname ).LowerCase(), it->keycode ) );
341  it++;
342  };
343 }
344 
345 int FindKeyCode( const CStr& keyname )
346 {
347  std::map<CStr,int>::iterator it;
348  it = keymap.find( keyname.LowerCase() );
349  if( it != keymap.end() )
350  return( it->second );
351  return( 0 );
352 }
353 
354 CStr FindKeyName( int keycode )
355 {
357  while( it->keycode != 0 )
358  {
359  if( it->keycode == keycode )
360  return( CStr( it->keyname ) );
361  }
362  return( CStr( "Unknown" ) );
363 }
364 
365 
int FindKeyCode(const CStr &keyname)
Definition: KeyName.cpp:345
CStr FindKeyName(int keycode)
Definition: KeyName.cpp:354
static std::map< CStr, int > keymap
Definition: KeyName.cpp:28
void InitKeyNameMap()
Definition: KeyName.cpp:333
const char * altkeyname
Definition: KeyName.cpp:34
const char * keyname
Definition: KeyName.cpp:33
static SKeycodeMapping keycodeMapping[]
Definition: KeyName.cpp:39