Update VS2010 project to add new files; update new files so code builds on Win32/Win64
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2010 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* General keyboard handling code for SDL */
26 #include "SDL_timer.h"
27 #include "SDL_events.h"
28 #include "SDL_events_c.h"
29 #include "SDL_sysevents.h"
32 /* Global keyboard information */
34 typedef struct SDL_Keyboard SDL_Keyboard;
38 /* Data common to all keyboards */
41 Uint8 keystate[SDL_NUM_SCANCODES];
42 SDLKey keymap[SDL_NUM_SCANCODES];
45 static SDL_Keyboard SDL_keyboard;
47 static const SDLKey SDL_default_keymap[SDL_NUM_SCANCODES] = {
178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194 SDLK_THOUSANDSSEPARATOR,
195 SDLK_DECIMALSEPARATOR,
197 SDLK_CURRENCYSUBUNIT,
216 SDLK_KP_DBLAMPERSAND,
218 SDLK_KP_DBLVERTICALBAR,
247 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,
276 static const char *SDL_scancode_names[SDL_NUM_SCANCODES] = {
277 NULL, NULL, NULL, NULL,
407 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
408 NULL, NULL, NULL, NULL, NULL, NULL,
421 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
424 "ThousandsSeparator",
458 "Keypad MemSubtract",
459 "Keypad MemMultiply",
467 "Keypad Hexadecimal",
477 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
478 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
508 /* Taken from SDL_iconv() */
510 SDL_UCS4ToUTF8(Uint32 ch, char *dst)
512 Uint8 *p = (Uint8 *) dst;
516 } else if (ch <= 0x7FF) {
517 p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
518 p[1] = 0x80 | (Uint8) (ch & 0x3F);
520 } else if (ch <= 0xFFFF) {
521 p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
522 p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
523 p[2] = 0x80 | (Uint8) (ch & 0x3F);
525 } else if (ch <= 0x1FFFFF) {
526 p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
527 p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
528 p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
529 p[3] = 0x80 | (Uint8) (ch & 0x3F);
531 } else if (ch <= 0x3FFFFFF) {
532 p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
533 p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
534 p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
535 p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
536 p[4] = 0x80 | (Uint8) (ch & 0x3F);
539 p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
540 p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
541 p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
542 p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
543 p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
544 p[5] = 0x80 | (Uint8) (ch & 0x3F);
550 /* Public functions */
552 SDL_KeyboardInit(void)
554 SDL_Keyboard *keyboard = &SDL_keyboard;
556 /* Set the default keymap */
557 SDL_memcpy(keyboard->keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
562 SDL_ResetKeyboard(void)
564 SDL_Keyboard *keyboard = &SDL_keyboard;
565 SDL_scancode scancode;
567 for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) {
568 if (keyboard->keystate[scancode] == SDL_PRESSED) {
569 SDL_SendKeyboardKey(SDL_RELEASED, scancode);
575 SDL_GetDefaultKeymap(SDLKey * keymap)
577 SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
581 SDL_SetKeymap(int start, SDLKey * keys, int length)
583 SDL_Keyboard *keyboard = &SDL_keyboard;
585 if (start < 0 || start + length > SDL_NUM_SCANCODES) {
589 SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length);
593 SDL_SetScancodeName(SDL_scancode scancode, const char *name)
595 SDL_scancode_names[scancode] = name;
599 SDL_GetKeyboardFocus(void)
601 SDL_Keyboard *keyboard = &SDL_keyboard;
603 return keyboard->focus;
607 SDL_SetKeyboardFocus(SDL_Window * window)
609 SDL_Keyboard *keyboard = &SDL_keyboard;
611 /* See if the current window has lost focus */
612 if (keyboard->focus && keyboard->focus != window) {
613 SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST,
617 keyboard->focus = window;
619 if (keyboard->focus) {
620 SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_GAINED,
623 if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
624 SDL_StartTextInput();
630 SDL_SendKeyboardKey(Uint8 state, SDL_scancode scancode)
632 SDL_Keyboard *keyboard = &SDL_keyboard;
642 printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
643 state == SDL_PRESSED ? "pressed" : "released");
645 if (state == SDL_PRESSED) {
646 modstate = keyboard->modstate;
648 case SDL_SCANCODE_NUMLOCKCLEAR:
649 keyboard->modstate ^= KMOD_NUM;
651 case SDL_SCANCODE_CAPSLOCK:
652 keyboard->modstate ^= KMOD_CAPS;
654 case SDL_SCANCODE_LCTRL:
655 keyboard->modstate |= KMOD_LCTRL;
657 case SDL_SCANCODE_RCTRL:
658 keyboard->modstate |= KMOD_RCTRL;
660 case SDL_SCANCODE_LSHIFT:
661 keyboard->modstate |= KMOD_LSHIFT;
663 case SDL_SCANCODE_RSHIFT:
664 keyboard->modstate |= KMOD_RSHIFT;
666 case SDL_SCANCODE_LALT:
667 keyboard->modstate |= KMOD_LALT;
669 case SDL_SCANCODE_RALT:
670 keyboard->modstate |= KMOD_RALT;
672 case SDL_SCANCODE_LGUI:
673 keyboard->modstate |= KMOD_LGUI;
675 case SDL_SCANCODE_RGUI:
676 keyboard->modstate |= KMOD_RGUI;
678 case SDL_SCANCODE_MODE:
679 keyboard->modstate |= KMOD_MODE;
686 case SDL_SCANCODE_NUMLOCKCLEAR:
687 case SDL_SCANCODE_CAPSLOCK:
689 case SDL_SCANCODE_LCTRL:
690 keyboard->modstate &= ~KMOD_LCTRL;
692 case SDL_SCANCODE_RCTRL:
693 keyboard->modstate &= ~KMOD_RCTRL;
695 case SDL_SCANCODE_LSHIFT:
696 keyboard->modstate &= ~KMOD_LSHIFT;
698 case SDL_SCANCODE_RSHIFT:
699 keyboard->modstate &= ~KMOD_RSHIFT;
701 case SDL_SCANCODE_LALT:
702 keyboard->modstate &= ~KMOD_LALT;
704 case SDL_SCANCODE_RALT:
705 keyboard->modstate &= ~KMOD_RALT;
707 case SDL_SCANCODE_LGUI:
708 keyboard->modstate &= ~KMOD_LGUI;
710 case SDL_SCANCODE_RGUI:
711 keyboard->modstate &= ~KMOD_RGUI;
713 case SDL_SCANCODE_MODE:
714 keyboard->modstate &= ~KMOD_MODE;
719 modstate = keyboard->modstate;
722 /* Figure out what type of event this is */
731 /* Invalid state -- bail */
735 /* Drop events that don't change state */
736 repeat = (state && keyboard->keystate[scancode]);
737 if (keyboard->keystate[scancode] == state && !repeat) {
739 printf("Keyboard event didn't change state - dropped!\n");
744 /* Update internal keyboard state */
745 keyboard->keystate[scancode] = state;
747 /* Post the event, if desired */
749 if (SDL_GetEventState(type) == SDL_ENABLE) {
751 event.key.type = type;
752 event.key.state = state;
753 event.key.repeat = repeat;
754 event.key.keysym.scancode = scancode;
755 event.key.keysym.sym = keyboard->keymap[scancode];
756 event.key.keysym.mod = modstate;
757 event.key.keysym.unicode = 0;
758 event.key.windowID = keyboard->focus ? keyboard->focus->id : 0;
759 posted = (SDL_PushEvent(&event) > 0);
765 SDL_SendKeyboardText(const char *text)
767 SDL_Keyboard *keyboard = &SDL_keyboard;
770 /* Don't post text events for unprintable characters */
771 if ((unsigned char)*text < ' ' || *text == 127) {
775 /* Post the event, if desired */
777 if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
779 event.text.type = SDL_TEXTINPUT;
780 event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
781 SDL_utf8strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
782 event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
783 posted = (SDL_PushEvent(&event) > 0);
789 SDL_SendEditingText(const char *text, int start, int length)
791 SDL_Keyboard *keyboard = &SDL_keyboard;
794 /* Post the event, if desired */
796 if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) {
798 event.edit.type = SDL_TEXTEDITING;
799 event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
800 event.edit.start = start;
801 event.edit.length = length;
802 SDL_utf8strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text));
803 posted = (SDL_PushEvent(&event) > 0);
809 SDL_KeyboardQuit(void)
814 SDL_GetKeyboardState(int *numkeys)
816 SDL_Keyboard *keyboard = &SDL_keyboard;
818 if (numkeys != (int *) 0) {
819 *numkeys = SDL_NUM_SCANCODES;
821 return keyboard->keystate;
825 SDL_GetModState(void)
827 SDL_Keyboard *keyboard = &SDL_keyboard;
829 return keyboard->modstate;
833 SDL_SetModState(SDLMod modstate)
835 SDL_Keyboard *keyboard = &SDL_keyboard;
837 keyboard->modstate = modstate;
841 SDL_GetKeyFromScancode(SDL_scancode scancode)
843 SDL_Keyboard *keyboard = &SDL_keyboard;
845 return keyboard->keymap[scancode];
849 SDL_GetScancodeFromKey(SDLKey key)
851 SDL_Keyboard *keyboard = &SDL_keyboard;
852 SDL_scancode scancode;
854 for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
856 if (keyboard->keymap[scancode] == key) {
860 return SDL_SCANCODE_UNKNOWN;
864 SDL_GetScancodeName(SDL_scancode scancode)
866 const char *name = SDL_scancode_names[scancode];
875 SDL_GetKeyName(SDLKey key)
880 if (key & SDLK_SCANCODE_MASK) {
882 SDL_GetScancodeName((SDL_scancode) (key & ~SDLK_SCANCODE_MASK));
887 return SDL_GetScancodeName(SDL_SCANCODE_RETURN);
889 return SDL_GetScancodeName(SDL_SCANCODE_ESCAPE);
891 return SDL_GetScancodeName(SDL_SCANCODE_BACKSPACE);
893 return SDL_GetScancodeName(SDL_SCANCODE_TAB);
895 return SDL_GetScancodeName(SDL_SCANCODE_SPACE);
897 return SDL_GetScancodeName(SDL_SCANCODE_DELETE);
899 /* Unaccented letter keys on latin keyboards are normally
900 labeled in upper case (and probably on others like Greek or
901 Cyrillic too, so if you happen to know for sure, please
903 if (key >= 'a' && key <= 'z') {
907 end = SDL_UCS4ToUTF8((Uint32) key, name);
913 /* vi: set ts=4 sw=4 expandtab: */