Mercurial > sdl-ios-xcode
comparison src/video/cocoa/SDL_cocoakeyboard.m @ 1959:25d6537feea4
Implemented Cocoa key event handling.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 30 Jul 2006 05:18:33 +0000 |
parents | 103c6fec2a60 |
children | 118daa3a24cc |
comparison
equal
deleted
inserted
replaced
1958:5fc6fb0fb605 | 1959:25d6537feea4 |
---|---|
20 slouken@libsdl.org | 20 slouken@libsdl.org |
21 */ | 21 */ |
22 #include "SDL_config.h" | 22 #include "SDL_config.h" |
23 | 23 |
24 #include "SDL_cocoavideo.h" | 24 #include "SDL_cocoavideo.h" |
25 #include "SDL_cocoakeys.h" | |
25 | 26 |
26 #include "../../events/SDL_keyboard_c.h" | 27 #include "../../events/SDL_keyboard_c.h" |
28 | |
29 | |
30 #ifndef NX_DEVICERCTLKEYMASK | |
31 #define NX_DEVICELCTLKEYMASK 0x00000001 | |
32 #endif | |
33 #ifndef NX_DEVICELSHIFTKEYMASK | |
34 #define NX_DEVICELSHIFTKEYMASK 0x00000002 | |
35 #endif | |
36 #ifndef NX_DEVICERSHIFTKEYMASK | |
37 #define NX_DEVICERSHIFTKEYMASK 0x00000004 | |
38 #endif | |
39 #ifndef NX_DEVICELCMDKEYMASK | |
40 #define NX_DEVICELCMDKEYMASK 0x00000008 | |
41 #endif | |
42 #ifndef NX_DEVICERCMDKEYMASK | |
43 #define NX_DEVICERCMDKEYMASK 0x00000010 | |
44 #endif | |
45 #ifndef NX_DEVICELALTKEYMASK | |
46 #define NX_DEVICELALTKEYMASK 0x00000020 | |
47 #endif | |
48 #ifndef NX_DEVICERALTKEYMASK | |
49 #define NX_DEVICERALTKEYMASK 0x00000040 | |
50 #endif | |
51 #ifndef NX_DEVICERCTLKEYMASK | |
52 #define NX_DEVICERCTLKEYMASK 0x00002000 | |
53 #endif | |
54 | |
55 static void | |
56 InitKeymap (SDLKey *keymap) | |
57 { | |
58 const void *KCHRPtr; | |
59 UInt32 state; | |
60 UInt32 value; | |
61 int i; | |
62 | |
63 for ( i=0; i<256; ++i ) | |
64 keymap[i] = SDLK_UNKNOWN; | |
65 | |
66 /* This keymap is almost exactly the same as the OS 9 one */ | |
67 keymap[KEY_ESCAPE] = SDLK_ESCAPE; | |
68 keymap[KEY_F1] = SDLK_F1; | |
69 keymap[KEY_F2] = SDLK_F2; | |
70 keymap[KEY_F3] = SDLK_F3; | |
71 keymap[KEY_F4] = SDLK_F4; | |
72 keymap[KEY_F5] = SDLK_F5; | |
73 keymap[KEY_F6] = SDLK_F6; | |
74 keymap[KEY_F7] = SDLK_F7; | |
75 keymap[KEY_F8] = SDLK_F8; | |
76 keymap[KEY_F9] = SDLK_F9; | |
77 keymap[KEY_F10] = SDLK_F10; | |
78 keymap[KEY_F11] = SDLK_F11; | |
79 keymap[KEY_F12] = SDLK_F12; | |
80 keymap[KEY_PRINT] = SDLK_PRINT; | |
81 keymap[KEY_SCROLLOCK] = SDLK_SCROLLOCK; | |
82 keymap[KEY_PAUSE] = SDLK_PAUSE; | |
83 keymap[KEY_POWER] = SDLK_POWER; | |
84 keymap[KEY_BACKQUOTE] = SDLK_BACKQUOTE; | |
85 keymap[KEY_1] = SDLK_1; | |
86 keymap[KEY_2] = SDLK_2; | |
87 keymap[KEY_3] = SDLK_3; | |
88 keymap[KEY_4] = SDLK_4; | |
89 keymap[KEY_5] = SDLK_5; | |
90 keymap[KEY_6] = SDLK_6; | |
91 keymap[KEY_7] = SDLK_7; | |
92 keymap[KEY_8] = SDLK_8; | |
93 keymap[KEY_9] = SDLK_9; | |
94 keymap[KEY_0] = SDLK_0; | |
95 keymap[KEY_MINUS] = SDLK_MINUS; | |
96 keymap[KEY_EQUALS] = SDLK_EQUALS; | |
97 keymap[KEY_BACKSPACE] = SDLK_BACKSPACE; | |
98 keymap[KEY_INSERT] = SDLK_INSERT; | |
99 keymap[KEY_HOME] = SDLK_HOME; | |
100 keymap[KEY_PAGEUP] = SDLK_PAGEUP; | |
101 keymap[KEY_NUMLOCK] = SDLK_NUMLOCK; | |
102 keymap[KEY_KP_EQUALS] = SDLK_KP_EQUALS; | |
103 keymap[KEY_KP_DIVIDE] = SDLK_KP_DIVIDE; | |
104 keymap[KEY_KP_MULTIPLY] = SDLK_KP_MULTIPLY; | |
105 keymap[KEY_TAB] = SDLK_TAB; | |
106 keymap[KEY_q] = SDLK_q; | |
107 keymap[KEY_w] = SDLK_w; | |
108 keymap[KEY_e] = SDLK_e; | |
109 keymap[KEY_r] = SDLK_r; | |
110 keymap[KEY_t] = SDLK_t; | |
111 keymap[KEY_y] = SDLK_y; | |
112 keymap[KEY_u] = SDLK_u; | |
113 keymap[KEY_i] = SDLK_i; | |
114 keymap[KEY_o] = SDLK_o; | |
115 keymap[KEY_p] = SDLK_p; | |
116 keymap[KEY_LEFTBRACKET] = SDLK_LEFTBRACKET; | |
117 keymap[KEY_RIGHTBRACKET] = SDLK_RIGHTBRACKET; | |
118 keymap[KEY_BACKSLASH] = SDLK_BACKSLASH; | |
119 keymap[KEY_DELETE] = SDLK_DELETE; | |
120 keymap[KEY_END] = SDLK_END; | |
121 keymap[KEY_PAGEDOWN] = SDLK_PAGEDOWN; | |
122 keymap[KEY_KP7] = SDLK_KP7; | |
123 keymap[KEY_KP8] = SDLK_KP8; | |
124 keymap[KEY_KP9] = SDLK_KP9; | |
125 keymap[KEY_KP_MINUS] = SDLK_KP_MINUS; | |
126 keymap[KEY_CAPSLOCK] = SDLK_CAPSLOCK; | |
127 keymap[KEY_a] = SDLK_a; | |
128 keymap[KEY_s] = SDLK_s; | |
129 keymap[KEY_d] = SDLK_d; | |
130 keymap[KEY_f] = SDLK_f; | |
131 keymap[KEY_g] = SDLK_g; | |
132 keymap[KEY_h] = SDLK_h; | |
133 keymap[KEY_j] = SDLK_j; | |
134 keymap[KEY_k] = SDLK_k; | |
135 keymap[KEY_l] = SDLK_l; | |
136 keymap[KEY_SEMICOLON] = SDLK_SEMICOLON; | |
137 keymap[KEY_QUOTE] = SDLK_QUOTE; | |
138 keymap[KEY_RETURN] = SDLK_RETURN; | |
139 keymap[KEY_KP4] = SDLK_KP4; | |
140 keymap[KEY_KP5] = SDLK_KP5; | |
141 keymap[KEY_KP6] = SDLK_KP6; | |
142 keymap[KEY_KP_PLUS] = SDLK_KP_PLUS; | |
143 keymap[KEY_LSHIFT] = SDLK_LSHIFT; | |
144 keymap[KEY_RSHIFT] = SDLK_RSHIFT; | |
145 keymap[KEY_z] = SDLK_z; | |
146 keymap[KEY_x] = SDLK_x; | |
147 keymap[KEY_c] = SDLK_c; | |
148 keymap[KEY_v] = SDLK_v; | |
149 keymap[KEY_b] = SDLK_b; | |
150 keymap[KEY_n] = SDLK_n; | |
151 keymap[KEY_m] = SDLK_m; | |
152 keymap[KEY_COMMA] = SDLK_COMMA; | |
153 keymap[KEY_PERIOD] = SDLK_PERIOD; | |
154 keymap[KEY_SLASH] = SDLK_SLASH; | |
155 keymap[KEY_UP] = SDLK_UP; | |
156 keymap[KEY_KP1] = SDLK_KP1; | |
157 keymap[KEY_KP2] = SDLK_KP2; | |
158 keymap[KEY_KP3] = SDLK_KP3; | |
159 keymap[KEY_KP_ENTER] = SDLK_KP_ENTER; | |
160 keymap[KEY_LCTRL] = SDLK_LCTRL; | |
161 keymap[KEY_LALT] = SDLK_LALT; | |
162 keymap[KEY_LMETA] = SDLK_LMETA; | |
163 keymap[KEY_RCTRL] = SDLK_RCTRL; | |
164 keymap[KEY_RALT] = SDLK_RALT; | |
165 keymap[KEY_RMETA] = SDLK_RMETA; | |
166 keymap[KEY_SPACE] = SDLK_SPACE; | |
167 keymap[KEY_LEFT] = SDLK_LEFT; | |
168 keymap[KEY_DOWN] = SDLK_DOWN; | |
169 keymap[KEY_RIGHT] = SDLK_RIGHT; | |
170 keymap[KEY_KP0] = SDLK_KP0; | |
171 keymap[KEY_KP_PERIOD] = SDLK_KP_PERIOD; | |
172 keymap[KEY_IBOOK_ENTER] = SDLK_KP_ENTER; | |
173 keymap[KEY_IBOOK_RIGHT] = SDLK_RIGHT; | |
174 keymap[KEY_IBOOK_DOWN] = SDLK_DOWN; | |
175 keymap[KEY_IBOOK_UP] = SDLK_UP; | |
176 keymap[KEY_IBOOK_LEFT] = SDLK_LEFT; | |
177 | |
178 /* | |
179 Up there we setup a static scancode->keysym map. However, it will not | |
180 work very well on international keyboard. Hence we now query Mac OS X | |
181 for its own keymap to adjust our own mapping table. However, this is | |
182 basically only useful for ascii char keys. This is also the reason | |
183 why we keep the static table, too. | |
184 */ | |
185 | |
186 /* Get a pointer to the systems cached KCHR */ | |
187 KCHRPtr = (void *)GetScriptManagerVariable(smKCHRCache); | |
188 if (KCHRPtr) { | |
189 /* Loop over all 127 possible scan codes */ | |
190 for (i = 0; i < 0x7F; i++) { | |
191 /* We pretend a clean start to begin with (i.e. no dead keys active */ | |
192 state = 0; | |
193 | |
194 /* Now translate the key code to a key value */ | |
195 value = KeyTranslate(KCHRPtr, i, &state) & 0xff; | |
196 | |
197 /* If the state become 0, it was a dead key. We need to translate again, | |
198 passing in the new state, to get the actual key value */ | |
199 if (state != 0) | |
200 value = KeyTranslate(KCHRPtr, i, &state) & 0xff; | |
201 | |
202 /* Now we should have a latin1 value, which are SDL keysyms */ | |
203 if (value >= 32 && value <= 255) { | |
204 keymap[i] = value; | |
205 } | |
206 } | |
207 } | |
208 | |
209 /* | |
210 The keypad codes are re-setup here, because the loop above cannot | |
211 distinguish between a key on the keypad and a regular key. We maybe | |
212 could get around this problem in another fashion: NSEvent's flags | |
213 include a "NSNumericPadKeyMask" bit; we could check that and modify | |
214 the symbol we return on the fly. However, this flag seems to exhibit | |
215 some weird behaviour related to the num lock key | |
216 */ | |
217 keymap[KEY_KP0] = SDLK_KP0; | |
218 keymap[KEY_KP1] = SDLK_KP1; | |
219 keymap[KEY_KP2] = SDLK_KP2; | |
220 keymap[KEY_KP3] = SDLK_KP3; | |
221 keymap[KEY_KP4] = SDLK_KP4; | |
222 keymap[KEY_KP5] = SDLK_KP5; | |
223 keymap[KEY_KP6] = SDLK_KP6; | |
224 keymap[KEY_KP7] = SDLK_KP7; | |
225 keymap[KEY_KP8] = SDLK_KP8; | |
226 keymap[KEY_KP9] = SDLK_KP9; | |
227 keymap[KEY_KP_MINUS] = SDLK_KP_MINUS; | |
228 keymap[KEY_KP_PLUS] = SDLK_KP_PLUS; | |
229 keymap[KEY_KP_PERIOD] = SDLK_KP_PERIOD; | |
230 keymap[KEY_KP_EQUALS] = SDLK_KP_EQUALS; | |
231 keymap[KEY_KP_DIVIDE] = SDLK_KP_DIVIDE; | |
232 keymap[KEY_KP_MULTIPLY] = SDLK_KP_MULTIPLY; | |
233 keymap[KEY_KP_ENTER] = SDLK_KP_ENTER; | |
234 } | |
235 | |
236 /* This is the original behavior, before support was added for | |
237 * differentiating between left and right versions of the keys. | |
238 */ | |
239 static void | |
240 DoUnsidedModifiers(int keyboard, unsigned short scancode, | |
241 unsigned int oldMods, unsigned int newMods) | |
242 { | |
243 const int mapping[] = { SDLK_CAPSLOCK, SDLK_LSHIFT, SDLK_LCTRL, SDLK_LALT, SDLK_LMETA }; | |
244 unsigned int i, bit; | |
245 | |
246 /* Iterate through the bits, testing each against the current modifiers */ | |
247 for (i = 0, bit = NSAlphaShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) { | |
248 unsigned int oldMask, newMask; | |
249 | |
250 oldMask = oldMods & bit; | |
251 newMask = newMods & bit; | |
252 | |
253 if (oldMask && oldMask != newMask) { /* modifier up event */ | |
254 /* If this was Caps Lock, we need some additional voodoo to make SDL happy */ | |
255 if (bit == NSAlphaShiftKeyMask) { | |
256 SDL_SendKeyboardKey(keyboard, SDL_PRESSED, (Uint8)scancode, mapping[i]); | |
257 } | |
258 SDL_SendKeyboardKey(keyboard, SDL_RELEASED, (Uint8)scancode, mapping[i]); | |
259 } else if (newMask && oldMask != newMask) { /* modifier down event */ | |
260 SDL_SendKeyboardKey(keyboard, SDL_PRESSED, (Uint8)scancode, mapping[i]); | |
261 /* If this was Caps Lock, we need some additional voodoo to make SDL happy */ | |
262 if (bit == NSAlphaShiftKeyMask) { | |
263 SDL_SendKeyboardKey(keyboard, SDL_RELEASED, (Uint8)scancode, mapping[i]); | |
264 } | |
265 } | |
266 } | |
267 } | |
268 | |
269 /* This is a helper function for HandleModifierSide. This | |
270 * function reverts back to behavior before the distinction between | |
271 * sides was made. | |
272 */ | |
273 static void | |
274 HandleNonDeviceModifier(int keyboard, unsigned short scancode, | |
275 unsigned int device_independent_mask, | |
276 unsigned int oldMods, | |
277 unsigned int newMods, | |
278 SDLKey key_sym) | |
279 { | |
280 unsigned int oldMask, newMask; | |
281 | |
282 /* Isolate just the bits we care about in the depedent bits so we can | |
283 * figure out what changed | |
284 */ | |
285 oldMask = oldMods & device_independent_mask; | |
286 newMask = newMods & device_independent_mask; | |
287 | |
288 if (oldMask && oldMask != newMask) { | |
289 SDL_SendKeyboardKey(keyboard, SDL_RELEASED, (Uint8)scancode, key_sym); | |
290 } else if (newMask && oldMask != newMask) { | |
291 SDL_SendKeyboardKey(keyboard, SDL_PRESSED, (Uint8)scancode, key_sym); | |
292 } | |
293 } | |
294 | |
295 /* This is a helper function for HandleModifierSide. | |
296 * This function sets the actual SDL_PrivateKeyboard event. | |
297 */ | |
298 static void | |
299 HandleModifierOneSide(int keyboard, unsigned short scancode, | |
300 unsigned int oldMods, unsigned int newMods, | |
301 SDLKey key_sym, | |
302 unsigned int sided_device_dependent_mask) | |
303 { | |
304 unsigned int old_dep_mask, new_dep_mask; | |
305 | |
306 /* Isolate just the bits we care about in the depedent bits so we can | |
307 * figure out what changed | |
308 */ | |
309 old_dep_mask = oldMods & sided_device_dependent_mask; | |
310 new_dep_mask = newMods & sided_device_dependent_mask; | |
311 | |
312 /* We now know that this side bit flipped. But we don't know if | |
313 * it went pressed to released or released to pressed, so we must | |
314 * find out which it is. | |
315 */ | |
316 if (new_dep_mask && old_dep_mask != new_dep_mask) { | |
317 SDL_SendKeyboardKey(keyboard, SDL_PRESSED, (Uint8)scancode, key_sym); | |
318 } else { | |
319 SDL_SendKeyboardKey(keyboard, SDL_RELEASED, (Uint8)scancode, key_sym); | |
320 } | |
321 } | |
322 | |
323 /* This is a helper function for DoSidedModifiers. | |
324 * This function will figure out if the modifier key is the left or right side, | |
325 * e.g. left-shift vs right-shift. | |
326 */ | |
327 static void | |
328 HandleModifierSide(int keyboard, unsigned short scancode, | |
329 int device_independent_mask, | |
330 unsigned int oldMods, unsigned int newMods, | |
331 SDLKey left_key_sym, | |
332 SDLKey right_key_sym, | |
333 unsigned int left_device_dependent_mask, | |
334 unsigned int right_device_dependent_mask) | |
335 { | |
336 unsigned int device_dependent_mask = (left_device_dependent_mask | | |
337 right_device_dependent_mask); | |
338 unsigned int diff_mod; | |
339 | |
340 /* On the basis that the device independent mask is set, but there are | |
341 * no device dependent flags set, we'll assume that we can't detect this | |
342 * keyboard and revert to the unsided behavior. | |
343 */ | |
344 if ((device_dependent_mask & newMods) == 0) { | |
345 /* Revert to the old behavior */ | |
346 HandleNonDeviceModifier(keyboard, scancode, device_independent_mask, oldMods, newMods, left_key_sym); | |
347 return; | |
348 } | |
349 | |
350 /* XOR the previous state against the new state to see if there's a change */ | |
351 diff_mod = (device_dependent_mask & oldMods) ^ | |
352 (device_dependent_mask & newMods); | |
353 if (diff_mod) { | |
354 /* A change in state was found. Isolate the left and right bits | |
355 * to handle them separately just in case the values can simulataneously | |
356 * change or if the bits don't both exist. | |
357 */ | |
358 if (left_device_dependent_mask & diff_mod) { | |
359 HandleModifierOneSide(keyboard, scancode, oldMods, newMods, left_key_sym, left_device_dependent_mask); | |
360 } | |
361 if (right_device_dependent_mask & diff_mod) { | |
362 HandleModifierOneSide(keyboard, scancode, oldMods, newMods, right_key_sym, right_device_dependent_mask); | |
363 } | |
364 } | |
365 } | |
366 | |
367 /* This is a helper function for DoSidedModifiers. | |
368 * This function will release a key press in the case that | |
369 * it is clear that the modifier has been released (i.e. one side | |
370 * can't still be down). | |
371 */ | |
372 static void | |
373 ReleaseModifierSide(int keyboard, unsigned short scancode, | |
374 unsigned int device_independent_mask, | |
375 unsigned int oldMods, unsigned int newMods, | |
376 SDLKey left_key_sym, | |
377 SDLKey right_key_sym, | |
378 unsigned int left_device_dependent_mask, | |
379 unsigned int right_device_dependent_mask) | |
380 { | |
381 unsigned int device_dependent_mask = (left_device_dependent_mask | | |
382 right_device_dependent_mask); | |
383 | |
384 /* On the basis that the device independent mask is set, but there are | |
385 * no device dependent flags set, we'll assume that we can't detect this | |
386 * keyboard and revert to the unsided behavior. | |
387 */ | |
388 if ((device_dependent_mask & oldMods) == 0) { | |
389 /* In this case, we can't detect the keyboard, so use the left side | |
390 * to represent both, and release it. | |
391 */ | |
392 SDL_SendKeyboardKey(keyboard, SDL_RELEASED, (Uint8)scancode, left_key_sym); | |
393 return; | |
394 } | |
395 | |
396 /* | |
397 * This could have been done in an if-else case because at this point, | |
398 * we know that all keys have been released when calling this function. | |
399 * But I'm being paranoid so I want to handle each separately, | |
400 * so I hope this doesn't cause other problems. | |
401 */ | |
402 if ( left_device_dependent_mask & oldMods ) { | |
403 SDL_SendKeyboardKey(keyboard, SDL_RELEASED, (Uint8)scancode, left_key_sym); | |
404 } | |
405 if ( right_device_dependent_mask & oldMods ) { | |
406 SDL_SendKeyboardKey(keyboard, SDL_RELEASED, (Uint8)scancode, right_key_sym); | |
407 } | |
408 } | |
409 | |
410 /* This is a helper function for DoSidedModifiers. | |
411 * This function handles the CapsLock case. | |
412 */ | |
413 static void | |
414 HandleCapsLock(int keyboard, unsigned short scancode, | |
415 unsigned int oldMods, unsigned int newMods) | |
416 { | |
417 unsigned int oldMask, newMask; | |
418 | |
419 oldMask = oldMods & NSAlphaShiftKeyMask; | |
420 newMask = newMods & NSAlphaShiftKeyMask; | |
421 | |
422 if (oldMask != newMask) { | |
423 SDL_SendKeyboardKey(keyboard, SDL_PRESSED, (Uint8)scancode, SDLK_CAPSLOCK); | |
424 SDL_SendKeyboardKey(keyboard, SDL_RELEASED, (Uint8)scancode, SDLK_CAPSLOCK); | |
425 } | |
426 } | |
427 | |
428 /* This function will handle the modifier keys and also determine the | |
429 * correct side of the key. | |
430 */ | |
431 static void | |
432 DoSidedModifiers(int keyboard, unsigned short scancode, | |
433 unsigned int oldMods, unsigned int newMods) | |
434 { | |
435 /* Set up arrays for the key syms for the left and right side. */ | |
436 const SDLKey left_mapping[] = { SDLK_LSHIFT, SDLK_LCTRL, SDLK_LALT, SDLK_LMETA }; | |
437 const SDLKey right_mapping[] = { SDLK_RSHIFT, SDLK_RCTRL, SDLK_RALT, SDLK_RMETA }; | |
438 /* Set up arrays for the device dependent masks with indices that | |
439 * correspond to the _mapping arrays | |
440 */ | |
441 const unsigned int left_device_mapping[] = { NX_DEVICELSHIFTKEYMASK, NX_DEVICELCTLKEYMASK, NX_DEVICELALTKEYMASK, NX_DEVICELCMDKEYMASK }; | |
442 const unsigned int right_device_mapping[] = { NX_DEVICERSHIFTKEYMASK, NX_DEVICERCTLKEYMASK, NX_DEVICERALTKEYMASK, NX_DEVICERCMDKEYMASK }; | |
443 | |
444 unsigned int i, bit; | |
445 | |
446 /* Handle CAPSLOCK separately because it doesn't have a left/right side */ | |
447 HandleCapsLock(keyboard, scancode, oldMods, newMods); | |
448 | |
449 /* Iterate through the bits, testing each against the old modifiers */ | |
450 for (i = 0, bit = NSShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) { | |
451 unsigned int oldMask, newMask; | |
452 | |
453 oldMask = oldMods & bit; | |
454 newMask = newMods & bit; | |
455 | |
456 /* If the bit is set, we must always examine it because the left | |
457 * and right side keys may alternate or both may be pressed. | |
458 */ | |
459 if (newMask) { | |
460 HandleModifierSide(keyboard, scancode, bit, oldMods, newMods, | |
461 left_mapping[i], right_mapping[i], | |
462 left_device_mapping[i], right_device_mapping[i]); | |
463 } | |
464 /* If the state changed from pressed to unpressed, we must examine | |
465 * the device dependent bits to release the correct keys. | |
466 */ | |
467 else if (oldMask && oldMask != newMask) { | |
468 ReleaseModifierSide(keyboard, scancode, bit, oldMods, newMods, | |
469 left_mapping[i], right_mapping[i], | |
470 left_device_mapping[i], right_device_mapping[i]); | |
471 } | |
472 } | |
473 } | |
474 | |
475 static void | |
476 HandleModifiers(_THIS, unsigned short scancode, unsigned int modifierFlags) | |
477 { | |
478 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; | |
479 | |
480 if (modifierFlags == data->modifierFlags) { | |
481 return; | |
482 } | |
483 | |
484 /* | |
485 * Starting with Panther (10.3.0), the ability to distinguish between | |
486 * left side and right side modifiers is available. | |
487 */ | |
488 if (data->osversion >= 0x1030) { | |
489 DoSidedModifiers(data->keyboard, scancode, data->modifierFlags, modifierFlags); | |
490 } else { | |
491 DoUnsidedModifiers(data->keyboard, scancode, data->modifierFlags, modifierFlags); | |
492 } | |
493 data->modifierFlags = modifierFlags; | |
494 } | |
27 | 495 |
28 void | 496 void |
29 Cocoa_InitKeyboard(_THIS) | 497 Cocoa_InitKeyboard(_THIS) |
30 { | 498 { |
31 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; | 499 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; |
32 SDL_Keyboard keyboard; | 500 SDL_Keyboard keyboard; |
33 | 501 |
502 InitKeymap(data->keymap); | |
503 | |
34 SDL_zero(keyboard); | 504 SDL_zero(keyboard); |
35 data->keyboard = SDL_AddKeyboard(&keyboard, -1); | 505 data->keyboard = SDL_AddKeyboard(&keyboard, -1); |
36 } | 506 } |
37 | 507 |
38 void | 508 void |
509 Cocoa_HandleKeyEvent(_THIS, NSEvent *event) | |
510 { | |
511 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; | |
512 unsigned short scancode = [event keyCode]; | |
513 const char *text; | |
514 | |
515 if (scancode >= 256) { | |
516 /* Hmm, does this ever happen? If so, need to extend the keymap... */ | |
517 return; | |
518 } | |
519 | |
520 switch ([event type]) { | |
521 case NSKeyDown: | |
522 if ([event isARepeat]) { | |
523 break; | |
524 } | |
525 printf("NSKeyDown: %x, %x\n", scancode, [event modifierFlags]); | |
526 SDL_SendKeyboardKey(data->keyboard, SDL_PRESSED, (Uint8)scancode, | |
527 data->keymap[scancode]); | |
528 text = [[event characters] UTF8String]; | |
529 if(text && *text) { | |
530 SDL_SendKeyboardText(data->keyboard, text); | |
531 } | |
532 break; | |
533 case NSKeyUp: | |
534 printf("NSKeyUp: %x, %x\n", scancode, [event modifierFlags]); | |
535 SDL_SendKeyboardKey(data->keyboard, SDL_RELEASED, (Uint8)scancode, | |
536 data->keymap[scancode]); | |
537 break; | |
538 case NSFlagsChanged: | |
539 printf("NSFlagsChanged: %x, %x\n", scancode, [event modifierFlags]); | |
540 HandleModifiers(_this, scancode, [event modifierFlags]); | |
541 break; | |
542 } | |
543 } | |
544 | |
545 void | |
39 Cocoa_QuitKeyboard(_THIS) | 546 Cocoa_QuitKeyboard(_THIS) |
40 { | 547 { |
41 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; | 548 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; |
42 | 549 |
43 SDL_DelKeyboard(data->keyboard); | 550 SDL_DelKeyboard(data->keyboard); |