comparison engine/core/eventchannel/key/ec_key.h @ 111:abfe54ebae15

fix key event, patch by yonibear
author spq@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 26 Jul 2008 17:35:02 +0000
parents 90005975cdbb
children bb9902910067
comparison
equal deleted inserted replaced
110:68534083e1a1 111:abfe54ebae15
25 // Standard C++ library includes 25 // Standard C++ library includes
26 // 26 //
27 27
28 // 3rd party library includes 28 // 3rd party library includes
29 // 29 //
30 #include <SDL.h>
30 31
31 // FIFE includes 32 // FIFE includes
32 // These includes are split up in two parts, separated by one empty line 33 // These includes are split up in two parts, separated by one empty line
33 // First block: files included from the FIFE root src directory 34 // First block: files included from the FIFE root src directory
34 // Second block: files included from the same folder 35 // Second block: files included from the same folder
37 38
38 /** Represents a key or a character. 39 /** Represents a key or a character.
39 */ 40 */
40 class Key { 41 class Key {
41 public: 42 public:
43 /** Codes for different keys
44 */
45 enum KeyType {
46 INVALID_KEY = -1,
47 BACKSPACE = SDLK_BACKSPACE,
48 TAB = SDLK_TAB,
49 CLEAR = SDLK_CLEAR,
50 ENTER = SDLK_RETURN,
51 PAUSE = SDLK_PAUSE,
52 ESCAPE = SDLK_ESCAPE,
53 SPACE = SDLK_SPACE,
54 EXCLAIM = SDLK_EXCLAIM,
55 QUOTEDBL = SDLK_QUOTEDBL,
56 HASH = SDLK_HASH,
57 DOLLAR = SDLK_DOLLAR,
58 AMPERSAND = SDLK_AMPERSAND,
59 QUOTE = SDLK_QUOTE,
60 LEFTPAREN = SDLK_LEFTPAREN,
61 RIGHTPAREN = SDLK_RIGHTPAREN,
62 ASTERISK = SDLK_ASTERISK,
63 PLUS = SDLK_PLUS,
64 COMMA = SDLK_COMMA,
65 MINUS = SDLK_MINUS,
66 PERIOD = SDLK_PERIOD,
67 SLASH = SDLK_SLASH,
68 NUM_0 = SDLK_0,
69 NUM_1 = SDLK_1,
70 NUM_2 = SDLK_2,
71 NUM_3 = SDLK_3,
72 NUM_4 = SDLK_4,
73 NUM_5 = SDLK_5,
74 NUM_6 = SDLK_6,
75 NUM_7 = SDLK_7,
76 NUM_8 = SDLK_8,
77 NUM_9 = SDLK_9,
78 COLON = SDLK_COLON,
79 SEMICOLON = SDLK_SEMICOLON,
80 LESS = SDLK_LESS,
81 EQUALS = SDLK_EQUALS,
82 GREATER = SDLK_GREATER,
83 QUESTION = SDLK_QUESTION,
84 AT = SDLK_AT,
85 LEFTBRACKET = SDLK_LEFTBRACKET,
86 BACKSLASH = SDLK_BACKSLASH,
87 RIGHTBRACKET = SDLK_RIGHTBRACKET,
88 CARET = SDLK_CARET,
89 UNDERSCORE = SDLK_UNDERSCORE,
90 BACKQUOTE = SDLK_BACKQUOTE,
91 A = SDLK_a,
92 B = SDLK_b,
93 C = SDLK_c,
94 D = SDLK_d,
95 E = SDLK_e,
96 F = SDLK_f,
97 G = SDLK_g,
98 H = SDLK_h,
99 I = SDLK_i,
100 J = SDLK_j,
101 K = SDLK_k,
102 L = SDLK_l,
103 M = SDLK_m,
104 N = SDLK_n,
105 O = SDLK_o,
106 P = SDLK_p,
107 Q = SDLK_q,
108 R = SDLK_r,
109 S = SDLK_s,
110 T = SDLK_t,
111 U = SDLK_u,
112 V = SDLK_v,
113 W = SDLK_w,
114 X = SDLK_x,
115 Y = SDLK_y,
116 Z = SDLK_z,
117 DELETE = SDLK_DELETE,
118 WORLD_0 = SDLK_WORLD_0,
119 WORLD_1 = SDLK_WORLD_1,
120 WORLD_2 = SDLK_WORLD_2,
121 WORLD_3 = SDLK_WORLD_3,
122 WORLD_4 = SDLK_WORLD_4,
123 WORLD_5 = SDLK_WORLD_5,
124 WORLD_6 = SDLK_WORLD_6,
125 WORLD_7 = SDLK_WORLD_7,
126 WORLD_8 = SDLK_WORLD_8,
127 WORLD_9 = SDLK_WORLD_9,
128 WORLD_10 = SDLK_WORLD_10,
129 WORLD_11 = SDLK_WORLD_11,
130 WORLD_12 = SDLK_WORLD_12,
131 WORLD_13 = SDLK_WORLD_13,
132 WORLD_14 = SDLK_WORLD_14,
133 WORLD_15 = SDLK_WORLD_15,
134 WORLD_16 = SDLK_WORLD_16,
135 WORLD_17 = SDLK_WORLD_17,
136 WORLD_18 = SDLK_WORLD_18,
137 WORLD_19 = SDLK_WORLD_19,
138 WORLD_20 = SDLK_WORLD_20,
139 WORLD_21 = SDLK_WORLD_21,
140 WORLD_22 = SDLK_WORLD_22,
141 WORLD_23 = SDLK_WORLD_23,
142 WORLD_24 = SDLK_WORLD_24,
143 WORLD_25 = SDLK_WORLD_25,
144 WORLD_26 = SDLK_WORLD_26,
145 WORLD_27 = SDLK_WORLD_27,
146 WORLD_28 = SDLK_WORLD_28,
147 WORLD_29 = SDLK_WORLD_29,
148 WORLD_30 = SDLK_WORLD_30,
149 WORLD_31 = SDLK_WORLD_31,
150 WORLD_32 = SDLK_WORLD_32,
151 WORLD_33 = SDLK_WORLD_33,
152 WORLD_34 = SDLK_WORLD_34,
153 WORLD_35 = SDLK_WORLD_35,
154 WORLD_36 = SDLK_WORLD_36,
155 WORLD_37 = SDLK_WORLD_37,
156 WORLD_38 = SDLK_WORLD_38,
157 WORLD_39 = SDLK_WORLD_39,
158 WORLD_40 = SDLK_WORLD_40,
159 WORLD_41 = SDLK_WORLD_41,
160 WORLD_42 = SDLK_WORLD_42,
161 WORLD_43 = SDLK_WORLD_43,
162 WORLD_44 = SDLK_WORLD_44,
163 WORLD_45 = SDLK_WORLD_45,
164 WORLD_46 = SDLK_WORLD_46,
165 WORLD_47 = SDLK_WORLD_47,
166 WORLD_48 = SDLK_WORLD_48,
167 WORLD_49 = SDLK_WORLD_49,
168 WORLD_50 = SDLK_WORLD_50,
169 WORLD_51 = SDLK_WORLD_51,
170 WORLD_52 = SDLK_WORLD_52,
171 WORLD_53 = SDLK_WORLD_53,
172 WORLD_54 = SDLK_WORLD_54,
173 WORLD_55 = SDLK_WORLD_55,
174 WORLD_56 = SDLK_WORLD_56,
175 WORLD_57 = SDLK_WORLD_57,
176 WORLD_58 = SDLK_WORLD_58,
177 WORLD_59 = SDLK_WORLD_59,
178 WORLD_60 = SDLK_WORLD_60,
179 WORLD_61 = SDLK_WORLD_61,
180 WORLD_62 = SDLK_WORLD_62,
181 WORLD_63 = SDLK_WORLD_63,
182 WORLD_64 = SDLK_WORLD_64,
183 WORLD_65 = SDLK_WORLD_65,
184 WORLD_66 = SDLK_WORLD_66,
185 WORLD_67 = SDLK_WORLD_67,
186 WORLD_68 = SDLK_WORLD_68,
187 WORLD_69 = SDLK_WORLD_69,
188 WORLD_70 = SDLK_WORLD_70,
189 WORLD_71 = SDLK_WORLD_71,
190 WORLD_72 = SDLK_WORLD_72,
191 WORLD_73 = SDLK_WORLD_73,
192 WORLD_74 = SDLK_WORLD_74,
193 WORLD_75 = SDLK_WORLD_75,
194 WORLD_76 = SDLK_WORLD_76,
195 WORLD_77 = SDLK_WORLD_77,
196 WORLD_78 = SDLK_WORLD_78,
197 WORLD_79 = SDLK_WORLD_79,
198 WORLD_80 = SDLK_WORLD_80,
199 WORLD_81 = SDLK_WORLD_81,
200 WORLD_82 = SDLK_WORLD_82,
201 WORLD_83 = SDLK_WORLD_83,
202 WORLD_84 = SDLK_WORLD_84,
203 WORLD_85 = SDLK_WORLD_85,
204 WORLD_86 = SDLK_WORLD_86,
205 WORLD_87 = SDLK_WORLD_87,
206 WORLD_88 = SDLK_WORLD_88,
207 WORLD_89 = SDLK_WORLD_89,
208 WORLD_90 = SDLK_WORLD_90,
209 WORLD_91 = SDLK_WORLD_91,
210 WORLD_92 = SDLK_WORLD_92,
211 WORLD_93 = SDLK_WORLD_93,
212 WORLD_94 = SDLK_WORLD_94,
213 WORLD_95 = SDLK_WORLD_95,
214 KP0 = SDLK_KP0,
215 KP1 = SDLK_KP1,
216 KP2 = SDLK_KP2,
217 KP3 = SDLK_KP3,
218 KP4 = SDLK_KP4,
219 KP5 = SDLK_KP5,
220 KP6 = SDLK_KP6,
221 KP7 = SDLK_KP7,
222 KP8 = SDLK_KP8,
223 KP9 = SDLK_KP9,
224 KP_PERIOD = SDLK_KP_PERIOD,
225 KP_DIVIDE = SDLK_KP_DIVIDE,
226 KP_MULTIPLY = SDLK_KP_MULTIPLY,
227 KP_MINUS = SDLK_KP_MINUS,
228 KP_PLUS = SDLK_KP_PLUS,
229 KP_ENTER = SDLK_KP_ENTER,
230 KP_EQUALS = SDLK_KP_EQUALS,
231 UP = SDLK_UP,
232 DOWN = SDLK_DOWN,
233 RIGHT = SDLK_RIGHT,
234 LEFT = SDLK_LEFT,
235 INSERT = SDLK_INSERT,
236 HOME = SDLK_HOME,
237 END = SDLK_END,
238 PAGE_UP = SDLK_PAGEUP,
239 PAGE_DOWN = SDLK_PAGEDOWN,
240 F1 = SDLK_F1,
241 F2 = SDLK_F2,
242 F3 = SDLK_F3,
243 F4 = SDLK_F4,
244 F5 = SDLK_F5,
245 F6 = SDLK_F6,
246 F7 = SDLK_F7,
247 F8 = SDLK_F8,
248 F9 = SDLK_F9,
249 F10 = SDLK_F10,
250 F11 = SDLK_F11,
251 F12 = SDLK_F12,
252 F13 = SDLK_F13,
253 F14 = SDLK_F14,
254 F15 = SDLK_F15,
255 NUM_LOCK = SDLK_NUMLOCK,
256 CAPS_LOCK = SDLK_CAPSLOCK,
257 SCROLL_LOCK = SDLK_SCROLLOCK,
258 RIGHT_SHIFT = SDLK_RSHIFT,
259 LEFT_SHIFT = SDLK_LSHIFT,
260 RIGHT_CONTROL = SDLK_RCTRL,
261 LEFT_CONTROL = SDLK_LCTRL,
262 RIGHT_ALT = SDLK_RALT,
263 LEFT_ALT = SDLK_LALT,
264 RIGHT_META = SDLK_RMETA,
265 LEFT_META = SDLK_LMETA,
266 RIGHT_SUPER = SDLK_RSUPER,
267 LEFT_SUPER = SDLK_LSUPER,
268 ALT_GR = SDLK_MODE,
269 COMPOSE = SDLK_COMPOSE,
270 HELP = SDLK_HELP,
271 PRINT_SCREEN = SDLK_PRINT,
272 SYSREQ = SDLK_SYSREQ,
273 BREAK = SDLK_BREAK,
274 MENU = SDLK_MENU,
275 POWER = SDLK_POWER,
276 EURO = SDLK_EURO,
277 UNDO = SDLK_UNDO
278 };
279
42 /** Constructor 280 /** Constructor
43 * @param value value of the key 281 * @param value value of the key
44 * @param repr string represntation of the key 282 * @param codepoint Unicode representation of the key
45 */ 283 */
46 Key(int value, const std::string& repr=""): 284 Key(KeyType key = INVALID_KEY, uint32_t codepoint = 0):
47 m_value(value), 285 m_key(key),
48 m_repr(repr) {} 286 m_unicode(codepoint) {}
49 287
50 /** Destructor. 288 /** Destructor.
51 */ 289 */
52 virtual ~Key() {} 290 virtual ~Key() {}
53 291
54 /** Checks whether a key is a character. 292 /** Tells whether the Key can be represented as a string.
55 */ 293 */
56 bool isCharacter() const { 294 bool isPrintable() const {
57 return (m_value >= 32 && m_value <= 126) 295 // Unicode does not have control characters, so this should be sufficient
58 || (m_value >= 162 && m_value <= 255) 296 return m_unicode != 0;
59 || (m_value == 9);
60 }
61
62 /** Gets the value of the key.
63 */
64 bool isNumber() const {
65 return m_value >= 48 && m_value <= 57;
66 }
67
68 /** Checks whether a key is a letter.
69 */
70 bool isLetter() const {
71 return (((m_value >= 65 && m_value <= 90)
72 || (m_value >= 97 && m_value <= 122)
73 || (m_value >= 192 && m_value <= 255))
74 && (m_value != 215) && (m_value != 247));
75 } 297 }
76 298
299
77 /** Gets the value of the key. 300 /** Gets the value of the key.
78 */ 301 */
79 int getValue() const { 302 KeyType getValue() const {
80 return m_value; 303 return m_key;
81 } 304 }
82 305
83 /** Gets the string representation of the key. 306 /** Gets the string representation of the key, encoded as UTF-8.
84 */ 307 */
85 const std::string& getAsString() const { 308 std::string getAsString() const {
86 return m_repr; 309 // older sdl versions have the 3rd argument non const, but it is in fact const...
310 char* converted = SDL_iconv_string("UTF-8", "UTF-32",
311 const_cast<char*>(reinterpret_cast<const char*>(&m_unicode)), 4);
312 std::string retval(converted);
313 SDL_free(converted);
314 return retval;
87 } 315 }
88 316
89 /** Codes for different keys
90 */
91 enum KeyType {
92 INVALID_KEY = -1,
93 SPACE = ' ',
94 TAB = '\t',
95 ENTER = '\n',
96 LEFT_ALT = 1000,
97 RIGHT_ALT,
98 LEFT_SHIFT,
99 RIGHT_SHIFT,
100 LEFT_CONTROL,
101 RIGHT_CONTROL,
102 LEFT_META,
103 RIGHT_META,
104 LEFT_SUPER,
105 RIGHT_SUPER,
106 INSERT,
107 HOME,
108 PAGE_UP,
109 DELETE_KEY,
110 END,
111 PAGE_DOWN,
112 ESCAPE,
113 CAPS_LOCK,
114 BACKSPACE,
115 F1,
116 F2,
117 F3,
118 F4,
119 F5,
120 F6,
121 F7,
122 F8,
123 F9,
124 F10,
125 F11,
126 F12,
127 F13,
128 F14,
129 F15,
130 PRINT_SCREEN,
131 SCROLL_LOCK,
132 PAUSE,
133 NUM_LOCK,
134 ALT_GR,
135 LEFT,
136 RIGHT,
137 UP,
138 DOWN
139 };
140
141 private: 317 private:
142 int m_value; 318 KeyType m_key;
143 std::string m_repr; 319 uint32_t m_unicode;
144 }; 320 };
145 321
146 } //FIFE 322 } //FIFE
147 323
148 #endif 324 #endif