SDL_keyboard.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. /* General keyboard handling code for SDL */
  20. #include "SDL_timer.h"
  21. #include "SDL_events.h"
  22. #include "SDL_events_c.h"
  23. #include "SDL_assert.h"
  24. #include "../video/SDL_sysvideo.h"
  25. /* #define DEBUG_KEYBOARD */
  26. /* Global keyboard information */
  27. typedef struct SDL_Keyboard SDL_Keyboard;
  28. struct SDL_Keyboard
  29. {
  30. /* Data common to all keyboards */
  31. SDL_Window *focus;
  32. Uint16 modstate;
  33. Uint8 keystate[SDL_NUM_SCANCODES];
  34. SDL_Keycode keymap[SDL_NUM_SCANCODES];
  35. };
  36. static SDL_Keyboard SDL_keyboard;
  37. static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES] = {
  38. 0, 0, 0, 0,
  39. 'a',
  40. 'b',
  41. 'c',
  42. 'd',
  43. 'e',
  44. 'f',
  45. 'g',
  46. 'h',
  47. 'i',
  48. 'j',
  49. 'k',
  50. 'l',
  51. 'm',
  52. 'n',
  53. 'o',
  54. 'p',
  55. 'q',
  56. 'r',
  57. 's',
  58. 't',
  59. 'u',
  60. 'v',
  61. 'w',
  62. 'x',
  63. 'y',
  64. 'z',
  65. '1',
  66. '2',
  67. '3',
  68. '4',
  69. '5',
  70. '6',
  71. '7',
  72. '8',
  73. '9',
  74. '0',
  75. SDLK_RETURN,
  76. SDLK_ESCAPE,
  77. SDLK_BACKSPACE,
  78. SDLK_TAB,
  79. SDLK_SPACE,
  80. '-',
  81. '=',
  82. '[',
  83. ']',
  84. '\\',
  85. '#',
  86. ';',
  87. '\'',
  88. '`',
  89. ',',
  90. '.',
  91. '/',
  92. SDLK_CAPSLOCK,
  93. SDLK_F1,
  94. SDLK_F2,
  95. SDLK_F3,
  96. SDLK_F4,
  97. SDLK_F5,
  98. SDLK_F6,
  99. SDLK_F7,
  100. SDLK_F8,
  101. SDLK_F9,
  102. SDLK_F10,
  103. SDLK_F11,
  104. SDLK_F12,
  105. SDLK_PRINTSCREEN,
  106. SDLK_SCROLLLOCK,
  107. SDLK_PAUSE,
  108. SDLK_INSERT,
  109. SDLK_HOME,
  110. SDLK_PAGEUP,
  111. SDLK_DELETE,
  112. SDLK_END,
  113. SDLK_PAGEDOWN,
  114. SDLK_RIGHT,
  115. SDLK_LEFT,
  116. SDLK_DOWN,
  117. SDLK_UP,
  118. SDLK_NUMLOCKCLEAR,
  119. SDLK_KP_DIVIDE,
  120. SDLK_KP_MULTIPLY,
  121. SDLK_KP_MINUS,
  122. SDLK_KP_PLUS,
  123. SDLK_KP_ENTER,
  124. SDLK_KP_1,
  125. SDLK_KP_2,
  126. SDLK_KP_3,
  127. SDLK_KP_4,
  128. SDLK_KP_5,
  129. SDLK_KP_6,
  130. SDLK_KP_7,
  131. SDLK_KP_8,
  132. SDLK_KP_9,
  133. SDLK_KP_0,
  134. SDLK_KP_PERIOD,
  135. 0,
  136. SDLK_APPLICATION,
  137. SDLK_POWER,
  138. SDLK_KP_EQUALS,
  139. SDLK_F13,
  140. SDLK_F14,
  141. SDLK_F15,
  142. SDLK_F16,
  143. SDLK_F17,
  144. SDLK_F18,
  145. SDLK_F19,
  146. SDLK_F20,
  147. SDLK_F21,
  148. SDLK_F22,
  149. SDLK_F23,
  150. SDLK_F24,
  151. SDLK_EXECUTE,
  152. SDLK_HELP,
  153. SDLK_MENU,
  154. SDLK_SELECT,
  155. SDLK_STOP,
  156. SDLK_AGAIN,
  157. SDLK_UNDO,
  158. SDLK_CUT,
  159. SDLK_COPY,
  160. SDLK_PASTE,
  161. SDLK_FIND,
  162. SDLK_MUTE,
  163. SDLK_VOLUMEUP,
  164. SDLK_VOLUMEDOWN,
  165. 0, 0, 0,
  166. SDLK_KP_COMMA,
  167. SDLK_KP_EQUALSAS400,
  168. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  169. SDLK_ALTERASE,
  170. SDLK_SYSREQ,
  171. SDLK_CANCEL,
  172. SDLK_CLEAR,
  173. SDLK_PRIOR,
  174. SDLK_RETURN2,
  175. SDLK_SEPARATOR,
  176. SDLK_OUT,
  177. SDLK_OPER,
  178. SDLK_CLEARAGAIN,
  179. SDLK_CRSEL,
  180. SDLK_EXSEL,
  181. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  182. SDLK_KP_00,
  183. SDLK_KP_000,
  184. SDLK_THOUSANDSSEPARATOR,
  185. SDLK_DECIMALSEPARATOR,
  186. SDLK_CURRENCYUNIT,
  187. SDLK_CURRENCYSUBUNIT,
  188. SDLK_KP_LEFTPAREN,
  189. SDLK_KP_RIGHTPAREN,
  190. SDLK_KP_LEFTBRACE,
  191. SDLK_KP_RIGHTBRACE,
  192. SDLK_KP_TAB,
  193. SDLK_KP_BACKSPACE,
  194. SDLK_KP_A,
  195. SDLK_KP_B,
  196. SDLK_KP_C,
  197. SDLK_KP_D,
  198. SDLK_KP_E,
  199. SDLK_KP_F,
  200. SDLK_KP_XOR,
  201. SDLK_KP_POWER,
  202. SDLK_KP_PERCENT,
  203. SDLK_KP_LESS,
  204. SDLK_KP_GREATER,
  205. SDLK_KP_AMPERSAND,
  206. SDLK_KP_DBLAMPERSAND,
  207. SDLK_KP_VERTICALBAR,
  208. SDLK_KP_DBLVERTICALBAR,
  209. SDLK_KP_COLON,
  210. SDLK_KP_HASH,
  211. SDLK_KP_SPACE,
  212. SDLK_KP_AT,
  213. SDLK_KP_EXCLAM,
  214. SDLK_KP_MEMSTORE,
  215. SDLK_KP_MEMRECALL,
  216. SDLK_KP_MEMCLEAR,
  217. SDLK_KP_MEMADD,
  218. SDLK_KP_MEMSUBTRACT,
  219. SDLK_KP_MEMMULTIPLY,
  220. SDLK_KP_MEMDIVIDE,
  221. SDLK_KP_PLUSMINUS,
  222. SDLK_KP_CLEAR,
  223. SDLK_KP_CLEARENTRY,
  224. SDLK_KP_BINARY,
  225. SDLK_KP_OCTAL,
  226. SDLK_KP_DECIMAL,
  227. SDLK_KP_HEXADECIMAL,
  228. 0, 0,
  229. SDLK_LCTRL,
  230. SDLK_LSHIFT,
  231. SDLK_LALT,
  232. SDLK_LGUI,
  233. SDLK_RCTRL,
  234. SDLK_RSHIFT,
  235. SDLK_RALT,
  236. SDLK_RGUI,
  237. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  238. SDLK_MODE,
  239. SDLK_AUDIONEXT,
  240. SDLK_AUDIOPREV,
  241. SDLK_AUDIOSTOP,
  242. SDLK_AUDIOPLAY,
  243. SDLK_AUDIOMUTE,
  244. SDLK_MEDIASELECT,
  245. SDLK_WWW,
  246. SDLK_MAIL,
  247. SDLK_CALCULATOR,
  248. SDLK_COMPUTER,
  249. SDLK_AC_SEARCH,
  250. SDLK_AC_HOME,
  251. SDLK_AC_BACK,
  252. SDLK_AC_FORWARD,
  253. SDLK_AC_STOP,
  254. SDLK_AC_REFRESH,
  255. SDLK_AC_BOOKMARKS,
  256. SDLK_BRIGHTNESSDOWN,
  257. SDLK_BRIGHTNESSUP,
  258. SDLK_DISPLAYSWITCH,
  259. SDLK_KBDILLUMTOGGLE,
  260. SDLK_KBDILLUMDOWN,
  261. SDLK_KBDILLUMUP,
  262. SDLK_EJECT,
  263. SDLK_SLEEP,
  264. };
  265. static const char *SDL_scancode_names[SDL_NUM_SCANCODES] = {
  266. NULL, NULL, NULL, NULL,
  267. "A",
  268. "B",
  269. "C",
  270. "D",
  271. "E",
  272. "F",
  273. "G",
  274. "H",
  275. "I",
  276. "J",
  277. "K",
  278. "L",
  279. "M",
  280. "N",
  281. "O",
  282. "P",
  283. "Q",
  284. "R",
  285. "S",
  286. "T",
  287. "U",
  288. "V",
  289. "W",
  290. "X",
  291. "Y",
  292. "Z",
  293. "1",
  294. "2",
  295. "3",
  296. "4",
  297. "5",
  298. "6",
  299. "7",
  300. "8",
  301. "9",
  302. "0",
  303. "Return",
  304. "Escape",
  305. "Backspace",
  306. "Tab",
  307. "Space",
  308. "-",
  309. "=",
  310. "[",
  311. "]",
  312. "\\",
  313. "#",
  314. ";",
  315. "'",
  316. "`",
  317. ",",
  318. ".",
  319. "/",
  320. "CapsLock",
  321. "F1",
  322. "F2",
  323. "F3",
  324. "F4",
  325. "F5",
  326. "F6",
  327. "F7",
  328. "F8",
  329. "F9",
  330. "F10",
  331. "F11",
  332. "F12",
  333. "PrintScreen",
  334. "ScrollLock",
  335. "Pause",
  336. "Insert",
  337. "Home",
  338. "PageUp",
  339. "Delete",
  340. "End",
  341. "PageDown",
  342. "Right",
  343. "Left",
  344. "Down",
  345. "Up",
  346. "Numlock",
  347. "Keypad /",
  348. "Keypad *",
  349. "Keypad -",
  350. "Keypad +",
  351. "Keypad Enter",
  352. "Keypad 1",
  353. "Keypad 2",
  354. "Keypad 3",
  355. "Keypad 4",
  356. "Keypad 5",
  357. "Keypad 6",
  358. "Keypad 7",
  359. "Keypad 8",
  360. "Keypad 9",
  361. "Keypad 0",
  362. "Keypad .",
  363. NULL,
  364. "Application",
  365. "Power",
  366. "Keypad =",
  367. "F13",
  368. "F14",
  369. "F15",
  370. "F16",
  371. "F17",
  372. "F18",
  373. "F19",
  374. "F20",
  375. "F21",
  376. "F22",
  377. "F23",
  378. "F24",
  379. "Execute",
  380. "Help",
  381. "Menu",
  382. "Select",
  383. "Stop",
  384. "Again",
  385. "Undo",
  386. "Cut",
  387. "Copy",
  388. "Paste",
  389. "Find",
  390. "Mute",
  391. "VolumeUp",
  392. "VolumeDown",
  393. NULL, NULL, NULL,
  394. "Keypad ,",
  395. "Keypad = (AS400)",
  396. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  397. NULL, NULL, NULL, NULL, NULL, NULL,
  398. "AltErase",
  399. "SysReq",
  400. "Cancel",
  401. "Clear",
  402. "Prior",
  403. "Return",
  404. "Separator",
  405. "Out",
  406. "Oper",
  407. "Clear / Again",
  408. "CrSel",
  409. "ExSel",
  410. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  411. "Keypad 00",
  412. "Keypad 000",
  413. "ThousandsSeparator",
  414. "DecimalSeparator",
  415. "CurrencyUnit",
  416. "CurrencySubUnit",
  417. "Keypad (",
  418. "Keypad )",
  419. "Keypad {",
  420. "Keypad }",
  421. "Keypad Tab",
  422. "Keypad Backspace",
  423. "Keypad A",
  424. "Keypad B",
  425. "Keypad C",
  426. "Keypad D",
  427. "Keypad E",
  428. "Keypad F",
  429. "Keypad XOR",
  430. "Keypad ^",
  431. "Keypad %",
  432. "Keypad <",
  433. "Keypad >",
  434. "Keypad &",
  435. "Keypad &&",
  436. "Keypad |",
  437. "Keypad ||",
  438. "Keypad :",
  439. "Keypad #",
  440. "Keypad Space",
  441. "Keypad @",
  442. "Keypad !",
  443. "Keypad MemStore",
  444. "Keypad MemRecall",
  445. "Keypad MemClear",
  446. "Keypad MemAdd",
  447. "Keypad MemSubtract",
  448. "Keypad MemMultiply",
  449. "Keypad MemDivide",
  450. "Keypad +/-",
  451. "Keypad Clear",
  452. "Keypad ClearEntry",
  453. "Keypad Binary",
  454. "Keypad Octal",
  455. "Keypad Decimal",
  456. "Keypad Hexadecimal",
  457. NULL, NULL,
  458. "Left Ctrl",
  459. "Left Shift",
  460. "Left Alt",
  461. "Left GUI",
  462. "Right Ctrl",
  463. "Right Shift",
  464. "Right Alt",
  465. "Right GUI",
  466. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  467. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  468. NULL,
  469. "ModeSwitch",
  470. "AudioNext",
  471. "AudioPrev",
  472. "AudioStop",
  473. "AudioPlay",
  474. "AudioMute",
  475. "MediaSelect",
  476. "WWW",
  477. "Mail",
  478. "Calculator",
  479. "Computer",
  480. "AC Search",
  481. "AC Home",
  482. "AC Back",
  483. "AC Forward",
  484. "AC Stop",
  485. "AC Refresh",
  486. "AC Bookmarks",
  487. "BrightnessDown",
  488. "BrightnessUp",
  489. "DisplaySwitch",
  490. "KBDIllumToggle",
  491. "KBDIllumDown",
  492. "KBDIllumUp",
  493. "Eject",
  494. "Sleep",
  495. };
  496. /* Taken from SDL_iconv() */
  497. char *
  498. SDL_UCS4ToUTF8(Uint32 ch, char *dst)
  499. {
  500. Uint8 *p = (Uint8 *) dst;
  501. if (ch <= 0x7F) {
  502. *p = (Uint8) ch;
  503. ++dst;
  504. } else if (ch <= 0x7FF) {
  505. p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
  506. p[1] = 0x80 | (Uint8) (ch & 0x3F);
  507. dst += 2;
  508. } else if (ch <= 0xFFFF) {
  509. p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
  510. p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
  511. p[2] = 0x80 | (Uint8) (ch & 0x3F);
  512. dst += 3;
  513. } else if (ch <= 0x1FFFFF) {
  514. p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
  515. p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
  516. p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
  517. p[3] = 0x80 | (Uint8) (ch & 0x3F);
  518. dst += 4;
  519. } else if (ch <= 0x3FFFFFF) {
  520. p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
  521. p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
  522. p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
  523. p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
  524. p[4] = 0x80 | (Uint8) (ch & 0x3F);
  525. dst += 5;
  526. } else {
  527. p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
  528. p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
  529. p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
  530. p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
  531. p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
  532. p[5] = 0x80 | (Uint8) (ch & 0x3F);
  533. dst += 6;
  534. }
  535. return dst;
  536. }
  537. /* Public functions */
  538. int
  539. SDL_KeyboardInit(void)
  540. {
  541. SDL_Keyboard *keyboard = &SDL_keyboard;
  542. /* Set the default keymap */
  543. SDL_memcpy(keyboard->keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
  544. return (0);
  545. }
  546. void
  547. SDL_ResetKeyboard(void)
  548. {
  549. SDL_Keyboard *keyboard = &SDL_keyboard;
  550. SDL_Scancode scancode;
  551. #ifdef DEBUG_KEYBOARD
  552. printf("Resetting keyboard\n");
  553. #endif
  554. for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) {
  555. if (keyboard->keystate[scancode] == SDL_PRESSED) {
  556. SDL_SendKeyboardKey(SDL_RELEASED, scancode);
  557. }
  558. }
  559. }
  560. void
  561. SDL_GetDefaultKeymap(SDL_Keycode * keymap)
  562. {
  563. SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
  564. }
  565. void
  566. SDL_SetKeymap(int start, SDL_Keycode * keys, int length)
  567. {
  568. SDL_Keyboard *keyboard = &SDL_keyboard;
  569. if (start < 0 || start + length > SDL_NUM_SCANCODES) {
  570. return;
  571. }
  572. SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length);
  573. }
  574. void
  575. SDL_SetScancodeName(SDL_Scancode scancode, const char *name)
  576. {
  577. SDL_scancode_names[scancode] = name;
  578. }
  579. SDL_Window *
  580. SDL_GetKeyboardFocus(void)
  581. {
  582. SDL_Keyboard *keyboard = &SDL_keyboard;
  583. return keyboard->focus;
  584. }
  585. void
  586. SDL_SetKeyboardFocus(SDL_Window * window)
  587. {
  588. SDL_Keyboard *keyboard = &SDL_keyboard;
  589. if (keyboard->focus && !window) {
  590. /* We won't get anymore keyboard messages, so reset keyboard state */
  591. SDL_ResetKeyboard();
  592. }
  593. /* See if the current window has lost focus */
  594. if (keyboard->focus && keyboard->focus != window) {
  595. /* new window shouldn't think it has mouse captured. */
  596. SDL_assert(!window || !(window->flags & SDL_WINDOW_MOUSE_CAPTURE));
  597. /* old window must lose an existing mouse capture. */
  598. if (keyboard->focus->flags & SDL_WINDOW_MOUSE_CAPTURE) {
  599. SDL_CaptureMouse(SDL_FALSE); /* drop the capture. */
  600. SDL_assert(!(keyboard->focus->flags & SDL_WINDOW_MOUSE_CAPTURE));
  601. }
  602. SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST,
  603. 0, 0);
  604. /* Ensures IME compositions are committed */
  605. if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
  606. SDL_VideoDevice *video = SDL_GetVideoDevice();
  607. if (video && video->StopTextInput) {
  608. video->StopTextInput(video);
  609. }
  610. }
  611. }
  612. keyboard->focus = window;
  613. if (keyboard->focus) {
  614. SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_GAINED,
  615. 0, 0);
  616. if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
  617. SDL_VideoDevice *video = SDL_GetVideoDevice();
  618. if (video && video->StartTextInput) {
  619. video->StartTextInput(video);
  620. }
  621. }
  622. }
  623. }
  624. int
  625. SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
  626. {
  627. SDL_Keyboard *keyboard = &SDL_keyboard;
  628. int posted;
  629. SDL_Keymod modifier;
  630. SDL_Keycode keycode;
  631. Uint16 modstate;
  632. Uint32 type;
  633. Uint8 repeat;
  634. if (!scancode) {
  635. return 0;
  636. }
  637. #ifdef DEBUG_KEYBOARD
  638. printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
  639. state == SDL_PRESSED ? "pressed" : "released");
  640. #endif
  641. /* Figure out what type of event this is */
  642. switch (state) {
  643. case SDL_PRESSED:
  644. type = SDL_KEYDOWN;
  645. break;
  646. case SDL_RELEASED:
  647. type = SDL_KEYUP;
  648. break;
  649. default:
  650. /* Invalid state -- bail */
  651. return 0;
  652. }
  653. /* Drop events that don't change state */
  654. repeat = (state && keyboard->keystate[scancode]);
  655. if (keyboard->keystate[scancode] == state && !repeat) {
  656. #if 0
  657. printf("Keyboard event didn't change state - dropped!\n");
  658. #endif
  659. return 0;
  660. }
  661. /* Update internal keyboard state */
  662. keyboard->keystate[scancode] = state;
  663. keycode = keyboard->keymap[scancode];
  664. /* Update modifiers state if applicable */
  665. switch (keycode) {
  666. case SDLK_LCTRL:
  667. modifier = KMOD_LCTRL;
  668. break;
  669. case SDLK_RCTRL:
  670. modifier = KMOD_RCTRL;
  671. break;
  672. case SDLK_LSHIFT:
  673. modifier = KMOD_LSHIFT;
  674. break;
  675. case SDLK_RSHIFT:
  676. modifier = KMOD_RSHIFT;
  677. break;
  678. case SDLK_LALT:
  679. modifier = KMOD_LALT;
  680. break;
  681. case SDLK_RALT:
  682. modifier = KMOD_RALT;
  683. break;
  684. case SDLK_LGUI:
  685. modifier = KMOD_LGUI;
  686. break;
  687. case SDLK_RGUI:
  688. modifier = KMOD_RGUI;
  689. break;
  690. case SDLK_MODE:
  691. modifier = KMOD_MODE;
  692. break;
  693. default:
  694. modifier = KMOD_NONE;
  695. break;
  696. }
  697. if (SDL_KEYDOWN == type) {
  698. modstate = keyboard->modstate;
  699. switch (keycode) {
  700. case SDLK_NUMLOCKCLEAR:
  701. keyboard->modstate ^= KMOD_NUM;
  702. break;
  703. case SDLK_CAPSLOCK:
  704. keyboard->modstate ^= KMOD_CAPS;
  705. break;
  706. default:
  707. keyboard->modstate |= modifier;
  708. break;
  709. }
  710. } else {
  711. keyboard->modstate &= ~modifier;
  712. modstate = keyboard->modstate;
  713. }
  714. /* Post the event, if desired */
  715. posted = 0;
  716. if (SDL_GetEventState(type) == SDL_ENABLE) {
  717. SDL_Event event;
  718. event.key.type = type;
  719. event.key.state = state;
  720. event.key.repeat = repeat;
  721. event.key.keysym.scancode = scancode;
  722. event.key.keysym.sym = keycode;
  723. event.key.keysym.mod = modstate;
  724. event.key.windowID = keyboard->focus ? keyboard->focus->id : 0;
  725. posted = (SDL_PushEvent(&event) > 0);
  726. }
  727. return (posted);
  728. }
  729. int
  730. SDL_SendKeyboardText(const char *text)
  731. {
  732. SDL_Keyboard *keyboard = &SDL_keyboard;
  733. int posted;
  734. /* Don't post text events for unprintable characters */
  735. if ((unsigned char)*text < ' ' || *text == 127) {
  736. return 0;
  737. }
  738. /* Post the event, if desired */
  739. posted = 0;
  740. if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
  741. SDL_Event event;
  742. event.text.type = SDL_TEXTINPUT;
  743. event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
  744. SDL_utf8strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
  745. posted = (SDL_PushEvent(&event) > 0);
  746. }
  747. return (posted);
  748. }
  749. int
  750. SDL_SendEditingText(const char *text, int start, int length)
  751. {
  752. SDL_Keyboard *keyboard = &SDL_keyboard;
  753. int posted;
  754. /* Post the event, if desired */
  755. posted = 0;
  756. if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) {
  757. SDL_Event event;
  758. event.edit.type = SDL_TEXTEDITING;
  759. event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
  760. event.edit.start = start;
  761. event.edit.length = length;
  762. SDL_utf8strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text));
  763. posted = (SDL_PushEvent(&event) > 0);
  764. }
  765. return (posted);
  766. }
  767. void
  768. SDL_KeyboardQuit(void)
  769. {
  770. }
  771. const Uint8 *
  772. SDL_GetKeyboardState(int *numkeys)
  773. {
  774. SDL_Keyboard *keyboard = &SDL_keyboard;
  775. if (numkeys != (int *) 0) {
  776. *numkeys = SDL_NUM_SCANCODES;
  777. }
  778. return keyboard->keystate;
  779. }
  780. SDL_Keymod
  781. SDL_GetModState(void)
  782. {
  783. SDL_Keyboard *keyboard = &SDL_keyboard;
  784. return keyboard->modstate;
  785. }
  786. void
  787. SDL_SetModState(SDL_Keymod modstate)
  788. {
  789. SDL_Keyboard *keyboard = &SDL_keyboard;
  790. keyboard->modstate = modstate;
  791. }
  792. /* Note that SDL_ToggleModState() is not a public API. SDL_SetModState() is. */
  793. void
  794. SDL_ToggleModState(const SDL_Keymod modstate, const SDL_bool toggle)
  795. {
  796. SDL_Keyboard *keyboard = &SDL_keyboard;
  797. if (toggle) {
  798. keyboard->modstate |= modstate;
  799. } else {
  800. keyboard->modstate &= ~modstate;
  801. }
  802. }
  803. SDL_Keycode
  804. SDL_GetKeyFromScancode(SDL_Scancode scancode)
  805. {
  806. SDL_Keyboard *keyboard = &SDL_keyboard;
  807. if (scancode < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
  808. SDL_InvalidParamError("scancode");
  809. return 0;
  810. }
  811. return keyboard->keymap[scancode];
  812. }
  813. SDL_Scancode
  814. SDL_GetScancodeFromKey(SDL_Keycode key)
  815. {
  816. SDL_Keyboard *keyboard = &SDL_keyboard;
  817. SDL_Scancode scancode;
  818. for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
  819. ++scancode) {
  820. if (keyboard->keymap[scancode] == key) {
  821. return scancode;
  822. }
  823. }
  824. return SDL_SCANCODE_UNKNOWN;
  825. }
  826. const char *
  827. SDL_GetScancodeName(SDL_Scancode scancode)
  828. {
  829. const char *name;
  830. if (scancode < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
  831. SDL_InvalidParamError("scancode");
  832. return "";
  833. }
  834. name = SDL_scancode_names[scancode];
  835. if (name)
  836. return name;
  837. else
  838. return "";
  839. }
  840. SDL_Scancode SDL_GetScancodeFromName(const char *name)
  841. {
  842. int i;
  843. if (!name || !*name) {
  844. SDL_InvalidParamError("name");
  845. return SDL_SCANCODE_UNKNOWN;
  846. }
  847. for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
  848. if (!SDL_scancode_names[i]) {
  849. continue;
  850. }
  851. if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
  852. return (SDL_Scancode)i;
  853. }
  854. }
  855. SDL_InvalidParamError("name");
  856. return SDL_SCANCODE_UNKNOWN;
  857. }
  858. const char *
  859. SDL_GetKeyName(SDL_Keycode key)
  860. {
  861. static char name[8];
  862. char *end;
  863. if (key & SDLK_SCANCODE_MASK) {
  864. return
  865. SDL_GetScancodeName((SDL_Scancode) (key & ~SDLK_SCANCODE_MASK));
  866. }
  867. switch (key) {
  868. case SDLK_RETURN:
  869. return SDL_GetScancodeName(SDL_SCANCODE_RETURN);
  870. case SDLK_ESCAPE:
  871. return SDL_GetScancodeName(SDL_SCANCODE_ESCAPE);
  872. case SDLK_BACKSPACE:
  873. return SDL_GetScancodeName(SDL_SCANCODE_BACKSPACE);
  874. case SDLK_TAB:
  875. return SDL_GetScancodeName(SDL_SCANCODE_TAB);
  876. case SDLK_SPACE:
  877. return SDL_GetScancodeName(SDL_SCANCODE_SPACE);
  878. case SDLK_DELETE:
  879. return SDL_GetScancodeName(SDL_SCANCODE_DELETE);
  880. default:
  881. /* Unaccented letter keys on latin keyboards are normally
  882. labeled in upper case (and probably on others like Greek or
  883. Cyrillic too, so if you happen to know for sure, please
  884. adapt this). */
  885. if (key >= 'a' && key <= 'z') {
  886. key -= 32;
  887. }
  888. end = SDL_UCS4ToUTF8((Uint32) key, name);
  889. *end = '\0';
  890. return name;
  891. }
  892. }
  893. SDL_Keycode
  894. SDL_GetKeyFromName(const char *name)
  895. {
  896. SDL_Keycode key;
  897. /* Check input */
  898. if (name == NULL) {
  899. return SDLK_UNKNOWN;
  900. }
  901. /* If it's a single UTF-8 character, then that's the keycode itself */
  902. key = *(const unsigned char *)name;
  903. if (key >= 0xF0) {
  904. if (SDL_strlen(name) == 4) {
  905. int i = 0;
  906. key = (Uint16)(name[i]&0x07) << 18;
  907. key |= (Uint16)(name[++i]&0x3F) << 12;
  908. key |= (Uint16)(name[++i]&0x3F) << 6;
  909. key |= (Uint16)(name[++i]&0x3F);
  910. return key;
  911. }
  912. return SDLK_UNKNOWN;
  913. } else if (key >= 0xE0) {
  914. if (SDL_strlen(name) == 3) {
  915. int i = 0;
  916. key = (Uint16)(name[i]&0x0F) << 12;
  917. key |= (Uint16)(name[++i]&0x3F) << 6;
  918. key |= (Uint16)(name[++i]&0x3F);
  919. return key;
  920. }
  921. return SDLK_UNKNOWN;
  922. } else if (key >= 0xC0) {
  923. if (SDL_strlen(name) == 2) {
  924. int i = 0;
  925. key = (Uint16)(name[i]&0x1F) << 6;
  926. key |= (Uint16)(name[++i]&0x3F);
  927. return key;
  928. }
  929. return SDLK_UNKNOWN;
  930. } else {
  931. if (SDL_strlen(name) == 1) {
  932. if (key >= 'A' && key <= 'Z') {
  933. key += 32;
  934. }
  935. return key;
  936. }
  937. /* Get the scancode for this name, and the associated keycode */
  938. return SDL_default_keymap[SDL_GetScancodeFromName(name)];
  939. }
  940. }
  941. /* vi: set ts=4 sw=4 expandtab: */