Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
923 lines (863 loc) · 19.4 KB

SDL_keyboard.c

File metadata and controls

923 lines (863 loc) · 19.4 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
3
Copyright (C) 1997-2011 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
26
/* General keyboard handling code for SDL */
#include "SDL_timer.h"
Feb 10, 2006
Feb 10, 2006
27
#include "SDL_events.h"
Apr 26, 2001
Apr 26, 2001
28
#include "SDL_events_c.h"
Jan 28, 2011
Jan 28, 2011
29
#include "../video/SDL_sysvideo.h"
Apr 26, 2001
Apr 26, 2001
30
31
Jul 10, 2006
Jul 10, 2006
32
/* Global keyboard information */
May 10, 2010
May 10, 2010
33
34
35
36
37
38
39
40
41
typedef struct SDL_Keyboard SDL_Keyboard;
struct SDL_Keyboard
{
/* Data common to all keyboards */
SDL_Window *focus;
Uint16 modstate;
Uint8 keystate[SDL_NUM_SCANCODES];
Feb 7, 2011
Feb 7, 2011
42
SDL_Keycode keymap[SDL_NUM_SCANCODES];
May 10, 2010
May 10, 2010
43
44
45
};
static SDL_Keyboard SDL_keyboard;
Apr 26, 2001
Apr 26, 2001
46
Feb 7, 2011
Feb 7, 2011
47
static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES] = {
Feb 5, 2008
Feb 5, 2008
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
0, 0, 0, 0,
'a',
'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',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'0',
SDLK_RETURN,
SDLK_ESCAPE,
SDLK_BACKSPACE,
SDLK_TAB,
SDLK_SPACE,
'-',
'=',
'[',
']',
'\\',
'#',
';',
'\'',
'`',
',',
'.',
'/',
SDLK_CAPSLOCK,
SDLK_F1,
SDLK_F2,
SDLK_F3,
SDLK_F4,
SDLK_F5,
S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WW,
Feb 5, 2008
Feb 5, 2008
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
SDLK_MAIL,
SDLK_CALCULATOR,
SDLK_COMPUTER,
SDLK_AC_SEARCH,
SDLK_AC_HOME,
SDLK_AC_BACK,
SDLK_AC_FORWARD,
SDLK_AC_STOP,
SDLK_AC_REFRESH,
SDLK_AC_BOOKMARKS,
SDLK_BRIGHTNESSDOWN,
SDLK_BRIGHTNESSUP,
SDLK_DISPLAYSWITCH,
SDLK_KBDILLUMTOGGLE,
SDLK_KBDILLUMDOWN,
SDLK_KBDILLUMUP,
SDLK_EJECT,
SDLK_SLEEP,
};
static const char *SDL_scancode_names[SDL_NUM_SCANCODES] = {
NULL, NULL, NULL, NULL,
"A",
"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",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"Return",
"Escape",
"Backspace",
"Tab",
"Space",
"-",
"=",
"[",
"]",
"\\",
"#",
";",
"'",
"`",
",",
".",
"/",
"CapsLock",
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"F10",
"F11",
"F12",
"PrintScreen",
"ScrollLock",
"Pause",
"Insert",
"Home",
"PageUp",
"Delete",
"End",
"PageDown",
"Right",
"Left",
"Down",
"Up",
"Numlock",
"Keypad /",
"Keypad *",
"Keypad -",
"Keypad +",
"Keypad Enter",
"Keypad 1",
"Keypad 2",
"Keypad 3",
"Keypad 4",
"Keypad 5",
"Keypad 6",
"Keypad 7",
"Keypad 8",
"Keypad 9",
"Keypad 0",
"Keypad .",
NULL,
"Application",
"Power",
"Keypad =",
"F13",
"F14",
"F15",
"F16",
"F17",
"F18",
"F19",
"F20",
"F21",
"F22",
"F23",
"F24",
"Execute",
"Help",
"Menu",
"Select",
"Stop",
"Again",
"Undo",
"Cut",
"Copy",
"Paste",
"Find",
"Mute",
"VolumeUp",
"VolumeDown",
NULL, NULL, NULL,
"Keypad ,",
"Keypad = (AS400)",
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL,
"AltErase",
"SysReq",
"Cancel",
"Clear",
"Prior",
"Return",
"Separator",
"Out",
"Oper",
"Clear / Again",
"CrSel",
"ExSel",
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
"Keypad 00",
"Keypad 000",
"ThousandsSeparator",
"DecimalSeparator",
"CurrencyUnit",
"CurrencySubUnit",
"Keypad (",
"Keypad )",
"Keypad {",
"Keypad }",
"Keypad Tab",
"Keypad Backspace",
"Keypad A",
"Keypad B",
"Keypad C",
"Keypad D",
"Keypad E",
"Keypad F",
"Keypad XOR",
"Keypad ^",
"Keypad %",
"Keypad <",
"Keypad >",
"Keypad &",
"Keypad &&",
"Keypad |",
"Keypad ||",
"Keypad :",
"Keypad #",
"Keypad Space",
"Keypad @",
"Keypad !",
"Keypad MemStore",
"Keypad MemRecall",
"Keypad MemClear",
"Keypad MemAdd",
"Keypad MemSubtract",
"Keypad MemMultiply",
"Keypad MemDivide",
"Keypad +/-",
"Keypad Clear",
"Keypad ClearEntry",
"Keypad Binary",
"Keypad Octal",
"Keypad Decimal",
"Keypad Hexadecimal",
NULL, NULL,
"Left Ctrl",
"Left Shift",
"Left Alt",
"Left GUI",
"Right Ctrl",
"Right Shift",
"Right Alt",
"Right GUI",
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL,
"ModeSwitch",
"AudioNext",
"AudioPrev",
"AudioStop",
"AudioPlay",
"AudioMute",
"MediaSelect",
Feb 7, 2008
Feb 7, 2008
487
"WWW",
Feb 5, 2008
Feb 5, 2008
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"Mail",
"Calculator",
"Computer",
"AC Search",
"AC Home",
"AC Back",
"AC Forward",
"AC Stop",
"AC Refresh",
"AC Bookmarks",
"BrightnessDown",
"BrightnessUp",
"DisplaySwitch",
"KBDIllumToggle",
"KBDIllumDown",
"KBDIllumUp",
"Eject",
"Sleep",
};
Jan 8, 2008
Jan 8, 2008
508
/* Taken from SDL_iconv() */
Feb 5, 2008
Feb 5, 2008
509
510
static char *
SDL_UCS4ToUTF8(Uint32 ch, char *dst)
Jan 8, 2008
Jan 8, 2008
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
{
Uint8 *p = (Uint8 *) dst;
if (ch <= 0x7F) {
*p = (Uint8) ch;
++dst;
} else if (ch <= 0x7FF) {
p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
p[1] = 0x80 | (Uint8) (ch & 0x3F);
dst += 2;
} else if (ch <= 0xFFFF) {
p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[2] = 0x80 | (Uint8) (ch & 0x3F);
dst += 3;
} else if (ch <= 0x1FFFFF) {
p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[3] = 0x80 | (Uint8) (ch & 0x3F);
dst += 4;
} else if (ch <= 0x3FFFFFF) {
p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[4] = 0x80 | (Uint8) (ch & 0x3F);
dst += 5;
} else {
p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[5] = 0x80 | (Uint8) (ch & 0x3F);
dst += 6;
}
return dst;
}
Jul 10, 2006
Jul 10, 2006
550
551
552
553
/* Public functions */
int
SDL_KeyboardInit(void)
{
Jul 8, 2010
Jul 8, 2010
554
555
556
557
SDL_Keyboard *keyboard = &SDL_keyboard;
/* Set the default keymap */
SDL_memcpy(keyboard->keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
Jul 10, 2006
Jul 10, 2006
558
559
return (0);
}
Apr 26, 2001
Apr 26, 2001
560
Jul 10, 2006
Jul 10, 2006
561
void
May 10, 2010
May 10, 2010
562
SDL_ResetKeyboard(void)
Apr 26, 2001
Apr 26, 2001
563
{
May 10, 2010
May 10, 2010
564
SDL_Keyboard *keyboard = &SDL_keyboard;
Feb 7, 2011
Feb 7, 2011
565
SDL_Scancode scancode;
Jul 10, 2006
Jul 10, 2006
566
Feb 5, 2008
Feb 5, 2008
567
568
for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) {
if (keyboard->keystate[scancode] == SDL_PRESSED) {
May 10, 2010
May 10, 2010
569
SDL_SendKeyboardKey(SDL_RELEASED, scancode);
Jul 10, 2006
Jul 10, 2006
570
571
}
}
Apr 26, 2001
Apr 26, 2001
572
573
}
Jul 10, 2006
Jul 10, 2006
574
void
Feb 7, 2011
Feb 7, 2011
575
SDL_GetDefaultKeymap(SDL_Keycode * keymap)
Apr 26, 2001
Apr 26, 2001
576
{
Feb 5, 2008
Feb 5, 2008
577
SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
Apr 26, 2001
Apr 26, 2001
578
579
}
Jul 10, 2006
Jul 10, 2006
580
void
Feb 7, 2011
Feb 7, 2011
581
SDL_SetKeymap(int start, SDL_Keycode * keys, int length)
Apr 26, 2001
Apr 26, 2001
582
{
May 10, 2010
May 10, 2010
583
SDL_Keyboard *keyboard = &SDL_keyboard;
Aug 19, 2007
Aug 19, 2007
584
Feb 5, 2008
Feb 5, 2008
585
586
if (start < 0 || start + length > SDL_NUM_SCANCODES) {
return;
Jul 10, 2006
Jul 10, 2006
587
}
Aug 19, 2007
Aug 19, 2007
588
Feb 5, 2008
Feb 5, 2008
589
SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length);
Jul 10, 2006
Jul 10, 2006
590
591
}
Aug 19, 2007
Aug 19, 2007
592
void
Feb 7, 2011
Feb 7, 2011
593
SDL_SetScancodeName(SDL_Scancode scancode, const char *name)
Aug 19, 2007
Aug 19, 2007
594
{
Feb 5, 2008
Feb 5, 2008
595
SDL_scancode_names[scancode] = name;
Aug 19, 2007
Aug 19, 2007
596
597
}
May 10, 2010
May 10, 2010
598
599
SDL_Window *
SDL_GetKeyboardFocus(void)
Jul 10, 2006
Jul 10, 2006
600
{
May 10, 2010
May 10, 2010
601
SDL_Keyboard *keyboard = &SDL_keyboard;
Jul 10, 2006
Jul 10, 2006
602
May 10, 2010
May 10, 2010
603
604
605
606
607
608
609
return keyboard->focus;
}
void
SDL_SetKeyboardFocus(SDL_Window * window)
{
SDL_Keyboard *keyboard = &SDL_keyboard;
Jul 10, 2006
Jul 10, 2006
610
611
/* See if the current window has lost focus */
Jan 21, 2010
Jan 21, 2010
612
if (keyboard->focus && keyboard->focus != window) {
May 10, 2010
May 10, 2010
613
614
SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST,
0, 0);
Sep 19, 2010
Sep 19, 2010
615
616
617
618
619
620
621
622
/* Ensures IME compositions are committed */
if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
SDL_VideoDevice *video = SDL_GetVideoDevice();
if (video && video->StopTextInput) {
video->StopTextInput(video);
}
}
Jul 10, 2006
Jul 10, 2006
623
624
}
Jan 21, 2010
Jan 21, 2010
625
keyboard->focus = window;
Jul 10, 2006
Jul 10, 2006
626
627
if (keyboard->focus) {
Dec 1, 2009
Dec 1, 2009
628
629
SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_GAINED,
0, 0);
Apr 16, 2010
Apr 16, 2010
630
631
if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
Sep 19, 2010
Sep 19, 2010
632
633
634
635
SDL_VideoDevice *video = SDL_GetVideoDevice();
if (video && video->StartTextInput) {
video->StartTextInput(video);
}
Apr 16, 2010
Apr 16, 2010
636
}
Jul 10, 2006
Jul 10, 2006
637
638
}
}
Apr 26, 2001
Apr 26, 2001
639
Jul 10, 2006
Jul 10, 2006
640
int
Feb 7, 2011
Feb 7, 2011
641
SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
Jul 10, 2006
Jul 10, 2006
642
{
May 10, 2010
May 10, 2010
643
SDL_Keyboard *keyboard = &SDL_keyboard;
Jun 16, 2007
Jun 16, 2007
644
int posted;
Jul 10, 2006
Jul 10, 2006
645
Uint16 modstate;
Mar 25, 2010
Mar 25, 2010
646
Uint32 type;
Jul 22, 2010
Jul 22, 2010
647
Uint8 repeat;
Jul 10, 2006
Jul 10, 2006
648
May 10, 2010
May 10, 2010
649
if (!scancode) {
Jul 10, 2006
Jul 10, 2006
650
651
return 0;
}
Apr 26, 2001
Apr 26, 2001
652
#if 0
Feb 5, 2008
Feb 5, 2008
653
printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
Jul 10, 2006
Jul 10, 2006
654
state == SDL_PRESSED ? "pressed" : "released");
Apr 26, 2001
Apr 26, 2001
655
#endif
Jul 10, 2006
Jul 10, 2006
656
657
if (state == SDL_PRESSED) {
modstate = keyboard->modstate;
Feb 5, 2008
Feb 5, 2008
658
659
switch (scancode) {
case SDL_SCANCODE_NUMLOCKCLEAR:
Jul 10, 2006
Jul 10, 2006
660
661
keyboard->modstate ^= KMOD_NUM;
break;
Feb 5, 2008
Feb 5, 2008
662
case SDL_SCANCODE_CAPSLOCK:
Jul 10, 2006
Jul 10, 2006
663
664
keyboard->modstate ^= KMOD_CAPS;
break;
Feb 5, 2008
Feb 5, 2008
665
case SDL_SCANCODE_LCTRL:
Jul 10, 2006
Jul 10, 2006
666
667
keyboard->modstate |= KMOD_LCTRL;
break;
Feb 5, 2008
Feb 5, 2008
668
case SDL_SCANCODE_RCTRL:
Jul 10, 2006
Jul 10, 2006
669
670
keyboard->modstate |= KMOD_RCTRL;
break;
Feb 5, 2008
Feb 5, 2008
671
case SDL_SCANCODE_LSHIFT:
Jul 10, 2006
Jul 10, 2006
672
673
keyboard->modstate |= KMOD_LSHIFT;
break;
Feb 5, 2008
Feb 5, 2008
674
case SDL_SCANCODE_RSHIFT:
Jul 10, 2006
Jul 10, 2006
675
676
keyboard->modstate |= KMOD_RSHIFT;
break;
Feb 5, 2008
Feb 5, 2008
677
case SDL_SCANCODE_LALT:
Jul 10, 2006
Jul 10, 2006
678
679
keyboard->modstate |= KMOD_LALT;
break;
Feb 5, 2008
Feb 5, 2008
680
case SDL_SCANCODE_RALT:
Jul 10, 2006
Jul 10, 2006
681
682
keyboard->modstate |= KMOD_RALT;
break;
Feb 5, 2008
Feb 5, 2008
683
684
case SDL_SCANCODE_LGUI:
keyboard->modstate |= KMOD_LGUI;
Jul 10, 2006
Jul 10, 2006
685
break;
Feb 5, 2008
Feb 5, 2008
686
687
case SDL_SCANCODE_RGUI:
keyboard->modstate |= KMOD_RGUI;
Jul 10, 2006
Jul 10, 2006
688
break;
Feb 5, 2008
Feb 5, 2008
689
case SDL_SCANCODE_MODE:
Jul 10, 2006
Jul 10, 2006
690
691
692
693
694
695
keyboard->modstate |= KMOD_MODE;
break;
default:
break;
}
} else {
Feb 5, 2008
Feb 5, 2008
696
697
698
switch (scancode) {
case SDL_SCANCODE_NUMLOCKCLEAR:
case SDL_SCANCODE_CAPSLOCK:
Jul 10, 2006
Jul 10, 2006
699
break;
Feb 5, 2008
Feb 5, 2008
700
case SDL_SCANCODE_LCTRL:
Jul 10, 2006
Jul 10, 2006
701
702
keyboard->modstate &= ~KMOD_LCTRL;
break;
Feb 5, 2008
Feb 5, 2008
703
case SDL_SCANCODE_RCTRL:
Jul 10, 2006
Jul 10, 2006
704
705
keyboard->modstate &= ~KMOD_RCTRL;
break;
Feb 5, 2008
Feb 5, 2008
706
case SDL_SCANCODE_LSHIFT:
Jul 10, 2006
Jul 10, 2006
707
708
keyboard->modstate &= ~KMOD_LSHIFT;
break;
Feb 5, 2008
Feb 5, 2008
709
case SDL_SCANCODE_RSHIFT:
Jul 10, 2006
Jul 10, 2006
710
711
keyboard->modstate &= ~KMOD_RSHIFT;
break;
Feb 5, 2008
Feb 5, 2008
712
case SDL_SCANCODE_LALT:
Jul 10, 2006
Jul 10, 2006
713
714
keyboard->modstate &= ~KMOD_LALT;
break;
Feb 5, 2008
Feb 5, 2008
715
case SDL_SCANCODE_RALT:
Jul 10, 2006
Jul 10, 2006
716
717
keyboard->modstate &= ~KMOD_RALT;
break;
Feb 5, 2008
Feb 5, 2008
718
719
case SDL_SCANCODE_LGUI:
keyboard->modstate &= ~KMOD_LGUI;
Jul 10, 2006
Jul 10, 2006
720
break;
Feb 5, 2008
Feb 5, 2008
721
722
case SDL_SCANCODE_RGUI:
keyboard->modstate &= ~KMOD_RGUI;
Jul 10, 2006
Jul 10, 2006
723
break;
Feb 5, 2008
Feb 5, 2008
724
case SDL_SCANCODE_MODE:
Jul 10, 2006
Jul 10, 2006
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
keyboard->modstate &= ~KMOD_MODE;
break;
default:
break;
}
modstate = keyboard->modstate;
}
/* Figure out what type of event this is */
switch (state) {
case SDL_PRESSED:
type = SDL_KEYDOWN;
break;
case SDL_RELEASED:
type = SDL_KEYUP;
break;
default:
/* Invalid state -- bail */
return 0;
}
Feb 5, 2008
Feb 5, 2008
746
/* Drop events that don't change state */
Jul 22, 2010
Jul 22, 2010
747
repeat = (state && keyboard->keystate[scancode]);
Jul 21, 2010
Jul 21, 2010
748
if (keyboard->keystate[scancode] == state && !repeat) {
Jan 29, 2006
Jan 29, 2006
749
#if 0
Feb 5, 2008
Feb 5, 2008
750
printf("Keyboard event didn't change state - dropped!\n");
Jan 29, 2006
Jan 29, 2006
751
#endif
Feb 5, 2008
Feb 5, 2008
752
return 0;
Jul 10, 2006
Jul 10, 2006
753
754
}
Feb 5, 2008
Feb 5, 2008
755
756
757
/* Update internal keyboard state */
keyboard->keystate[scancode] = state;
Jul 10, 2006
Jul 10, 2006
758
759
/* Post the event, if desired */
posted = 0;
Mar 25, 2010
Mar 25, 2010
760
if (SDL_GetEventState(type) == SDL_ENABLE) {
Jul 10, 2006
Jul 10, 2006
761
762
763
SDL_Event event;
event.key.type = type;
event.key.state = state;
Jul 22, 2010
Jul 22, 2010
764
event.key.repeat = repeat;
Jul 10, 2006
Jul 10, 2006
765
event.key.keysym.scancode = scancode;
Feb 5, 2008
Feb 5, 2008
766
event.key.keysym.sym = keyboard->keymap[scancode];
Jul 10, 2006
Jul 10, 2006
767
768
event.key.keysym.mod = modstate;
event.key.keysym.unicode = 0;
Apr 20, 2010
Apr 20, 2010
769
event.key.windowID = keyboard->focus ? keyboard->focus->id : 0;
Jul 10, 2006
Jul 10, 2006
770
771
772
773
774
775
posted = (SDL_PushEvent(&event) > 0);
}
return (posted);
}
int
May 10, 2010
May 10, 2010
776
SDL_SendKeyboardText(const char *text)
Jul 10, 2006
Jul 10, 2006
777
{
May 10, 2010
May 10, 2010
778
SDL_Keyboard *keyboard = &SDL_keyboard;
Jul 10, 2006
Jul 10, 2006
779
780
int posted;
Jul 18, 2010
Jul 18, 2010
781
/* Don't post text events for unprintable characters */
Jul 23, 2010
Jul 23, 2010
782
if ((unsigned char)*text < ' ' || *text == 127) {
Jul 18, 2010
Jul 18, 2010
783
784
785
return 0;
}
Jul 10, 2006
Jul 10, 2006
786
787
/* Post the event, if desired */
posted = 0;
Mar 25, 2010
Mar 25, 2010
788
if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
Jul 10, 2006
Jul 10, 2006
789
790
SDL_Event event;
event.text.type = SDL_TEXTINPUT;
Apr 16, 2010
Apr 16, 2010
791
event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
Jul 13, 2010
Jul 13, 2010
792
SDL_utf8strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
Jul 10, 2006
Jul 10, 2006
793
794
795
posted = (SDL_PushEvent(&event) > 0);
}
return (posted);
Apr 26, 2001
Apr 26, 2001
796
797
}
Sep 19, 2009
Sep 19, 2009
798
int
May 10, 2010
May 10, 2010
799
SDL_SendEditingText(const char *text, int start, int length)
Sep 19, 2009
Sep 19, 2009
800
{
May 10, 2010
May 10, 2010
801
SDL_Keyboard *keyboard = &SDL_keyboard;
Sep 19, 2009
Sep 19, 2009
802
803
804
805
int posted;
/* Post the event, if desired */
posted = 0;
Mar 25, 2010
Mar 25, 2010
806
if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) {
Sep 19, 2009
Sep 19, 2009
807
808
SDL_Event event;
event.edit.type = SDL_TEXTEDITING;
Apr 16, 2010
Apr 16, 2010
809
event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
Sep 19, 2009
Sep 19, 2009
810
811
event.edit.start = start;
event.edit.length = length;
Jul 13, 2010
Jul 13, 2010
812
SDL_utf8strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text));
Sep 19, 2009
Sep 19, 2009
813
814
815
816
817
posted = (SDL_PushEvent(&event) > 0);
}
return (posted);
}
Feb 5, 2008
Feb 5, 2008
818
819
820
821
822
823
824
825
void
SDL_KeyboardQuit(void)
{
}
Uint8 *
SDL_GetKeyboardState(int *numkeys)
{
May 10, 2010
May 10, 2010
826
SDL_Keyboard *keyboard = &SDL_keyboard;
Feb 5, 2008
Feb 5, 2008
827
828
829
830
831
832
833
if (numkeys != (int *) 0) {
*numkeys = SDL_NUM_SCANCODES;
}
return keyboard->keystate;
}
Feb 7, 2011
Feb 7, 2011
834
SDL_Keymod
Feb 5, 2008
Feb 5, 2008
835
836
SDL_GetModState(void)
{
May 10, 2010
May 10, 2010
837
SDL_Keyboard *keyboard = &SDL_keyboard;
Feb 5, 2008
Feb 5, 2008
838
839
840
841
842
return keyboard->modstate;
}
void
Feb 7, 2011
Feb 7, 2011
843
SDL_SetModState(SDL_Keymod modstate)
Feb 5, 2008
Feb 5, 2008
844
{
May 10, 2010
May 10, 2010
845
SDL_Keyboard *keyboard = &SDL_keyboard;
Feb 5, 2008
Feb 5, 2008
846
847
848
849
keyboard->modstate = modstate;
}
Feb 7, 2011
Feb 7, 2011
850
SDL_Keycode
Feb 7, 2011
Feb 7, 2011
851
SDL_GetKeyFromScancode(SDL_Scancode scancode)
Feb 5, 2008
Feb 5, 2008
852
{
May 10, 2010
May 10, 2010
853
SDL_Keyboard *keyboard = &SDL_keyboard;
Feb 5, 2008
Feb 5, 2008
854
855
856
857
return keyboard->keymap[scancode];
}
Feb 7, 2011
Feb 7, 2011
858
SDL_Scancode
Feb 7, 2011
Feb 7, 2011
859
SDL_GetScancodeFromKey(SDL_Keycode key)
Feb 5, 2008
Feb 5, 2008
860
{
May 10, 2010
May 10, 2010
861
SDL_Keyboard *keyboard = &SDL_keyboard;
Feb 7, 2011
Feb 7, 2011
862
SDL_Scancode scancode;
Feb 5, 2008
Feb 5, 2008
863
864
865
866
867
868
869
870
871
872
873
for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
++scancode) {
if (keyboard->keymap[scancode] == key) {
return scancode;
}
}
return SDL_SCANCODE_UNKNOWN;
}
const char *
Feb 7, 2011
Feb 7, 2011
874
SDL_GetScancodeName(SDL_Scancode scancode)
Feb 5, 2008
Feb 5, 2008
875
876
877
878
879
880
881
882
883
884
{
const char *name = SDL_scancode_names[scancode];
if (name)
return name;
else
return "";
}
const char *
Feb 7, 2011
Feb 7, 2011
885
SDL_GetKeyName(SDL_Keycode key)
Feb 5, 2008
Feb 5, 2008
886
887
888
889
890
891
{
static char name[8];
char *end;
if (key & SDLK_SCANCODE_MASK) {
return
Feb 7, 2011
Feb 7, 2011
892
SDL_GetScancodeName((SDL_Scancode) (key & ~SDLK_SCANCODE_MASK));
Feb 5, 2008
Feb 5, 2008
893
894
}
Jan 4, 2009
Jan 4, 2009
895
896
switch (key) {
case SDLK_RETURN:
Jan 4, 2009
Jan 4, 2009
897
return SDL_GetScancodeName(SDL_SCANCODE_RETURN);
Jan 4, 2009
Jan 4, 2009
898
case SDLK_ESCAPE:
Jan 4, 2009
Jan 4, 2009
899
return SDL_GetScancodeName(SDL_SCANCODE_ESCAPE);
Jan 4, 2009
Jan 4, 2009
900
case SDLK_BACKSPACE:
Jan 4, 2009
Jan 4, 2009
901
return SDL_GetScancodeName(SDL_SCANCODE_BACKSPACE);
Jan 4, 2009
Jan 4, 2009
902
case SDLK_TAB:
Jan 4, 2009
Jan 4, 2009
903
return SDL_GetScancodeName(SDL_SCANCODE_TAB);
Jan 4, 2009
Jan 4, 2009
904
case SDLK_SPACE:
Jan 4, 2009
Jan 4, 2009
905
return SDL_GetScancodeName(SDL_SCANCODE_SPACE);
Jan 4, 2009
Jan 4, 2009
906
case SDLK_DELETE:
Jan 4, 2009
Jan 4, 2009
907
return SDL_GetScancodeName(SDL_SCANCODE_DELETE);
Jan 4, 2009
Jan 4, 2009
908
909
910
911
912
913
914
915
default:
/* Unaccented letter keys on latin keyboards are normally
labeled in upper case (and probably on others like Greek or
Cyrillic too, so if you happen to know for sure, please
adapt this). */
if (key >= 'a' && key <= 'z') {
key -= 32;
}
Feb 5, 2008
Feb 5, 2008
916
Jan 4, 2009
Jan 4, 2009
917
918
919
920
end = SDL_UCS4ToUTF8((Uint32) key, name);
*end = '\0';
return name;
}
Feb 5, 2008
Feb 5, 2008
921
922
}
Jul 10, 2006
Jul 10, 2006
923
/* vi: set ts=4 sw=4 expandtab: */