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

Latest commit

 

History

History
775 lines (701 loc) · 18.9 KB

SDL_keyboard.c

File metadata and controls

775 lines (701 loc) · 18.9 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 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
29
30
31
#include "SDL_events_c.h"
#include "SDL_sysevents.h"
Jun 10, 2006
Jun 10, 2006
32
/* Global keyboard information */
Jun 11, 2006
Jun 11, 2006
33
int SDL_TranslateUNICODE = 0;
Jun 10, 2006
Jun 10, 2006
34
35
36
static int SDL_num_keyboards;
static int SDL_current_keyboard;
static SDL_Keyboard **SDL_keyboards;
Apr 26, 2001
Apr 26, 2001
37
Jun 10, 2006
Jun 10, 2006
38
static const char *SDL_keynames[SDLK_LAST]; /* Array of keycode names */
Apr 26, 2001
Apr 26, 2001
39
40
/* Public functions */
May 28, 2006
May 28, 2006
41
int
May 29, 2006
May 29, 2006
42
SDL_KeyboardInit(void)
Apr 26, 2001
Apr 26, 2001
43
{
Jun 10, 2006
Jun 10, 2006
44
int i;
May 28, 2006
May 28, 2006
45
46
/* Set default mode of UNICODE translation */
May 29, 2006
May 29, 2006
47
SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION);
May 28, 2006
May 28, 2006
48
Jun 10, 2006
Jun 10, 2006
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Initialize the tables */
for (i = 0; i < SDL_arraysize(SDL_keynames); ++i) {
switch (i) {
case SDLK_BACKSPACE:
SDL_keynames[i] = "backspace";
break;
case SDLK_TAB:
SDL_keynames[i] = "tab";
break;
case SDLK_CLEAR:
SDL_keynames[i] = "clear";
break;
case SDLK_RETURN:
SDL_keynames[i] = "return";
break;
case SDLK_PAUSE:
SDL_keynames[i] = "pause";
break;
case SDLK_ESCAPE:
SDL_keynames[i] = "escape";
break;
case SDLK_SPACE:
SDL_keynames[i] = "space";
break;
case SDLK_KP0:
SDL_keynames[i] = "[0]";
break;
case SDLK_KP1:
SDL_keynames[i] = "[1]";
break;
case SDLK_KP2:
SDL_keynames[i] = "[2]";
break;
case SDLK_KP3:
SDL_keynames[i] = "[3]";
break;
case SDLK_KP4:
SDL_keynames[i] = "[4]";
break;
case SDLK_KP5:
SDL_keynames[i] = "[5]";
break;
case SDLK_KP6:
SDL_keynames[i] = "[6]";
break;
case SDLK_KP7:
SDL_keynames[i] = "[7]";
break;
case SDLK_KP8:
SDL_keynames[i] = "[8]";
break;
case SDLK_KP9:
SDL_keynames[i] = "[9]";
break;
case SDLK_KP_PERIOD:
SDL_keynames[i] = "[.]";
break;
case SDLK_KP_DIVIDE:
SDL_keynames[i] = "[/]";
break;
case SDLK_KP_MULTIPLY:
SDL_keynames[i] = "[*]";
break;
case SDLK_KP_MINUS:
SDL_keynames[i] = "[-]";
break;
case SDLK_KP_PLUS:
SDL_keynames[i] = "[+]";
break;
case SDLK_KP_ENTER:
SDL_keynames[i] = "enter";
break;
case SDLK_KP_EQUALS:
SDL_keynames[i] = "equals";
break;
case SDLK_UP:
SDL_keynames[i] = "up";
break;
case SDLK_DOWN:
SDL_keynames[i] = "down";
break;
case SDLK_RIGHT:
SDL_keynames[i] = "right";
break;
case SDLK_LEFT:
SDL_keynames[i] = "left";
break;
case SDLK_INSERT:
SDL_keynames[i] = "insert";
break;
case SDLK_HOME:
SDL_keynames[i] = "home";
break;
case SDLK_END:
SDL_keynames[i] = "end";
break;
case SDLK_PAGEUP:
SDL_keynames[i] = "page up";
break;
case SDLK_PAGEDOWN:
SDL_keynames[i] = "page down";
break;
case SDLK_F1:
SDL_keynames[i] = "f1";
break;
case SDLK_F2:
SDL_keynames[i] = "f2";
break;
case SDLK_F3:
SDL_keynames[i] = "f3";
break;
case SDLK_F4:
SDL_keynames[i] = "f4";
break;
case SDLK_F5:
SDL_keynames[i] = "f5";
break;
case SDLK_F6:
SDL_keynames[i] = "f6";
break;
case SDLK_F7:
SDL_keynames[i] = "f7";
break;
case SDLK_F8:
SDL_keynames[i] = "f8";
break;
case SDLK_F9:
SDL_keynames[i] = "f9";
break;
case SDLK_F10:
SDL_keynames[i] = "f10";
break;
case SDLK_F11:
SDL_keynames[i] = "f11";
break;
case SDLK_F12:
SDL_keynames[i] = "f12";
break;
case SDLK_F13:
SDL_keynames[i] = "f13";
break;
case SDLK_F14:
SDL_keynames[i] = "f14";
break;
case SDLK_F15:
SDL_keynames[i] = "f15";
break;
case SDLK_NUMLOCK:
SDL_keynames[i] = "numlock";
break;
case SDLK_CAPSLOCK:
SDL_keynames[i] = "caps lock";
break;
case SDLK_SCROLLOCK:
SDL_keynames[i] = "scroll lock";
break;
case SDLK_RSHIFT:
SDL_keynames[i] = "right shift";
break;
case SDLK_LSHIFT:
SDL_keynames[i] = "left shift";
break;
case SDLK_RCTRL:
SDL_keynames[i] = "right ctrl";
break;
case SDLK_LCTRL:
SDL_keynames[i] = "left ctrl";
break;
case SDLK_RALT:
SDL_keynames[i] = "right alt";
break;
case SDLK_LALT:
SDL_keynames[i] = "left alt";
break;
case SDLK_RMETA:
SDL_keynames[i] = "right meta";
break;
case SDLK_LMETA:
SDL_keynames[i] = "left meta";
break;
case SDLK_LSUPER:
SDL_keynames[i] = "left super"; /* "Windows" keys */
break;
case SDLK_RSUPER:
SDL_keynames[i] = "right super";
break;
case SDLK_MODE:
SDL_keynames[i] = "alt gr";
break;
case SDLK_COMPOSE:
SDL_keynames[i] = "compose";
break;
case SDLK_HELP:
SDL_keynames[i] = "help";
break;
case SDLK_PRINT:
SDL_keynames[i] = "print screen";
break;
case SDLK_SYSREQ:
SDL_keynames[i] = "sys req";
break;
case SDLK_BREAK:
SDL_keynames[i] = "break";
break;
case SDLK_MENU:
SDL_keynames[i] = "menu";
break;
case SDLK_POWER:
SDL_keynames[i] = "power";
break;
case SDLK_EURO:
SDL_keynames[i] = "euro";
break;
case SDLK_UNDO:
SDL_keynames[i] = "undo";
break;
default:
SDL_keynames[i] = NULL;
break;
}
}
May 28, 2006
May 28, 2006
276
277
278
/* Done. Whew. */
return (0);
Apr 26, 2001
Apr 26, 2001
279
}
May 28, 2006
May 28, 2006
280
Jun 10, 2006
Jun 10, 2006
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
SDL_Keyboard *
SDL_GetKeyboard(int index)
{
if (index < 0 || index >= SDL_num_keyboards) {
return NULL;
}
return SDL_keyboards[index];
}
int
SDL_AddKeyboard(const SDL_Keyboard * keyboard, int index)
{
SDL_Keyboard **keyboards;
/* Add the keyboard to the list of keyboards */
if (index < 0 || index >= SDL_num_keyboards || SDL_keyboards[index]) {
keyboards =
(SDL_Keyboard **) SDL_realloc(SDL_keyboards,
(SDL_num_keyboards +
1) * sizeof(*keyboards));
if (!keyboards) {
SDL_OutOfMemory();
return -1;
}
SDL_keyboards = keyboards;
index = SDL_num_keyboards++;
}
SDL_keyboards[index] =
(SDL_Keyboard *) SDL_malloc(sizeof(*SDL_keyboards[index]));
if (!SDL_keyboards[index]) {
SDL_OutOfMemory();
return -1;
}
*SDL_keyboards[index] = *keyboard;
return index;
}
May 28, 2006
May 28, 2006
320
void
Jun 10, 2006
Jun 10, 2006
321
SDL_DelKeyboard(int index)
Aug 21, 2005
Aug 21, 2005
322
{
Jun 10, 2006
Jun 10, 2006
323
324
325
326
327
328
329
330
331
332
333
334
SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
if (!keyboard) {
return;
}
if (keyboard->FreeKeyboard) {
keyboard->FreeKeyboard(keyboard);
}
SDL_free(keyboard);
SDL_keyboards[index] = NULL;
Aug 21, 2005
Aug 21, 2005
335
}
Apr 26, 2001
Apr 26, 2001
336
May 28, 2006
May 28, 2006
337
void
Jun 10, 2006
Jun 10, 2006
338
SDL_ResetKeyboard(int index)
Apr 26, 2001
Apr 26, 2001
339
{
Jun 10, 2006
Jun 10, 2006
340
SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
Jul 6, 2006
Jul 6, 2006
341
SDLKey key;
May 28, 2006
May 28, 2006
342
Jun 10, 2006
Jun 10, 2006
343
344
345
346
if (!keyboard) {
return;
}
May 28, 2006
May 28, 2006
347
for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
Jun 10, 2006
Jun 10, 2006
348
if (keyboard->keystate[key] == SDL_PRESSED) {
Jul 6, 2006
Jul 6, 2006
349
SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key);
May 28, 2006
May 28, 2006
350
351
}
}
Jun 11, 2006
Jun 11, 2006
352
keyboard->repeat.timestamp = 0;
Jun 10, 2006
Jun 10, 2006
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
}
void
SDL_KeyboardQuit(void)
{
int i;
for (i = 0; i < SDL_num_keyboards; ++i) {
SDL_DelKeyboard(i);
}
SDL_num_keyboards = 0;
SDL_current_keyboard = 0;
if (SDL_keyboards) {
SDL_free(SDL_keyboards);
SDL_keyboards = NULL;
}
}
int
SDL_GetNumKeyboards(void)
{
return SDL_num_keyboards;
}
int
SDL_SelectKeyboard(int index)
{
if (index >= 0 && index < SDL_num_keyboards) {
SDL_current_keyboard = index;
}
return SDL_current_keyboard;
Apr 26, 2001
Apr 26, 2001
385
386
}
May 28, 2006
May 28, 2006
387
int
May 29, 2006
May 29, 2006
388
SDL_EnableUNICODE(int enable)
Apr 26, 2001
Apr 26, 2001
389
{
May 28, 2006
May 28, 2006
390
int old_mode;
Apr 26, 2001
Apr 26, 2001
391
May 28, 2006
May 28, 2006
392
393
394
395
396
old_mode = SDL_TranslateUNICODE;
if (enable >= 0) {
SDL_TranslateUNICODE = enable;
}
return (old_mode);
Apr 26, 2001
Apr 26, 2001
397
398
}
May 28, 2006
May 28, 2006
399
Uint8 *
May 29, 2006
May 29, 2006
400
SDL_GetKeyState(int *numkeys)
Apr 26, 2001
Apr 26, 2001
401
{
Jun 11, 2006
Jun 11, 2006
402
403
404
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
if (numkeys != (int *) 0) {
May 28, 2006
May 28, 2006
405
*numkeys = SDLK_LAST;
Jun 11, 2006
Jun 11, 2006
406
407
408
409
410
411
}
if (!keyboard) {
return NULL;
}
return keyboard->keystate;
Apr 26, 2001
Apr 26, 2001
412
}
May 28, 2006
May 28, 2006
413
414
SDLMod
May 29, 2006
May 29, 2006
415
SDL_GetModState(void)
Apr 26, 2001
Apr 26, 2001
416
{
Jun 11, 2006
Jun 11, 2006
417
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
Jun 10, 2006
Jun 10, 2006
418
419
420
421
422
if (!keyboard) {
return KMOD_NONE;
}
return keyboard->modstate;
Apr 26, 2001
Apr 26, 2001
423
}
May 28, 2006
May 28, 2006
424
425
void
May 29, 2006
May 29, 2006
426
SDL_SetModState(SDLMod modstate)
Apr 26, 2001
Apr 26, 2001
427
{
Jun 11, 2006
Jun 11, 2006
428
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
Jun 10, 2006
Jun 10, 2006
429
430
431
432
433
if (!keyboard) {
return;
}
keyboard->modstate = modstate;
Apr 26, 2001
Apr 26, 2001
434
435
}
Jun 10, 2006
Jun 10, 2006
436
const char *
May 29, 2006
May 29, 2006
437
SDL_GetKeyName(SDLKey key)
Apr 26, 2001
Apr 26, 2001
438
{
May 28, 2006
May 28, 2006
439
440
const char *keyname;
Jun 14, 2006
Jun 14, 2006
441
if (key < SDL_arraysize(SDL_keynames)) {
Jun 11, 2006
Jun 11, 2006
442
443
444
445
keyname = SDL_keynames[key];
} else {
keyname = NULL;
}
May 28, 2006
May 28, 2006
446
if (keyname == NULL) {
Jun 10, 2006
Jun 10, 2006
447
448
if (key < 256) {
static char temp[4];
Jun 11, 2006
Jun 11, 2006
449
450
451
452
453
454
455
char *cvt;
temp[0] = (char) key;
temp[1] = '\0';
cvt = SDL_iconv_string("UTF-8", "LATIN1", temp, 1);
SDL_strlcpy(temp, cvt, SDL_arraysize(temp));
SDL_free(cvt);
keyname = temp;
Jun 10, 2006
Jun 10, 2006
456
457
458
} else {
keyname = "unknown key";
}
May 28, 2006
May 28, 2006
459
}
Jun 10, 2006
Jun 10, 2006
460
return keyname;
Apr 26, 2001
Apr 26, 2001
461
462
}
Jul 6, 2006
Jul 6, 2006
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
void
SDL_SetKeyboardFocus(int index, SDL_WindowID windowID)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
int i;
SDL_bool focus;
if (!keyboard || (keyboard->focus == windowID)) {
return;
}
/* See if the current window has lost focus */
if (keyboard->focus) {
focus = SDL_FALSE;
for (i = 0; i < SDL_num_keyboards; ++i) {
SDL_Keyboard *check;
if (i != index) {
check = SDL_GetKeyboard(i);
if (check && check->focus == keyboard->focus) {
focus = SDL_TRUE;
break;
}
}
}
if (!focus) {
SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
}
}
keyboard->focus = windowID;
if (keyboard->focus) {
focus = SDL_FALSE;
for (i = 0; i < SDL_num_keyboards; ++i) {
SDL_Keyboard *check;
if (i != index) {
check = SDL_GetKeyboard(i);
if (check && check->focus == keyboard->focus) {
focus = SDL_TRUE;
break;
}
}
}
if (!focus) {
SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
}
}
}
May 28, 2006
May 28, 2006
512
int
Jul 6, 2006
Jul 6, 2006
513
SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key)
Apr 26, 2001
Apr 26, 2001
514
{
Jun 11, 2006
Jun 11, 2006
515
SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
May 28, 2006
May 28, 2006
516
517
int posted, repeatable;
Uint16 modstate;
Jun 11, 2006
Jun 11, 2006
518
Uint8 type;
Apr 26, 2001
Apr 26, 2001
519
Jun 11, 2006
Jun 11, 2006
520
521
522
if (!keyboard) {
return 0;
}
Apr 26, 2001
Apr 26, 2001
523
#if 0
Jul 6, 2006
Jul 6, 2006
524
printf("The '%s' key has been %s\n", SDL_GetKeyName(key),
May 29, 2006
May 29, 2006
525
state == SDL_PRESSED ? "pressed" : "released");
Apr 26, 2001
Apr 26, 2001
526
#endif
May 28, 2006
May 28, 2006
527
528
repeatable = 0;
if (state == SDL_PRESSED) {
Jul 6, 2006
Jul 6, 2006
529
530
modstate = keyboard->modstate;
switch (key) {
May 28, 2006
May 28, 2006
531
532
533
case SDLK_UNKNOWN:
break;
case SDLK_NUMLOCK:
Jul 6, 2006
Jul 6, 2006
534
keyboard->modstate ^= KMOD_NUM;
May 28, 2006
May 28, 2006
535
536
break;
case SDLK_CAPSLOCK:
Jul 6, 2006
Jul 6, 2006
537
keyboard->modstate ^= KMOD_CAPS;
May 28, 2006
May 28, 2006
538
539
break;
case SDLK_LCTRL:
Jul 6, 2006
Jul 6, 2006
540
keyboard->modstate |= KMOD_LCTRL;
May 28, 2006
May 28, 2006
541
542
break;
case SDLK_RCTRL:
Jul 6, 2006
Jul 6, 2006
543
keyboard->modstate |= KMOD_RCTRL;
May 28, 2006
May 28, 2006
544
545
break;
case SDLK_LSHIFT:
Jul 6, 2006
Jul 6, 2006
546
keyboard->modstate |= KMOD_LSHIFT;
May 28, 2006
May 28, 2006
547
548
break;
case SDLK_RSHIFT:
Jul 6, 2006
Jul 6, 2006
549
keyboard->modstate |= KMOD_RSHIFT;
May 28, 2006
May 28, 2006
550
551
break;
case SDLK_LALT:
Jul 6, 2006
Jul 6, 2006
552
keyboard->modstate |= KMOD_LALT;
May 28, 2006
May 28, 2006
553
554
break;
case SDLK_RALT:
Jul 6, 2006
Jul 6, 2006
555
keyboard->modstate |= KMOD_RALT;
May 28, 2006
May 28, 2006
556
557
break;
case SDLK_LMETA:
Jul 6, 2006
Jul 6, 2006
558
keyboard->modstate |= KMOD_LMETA;
May 28, 2006
May 28, 2006
559
560
break;
case SDLK_RMETA:
Jul 6, 2006
Jul 6, 2006
561
keyboard->modstate |= KMOD_RMETA;
May 28, 2006
May 28, 2006
562
563
break;
case SDLK_MODE:
Jul 6, 2006
Jul 6, 2006
564
keyboard->modstate |= KMOD_MODE;
May 28, 2006
May 28, 2006
565
566
567
568
569
570
break;
default:
repeatable = 1;
break;
}
} else {
Jul 6, 2006
Jul 6, 2006
571
switch (key) {
May 28, 2006
May 28, 2006
572
573
574
575
case SDLK_UNKNOWN:
break;
case SDLK_NUMLOCK:
case SDLK_CAPSLOCK:
Jul 6, 2006
Jul 6, 2006
576
break;
May 28, 2006
May 28, 2006
577
case SDLK_LCTRL:
Jul 6, 2006
Jul 6, 2006
578
keyboard->modstate &= ~KMOD_LCTRL;
May 28, 2006
May 28, 2006
579
580
break;
case SDLK_RCTRL:
Jul 6, 2006
Jul 6, 2006
581
keyboard->modstate &= ~KMOD_RCTRL;
May 28, 2006
May 28, 2006
582
583
break;
case SDLK_LSHIFT:
Jul 6, 2006
Jul 6, 2006
584
keyboard->modstate &= ~KMOD_LSHIFT;
May 28, 2006
May 28, 2006
585
586
break;
case SDLK_RSHIFT:
Jul 6, 2006
Jul 6, 2006
587
keyboard->modstate &= ~KMOD_RSHIFT;
May 28, 2006
May 28, 2006
588
589
break;
case SDLK_LALT:
Jul 6, 2006
Jul 6, 2006
590
keyboard->modstate &= ~KMOD_LALT;
May 28, 2006
May 28, 2006
591
592
break;
case SDLK_RALT:
Jul 6, 2006
Jul 6, 2006
593
keyboard->modstate &= ~KMOD_RALT;
May 28, 2006
May 28, 2006
594
595
break;
case SDLK_LMETA:
Jul 6, 2006
Jul 6, 2006
596
keyboard->modstate &= ~KMOD_LMETA;
May 28, 2006
May 28, 2006
597
598
break;
case SDLK_RMETA:
Jul 6, 2006
Jul 6, 2006
599
keyboard->modstate &= ~KMOD_RMETA;
May 28, 2006
May 28, 2006
600
601
break;
case SDLK_MODE:
Jul 6, 2006
Jul 6, 2006
602
keyboard->modstate &= ~KMOD_MODE;
May 28, 2006
May 28, 2006
603
604
605
606
break;
default:
break;
}
Jul 6, 2006
Jul 6, 2006
607
modstate = keyboard->modstate;
May 28, 2006
May 28, 2006
608
609
610
611
612
}
/* Figure out what type of event this is */
switch (state) {
case SDL_PRESSED:
Jun 11, 2006
Jun 11, 2006
613
type = SDL_KEYDOWN;
May 28, 2006
May 28, 2006
614
615
break;
case SDL_RELEASED:
Jun 11, 2006
Jun 11, 2006
616
type = SDL_KEYUP;
May 28, 2006
May 28, 2006
617
618
619
/*
* jk 991215 - Added
*/
Jun 11, 2006
Jun 11, 2006
620
if (keyboard->repeat.timestamp &&
Jul 6, 2006
Jul 6, 2006
621
keyboard->repeat.evt.key.keysym.sym == key) {
Jun 11, 2006
Jun 11, 2006
622
keyboard->repeat.timestamp = 0;
May 28, 2006
May 28, 2006
623
624
625
626
}
break;
default:
/* Invalid state -- bail */
Jun 11, 2006
Jun 11, 2006
627
return 0;
May 28, 2006
May 28, 2006
628
629
}
Jul 6, 2006
Jul 6, 2006
630
if (key != SDLK_UNKNOWN) {
May 28, 2006
May 28, 2006
631
/* Drop events that don't change state */
Jul 6, 2006
Jul 6, 2006
632
if (keyboard->keystate[key] == state) {
Jan 29, 2006
Jan 29, 2006
633
#if 0
May 29, 2006
May 29, 2006
634
printf("Keyboard event didn't change state - dropped!\n");
Jan 29, 2006
Jan 29, 2006
635
#endif
Jun 11, 2006
Jun 11, 2006
636
return 0;
May 28, 2006
May 28, 2006
637
638
639
}
/* Update internal keyboard state */
Jul 6, 2006
Jul 6, 2006
640
keyboard->keystate[key] = state;
May 28, 2006
May 28, 2006
641
642
643
644
}
/* Post the event, if desired */
posted = 0;
Jun 11, 2006
Jun 11, 2006
645
646
647
648
if (SDL_ProcessEvents[type] == SDL_ENABLE) {
SDL_Event event;
event.key.type = type;
event.key.which = (Uint8) index;
May 28, 2006
May 28, 2006
649
event.key.state = state;
Jul 6, 2006
Jul 6, 2006
650
651
652
653
event.key.keysym.scancode = scancode;
event.key.keysym.sym = (Uint16) key;
event.key.keysym.mod = modstate;
event.key.keysym.unicode = 0;
Jun 11, 2006
Jun 11, 2006
654
event.key.windowID = keyboard->focus;
May 28, 2006
May 28, 2006
655
656
657
/*
* jk 991215 - Added
*/
Jun 11, 2006
Jun 11, 2006
658
659
660
661
662
663
664
665
if (repeatable && (keyboard->repeat.delay != 0)) {
Uint32 timestamp = SDL_GetTicks();
if (!timestamp) {
timestamp = 1;
}
keyboard->repeat.evt = event;
keyboard->repeat.firsttime = 1;
keyboard->repeat.timestamp = 1;
May 28, 2006
May 28, 2006
666
}
Jul 8, 2006
Jul 8, 2006
667
posted = (SDL_PushEvent(&event) > 0);
May 28, 2006
May 28, 2006
668
669
}
return (posted);
Apr 26, 2001
Apr 26, 2001
670
671
}
Jul 6, 2006
Jul 6, 2006
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
int
SDL_SendKeyboardText(int index, const char *text)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
int posted;
if (!keyboard) {
return 0;
}
/* Post the event, if desired */
posted = 0;
if (SDL_ProcessEvents[SDL_TEXTINPUT] == SDL_ENABLE) {
SDL_Event event;
event.text.type = SDL_TEXTINPUT;
event.text.which = (Uint8) index;
SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
event.key.windowID = keyboard->focus;
Jul 8, 2006
Jul 8, 2006
690
posted = (SDL_PushEvent(&event) > 0);
Jul 6, 2006
Jul 6, 2006
691
692
693
694
}
return (posted);
}
Apr 26, 2001
Apr 26, 2001
695
696
697
/*
* jk 991215 - Added
*/
May 28, 2006
May 28, 2006
698
void
May 29, 2006
May 29, 2006
699
SDL_CheckKeyRepeat(void)
Apr 26, 2001
Apr 26, 2001
700
{
Jun 11, 2006
Jun 11, 2006
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
int i;
for (i = 0; i < SDL_num_keyboards; ++i) {
SDL_Keyboard *keyboard = SDL_keyboards[i];
if (!keyboard) {
continue;
}
if (keyboard->repeat.timestamp) {
Uint32 now, interval;
now = SDL_GetTicks();
interval = (now - keyboard->repeat.timestamp);
if (keyboard->repeat.firsttime) {
if (interval > (Uint32) keyboard->repeat.delay) {
keyboard->repeat.timestamp = now;
keyboard->repeat.firsttime = 0;
}
} else {
if (interval > (Uint32) keyboard->repeat.interval) {
keyboard->repeat.timestamp = now;
Jul 8, 2006
Jul 8, 2006
723
SDL_PushEvent(&keyboard->repeat.evt);
May 28, 2006
May 28, 2006
724
725
726
727
}
}
}
}
Apr 26, 2001
Apr 26, 2001
728
729
}
May 28, 2006
May 28, 2006
730
int
May 29, 2006
May 29, 2006
731
SDL_EnableKeyRepeat(int delay, int interval)
Apr 26, 2001
Apr 26, 2001
732
{
Jun 11, 2006
Jun 11, 2006
733
734
735
736
737
738
739
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
if (!keyboard) {
SDL_SetError("No keyboard is currently selected");
return -1;
}
May 28, 2006
May 28, 2006
740
if ((delay < 0) || (interval < 0)) {
May 29, 2006
May 29, 2006
741
SDL_SetError("keyboard repeat value less than zero");
Jun 11, 2006
Jun 11, 2006
742
return -1;
May 28, 2006
May 28, 2006
743
}
Jun 11, 2006
Jun 11, 2006
744
745
746
747
748
749
750
keyboard->repeat.firsttime = 0;
keyboard->repeat.delay = delay;
keyboard->repeat.interval = interval;
keyboard->repeat.timestamp = 0;
return 0;
Apr 26, 2001
Apr 26, 2001
751
752
}
May 28, 2006
May 28, 2006
753
void
May 29, 2006
May 29, 2006
754
SDL_GetKeyRepeat(int *delay, int *interval)
Mar 13, 2006
Mar 13, 2006
755
{
Jun 11, 2006
Jun 11, 2006
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
if (!keyboard) {
if (delay) {
*delay = 0;
}
if (interval) {
*interval = 0;
}
return;
}
if (delay) {
*delay = keyboard->repeat.delay;
}
if (interval) {
*interval = keyboard->repeat.interval;
}
Mar 13, 2006
Mar 13, 2006
773
774
}
May 28, 2006
May 28, 2006
775
/* vi: set ts=4 sw=4 expandtab: */