First pass implementation of new SDL scancode concept, as discussed with

Christian Walther.  Currently only implemented on Mac OS X for sanity
checking purposes.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402715
This commit is contained in:
Sam Lantinga 2008-02-05 07:19:23 +00:00
parent 844280a6a9
commit 02f2e328bd
16 changed files with 1575 additions and 1335 deletions

View file

@ -27,18 +27,475 @@
#include "SDL_events.h"
#include "SDL_events_c.h"
#include "SDL_sysevents.h"
#include "SDL_keynames.h"
/* Global keyboard information */
int SDL_TranslateUNICODE = 0;
static int SDL_num_keyboards;
static int SDL_current_keyboard;
static SDL_Keyboard **SDL_keyboards;
static SDLKey SDL_default_keymap[SDL_NUM_SCANCODES] = {
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,
SDLK_F6,
SDLK_F7,
SDLK_F8,
SDLK_F9,
SDLK_F10,
SDLK_F11,
SDLK_F12,
SDLK_PRINTSCREEN,
SDLK_SCROLLLOCK,
SDLK_PAUSE,
SDLK_INSERT,
SDLK_HOME,
SDLK_PAGEUP,
SDLK_DELETE,
SDLK_END,
SDLK_PAGEDOWN,
SDLK_RIGHT,
SDLK_LEFT,
SDLK_DOWN,
SDLK_UP,
SDLK_NUMLOCKCLEAR,
SDLK_KP_DIVIDE,
SDLK_KP_MULTIPLY,
SDLK_KP_MINUS,
SDLK_KP_PLUS,
SDLK_KP_ENTER,
SDLK_KP_1,
SDLK_KP_2,
SDLK_KP_3,
SDLK_KP_4,
SDLK_KP_5,
SDLK_KP_6,
SDLK_KP_7,
SDLK_KP_8,
SDLK_KP_9,
SDLK_KP_0,
SDLK_KP_PERIOD,
0,
SDLK_APPLICATION,
SDLK_POWER,
SDLK_KP_EQUALS,
SDLK_F13,
SDLK_F14,
SDLK_F15,
SDLK_F16,
SDLK_F17,
SDLK_F18,
SDLK_F19,
SDLK_F20,
SDLK_F21,
SDLK_F22,
SDLK_F23,
SDLK_F24,
SDLK_EXECUTE,
SDLK_HELP,
SDLK_MENU,
SDLK_SELECT,
SDLK_STOP,
SDLK_AGAIN,
SDLK_UNDO,
SDLK_CUT,
SDLK_COPY,
SDLK_PASTE,
SDLK_FIND,
SDLK_MUTE,
SDLK_VOLUMEUP,
SDLK_VOLUMEDOWN,
0, 0, 0,
SDLK_KP_COMMA,
SDLK_KP_EQUALSAS400,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
SDLK_ALTERASE,
SDLK_SYSREQ,
SDLK_CANCEL,
SDLK_CLEAR,
SDLK_PRIOR,
SDLK_RETURN2,
SDLK_SEPARATOR,
SDLK_OUT,
SDLK_OPER,
SDLK_CLEARAGAIN,
SDLK_CRSEL,
SDLK_EXSEL,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
SDLK_KP_00,
SDLK_KP_000,
SDLK_THOUSANDSSEPARATOR,
SDLK_DECIMALSEPARATOR,
SDLK_CURRENCYUNIT,
SDLK_CURRENCYSUBUNIT,
SDLK_KP_LEFTPAREN,
SDLK_KP_RIGHTPAREN,
SDLK_KP_LEFTBRACE,
SDLK_KP_RIGHTBRACE,
SDLK_KP_TAB,
SDLK_KP_BACKSPACE,
SDLK_KP_A,
SDLK_KP_B,
SDLK_KP_C,
SDLK_KP_D,
SDLK_KP_E,
SDLK_KP_F,
SDLK_KP_XOR,
SDLK_KP_POWER,
SDLK_KP_PERCENT,
SDLK_KP_LESS,
SDLK_KP_GREATER,
SDLK_KP_AMPERSAND,
SDLK_KP_DBLAMPERSAND,
SDLK_KP_VERTICALBAR,
SDLK_KP_DBLVERTICALBAR,
SDLK_KP_COLON,
SDLK_KP_HASH,
SDLK_KP_SPACE,
SDLK_KP_AT,
SDLK_KP_EXCLAM,
SDLK_KP_MEMSTORE,
SDLK_KP_MEMRECALL,
SDLK_KP_MEMCLEAR,
SDLK_KP_MEMADD,
SDLK_KP_MEMSUBTRACT,
SDLK_KP_MEMMULTIPLY,
SDLK_KP_MEMDIVIDE,
SDLK_KP_PLUSMINUS,
SDLK_KP_CLEAR,
SDLK_KP_CLEARENTRY,
SDLK_KP_BINARY,
SDLK_KP_OCTAL,
SDLK_KP_DECIMAL,
SDLK_KP_HEXADECIMAL,
0, 0,
SDLK_LCTRL,
SDLK_LSHIFT,
SDLK_LALT,
SDLK_LGUI,
SDLK_RCTRL,
SDLK_RSHIFT,
SDLK_RALT,
SDLK_RGUI,
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,
SDLK_MODE,
SDLK_AUDIONEXT,
SDLK_AUDIOPREV,
SDLK_AUDIOSTOP,
SDLK_AUDIOPLAY,
SDLK_AUDIOMUTE,
SDLK_MEDIASELECT,
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",
"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",
};
/* Taken from SDL_iconv() */
char *
SDL_Ucs4ToUtf8(Uint32 ch, char *dst)
static char *
SDL_UCS4ToUTF8(Uint32 ch, char *dst)
{
Uint8 *p = (Uint8 *) dst;
if (ch <= 0x7F) {
@ -82,9 +539,6 @@ SDL_Ucs4ToUtf8(Uint32 ch, char *dst)
int
SDL_KeyboardInit(void)
{
/* Set default mode of UNICODE translation */
SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION);
return (0);
}
@ -148,170 +602,45 @@ void
SDL_ResetKeyboard(int index)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
int key;
SDL_scancode scancode;
if (!keyboard) {
return;
}
for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
if (keyboard->keystate[key] == SDL_PRESSED) {
SDL_SendKeyboardKey(index, SDL_RELEASED, 0,
key | SDL_KEY_CAN_BE_PHYSICAL_BIT);
for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) {
if (keyboard->keystate[scancode] == SDL_PRESSED) {
SDL_SendKeyboardKey(index, SDL_RELEASED, scancode);
}
}
}
void
SDL_KeyboardQuit(void)
SDL_GetDefaultKeymap(SDLKey * keymap)
{
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;
}
int
SDL_EnableUNICODE(int enable)
{
int old_mode;
old_mode = SDL_TranslateUNICODE;
if (enable >= 0) {
SDL_TranslateUNICODE = enable;
}
return (old_mode);
}
Uint8 *
SDL_GetKeyState(int *numkeys)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
if (numkeys != (int *) 0) {
*numkeys = SDLK_LAST;
}
if (!keyboard) {
return NULL;
}
return keyboard->keystate;
}
SDLMod
SDL_GetModState(void)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
if (!keyboard) {
return KMOD_NONE;
}
return keyboard->modstate;
SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
}
void
SDL_SetModState(SDLMod modstate)
SDL_SetKeymap(int index, int start, SDLKey * keys, int length)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
if (!keyboard) {
return;
}
keyboard->modstate = modstate;
}
SDLKey
SDL_GetLayoutKey(SDLKey physicalKey)
{
SDL_VideoDevice *_this = SDL_GetVideoDevice();
if (_this && _this->GetLayoutKey) {
return _this->GetLayoutKey(_this, physicalKey)
| (physicalKey & SDL_KEY_KEYPAD_BIT);
} else {
return physicalKey;
}
}
const char *
SDL_GetKeyName(SDLKey layoutKey)
{
const char *keyname = NULL;
if ((layoutKey & SDL_KEY_LAYOUT_SPECIAL_BIT) != 0) {
SDL_VideoDevice *_this = SDL_GetVideoDevice();
if (_this && _this->GetSpecialKeyName) {
keyname = _this->GetSpecialKeyName(_this, layoutKey);
}
} else if ((layoutKey & SDL_KEY_CAN_BE_PHYSICAL_BIT) == 0) {
/* SDLK_INDEX(layoutKey) is the unicode code point of the
character generated by the key */
static char buffer[9]; /* 6 (maximal UTF-8 char length) + 2 ([] for keypad) + 1 (null teminator) */
char *bufferPtr = &buffer[1];
Uint32 codepoint = SDLK_INDEX(layoutKey);
/* 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 (codepoint >= 'a' && codepoint <= 'z') {
codepoint -= 32;
}
bufferPtr = SDL_Ucs4ToUtf8(codepoint, bufferPtr);
*bufferPtr = '\0';
if ((layoutKey & SDL_KEY_KEYPAD_BIT) != 0) {
buffer[0] = '[';
*bufferPtr++ = ']';
*bufferPtr = '\0';
keyname = buffer;
} else {
keyname = &buffer[1];
}
} else {
/* SDLK_INDEX(layoutKey) is a physical key number */
if (SDLK_INDEX(layoutKey) < SDL_arraysize(SDL_keynames)) {
keyname = SDL_keynames[SDLK_INDEX(layoutKey)];
}
if (start < 0 || start + length > SDL_NUM_SCANCODES) {
return;
}
if (keyname == NULL) {
keyname = SDL_keynames[SDLK_INDEX(SDLK_UNKNOWN)];
}
return keyname;
SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length);
}
void
SDL_SetKeyName(SDLKey physicalKey, const char *name)
SDL_SetScancodeName(SDL_scancode scancode, const char *name)
{
physicalKey = SDLK_INDEX(physicalKey);
if (physicalKey < SDL_arraysize(SDL_keynames)) {
SDL_keynames[physicalKey] = name;
}
SDL_scancode_names[scancode] = name;
}
void
@ -366,94 +695,89 @@ SDL_SetKeyboardFocus(int index, SDL_WindowID windowID)
}
int
SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode,
SDLKey physicalKey)
SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
int posted;
Uint16 modstate;
Uint8 type;
if (!keyboard || physicalKey == SDLK_NONE) {
if (!keyboard || !scancode) {
return 0;
}
#if 0
printf("The '%s' key has been %s\n", SDL_GetKeyName(physicalKey),
printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
state == SDL_PRESSED ? "pressed" : "released");
#endif
if (state == SDL_PRESSED) {
modstate = keyboard->modstate;
switch (physicalKey) {
case SDLK_UNKNOWN:
break;
case SDLK_KP_NUMLOCKCLEAR:
switch (scancode) {
case SDL_SCANCODE_NUMLOCKCLEAR:
keyboard->modstate ^= KMOD_NUM;
break;
case SDLK_CAPSLOCK:
case SDL_SCANCODE_CAPSLOCK:
keyboard->modstate ^= KMOD_CAPS;
break;
case SDLK_LCTRL:
case SDL_SCANCODE_LCTRL:
keyboard->modstate |= KMOD_LCTRL;
break;
case SDLK_RCTRL:
case SDL_SCANCODE_RCTRL:
keyboard->modstate |= KMOD_RCTRL;
break;
case SDLK_LSHIFT:
case SDL_SCANCODE_LSHIFT:
keyboard->modstate |= KMOD_LSHIFT;
break;
case SDLK_RSHIFT:
case SDL_SCANCODE_RSHIFT:
keyboard->modstate |= KMOD_RSHIFT;
break;
case SDLK_LALT:
case SDL_SCANCODE_LALT:
keyboard->modstate |= KMOD_LALT;
break;
case SDLK_RALT:
case SDL_SCANCODE_RALT:
keyboard->modstate |= KMOD_RALT;
break;
case SDLK_LMETA:
keyboard->modstate |= KMOD_LMETA;
case SDL_SCANCODE_LGUI:
keyboard->modstate |= KMOD_LGUI;
break;
case SDLK_RMETA:
keyboard->modstate |= KMOD_RMETA;
case SDL_SCANCODE_RGUI:
keyboard->modstate |= KMOD_RGUI;
break;
case SDLK_MODE:
case SDL_SCANCODE_MODE:
keyboard->modstate |= KMOD_MODE;
break;
default:
break;
}
} else {
switch (physicalKey) {
case SDLK_UNKNOWN:
switch (scancode) {
case SDL_SCANCODE_NUMLOCKCLEAR:
case SDL_SCANCODE_CAPSLOCK:
break;
case SDLK_KP_NUMLOCKCLEAR:
case SDLK_CAPSLOCK:
break;
case SDLK_LCTRL:
case SDL_SCANCODE_LCTRL:
keyboard->modstate &= ~KMOD_LCTRL;
break;
case SDLK_RCTRL:
case SDL_SCANCODE_RCTRL:
keyboard->modstate &= ~KMOD_RCTRL;
break;
case SDLK_LSHIFT:
case SDL_SCANCODE_LSHIFT:
keyboard->modstate &= ~KMOD_LSHIFT;
break;
case SDLK_RSHIFT:
case SDL_SCANCODE_RSHIFT:
keyboard->modstate &= ~KMOD_RSHIFT;
break;
case SDLK_LALT:
case SDL_SCANCODE_LALT:
keyboard->modstate &= ~KMOD_LALT;
break;
case SDLK_RALT:
case SDL_SCANCODE_RALT:
keyboard->modstate &= ~KMOD_RALT;
break;
case SDLK_LMETA:
keyboard->modstate &= ~KMOD_LMETA;
case SDL_SCANCODE_LGUI:
keyboard->modstate &= ~KMOD_LGUI;
break;
case SDLK_RMETA:
keyboard->modstate &= ~KMOD_RMETA;
case SDL_SCANCODE_RGUI:
keyboard->modstate &= ~KMOD_RGUI;
break;
case SDLK_MODE:
case SDL_SCANCODE_MODE:
keyboard->modstate &= ~KMOD_MODE;
break;
default:
@ -475,19 +799,17 @@ SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode,
return 0;
}
if (physicalKey != SDLK_UNKNOWN) {
/* Drop events that don't change state */
if (keyboard->keystate[SDLK_INDEX(physicalKey)] == state) {
/* Drop events that don't change state */
if (keyboard->keystate[scancode] == state) {
#if 0
printf("Keyboard event didn't change state - dropped!\n");
printf("Keyboard event didn't change state - dropped!\n");
#endif
return 0;
}
/* Update internal keyboard state */
keyboard->keystate[SDLK_INDEX(physicalKey)] = state;
return 0;
}
/* Update internal keyboard state */
keyboard->keystate[scancode] = state;
/* Post the event, if desired */
posted = 0;
if (SDL_ProcessEvents[type] == SDL_ENABLE) {
@ -496,7 +818,7 @@ SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode,
event.key.which = (Uint8) index;
event.key.state = state;
event.key.keysym.scancode = scancode;
event.key.keysym.sym = physicalKey;
event.key.keysym.sym = keyboard->keymap[scancode];
event.key.keysym.mod = modstate;
event.key.keysym.unicode = 0;
event.key.windowID = keyboard->focus;
@ -528,4 +850,138 @@ SDL_SendKeyboardText(int index, const char *text)
return (posted);
}
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;
}
Uint8 *
SDL_GetKeyboardState(int *numkeys)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
if (numkeys != (int *) 0) {
*numkeys = SDL_NUM_SCANCODES;
}
if (!keyboard) {
return NULL;
}
return keyboard->keystate;
}
SDLMod
SDL_GetModState(void)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
if (!keyboard) {
return KMOD_NONE;
}
return keyboard->modstate;
}
void
SDL_SetModState(SDLMod modstate)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
if (!keyboard) {
return;
}
keyboard->modstate = modstate;
}
SDLKey
SDL_GetKeyFromScancode(SDL_scancode scancode)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
if (!keyboard) {
return SDLK_UNKNOWN;
}
return keyboard->keymap[scancode];
}
SDL_scancode
SDL_GetScancodeFromKey(SDLKey key)
{
SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
SDL_scancode scancode;
if (!keyboard) {
return SDL_SCANCODE_UNKNOWN;
}
for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
++scancode) {
if (keyboard->keymap[scancode] == key) {
return scancode;
}
}
return SDL_SCANCODE_UNKNOWN;
}
const char *
SDL_GetScancodeName(SDL_scancode scancode)
{
const char *name = SDL_scancode_names[scancode];
if (name)
return name;
else
return "";
}
const char *
SDL_GetKeyName(SDLKey key)
{
static char name[8];
char *end;
if (key & SDLK_SCANCODE_MASK) {
return
SDL_GetScancodeName((SDL_scancode) (key & ~SDLK_SCANCODE_MASK));
}
/* 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;
}
end = SDL_UCS4ToUTF8((Uint32) key, name);
*end = '\0';
return name;
}
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -37,20 +37,12 @@ struct SDL_Keyboard
/* Data common to all keyboards */
SDL_WindowID focus;
Uint16 modstate;
Uint8 keystate[SDLK_LAST];
Uint8 keystate[(SDL_NUM_SCANCODES + 7) / 8];
SDLKey keymap[SDL_NUM_SCANCODES];
void *driverdata;
};
/* Used by the OS keyboard code to detect whether or not to do UNICODE */
#ifndef DEFAULT_UNICODE_TRANSLATION
#define DEFAULT_UNICODE_TRANSLATION 0 /* Default off because of overhead */
#endif
extern int SDL_TranslateUNICODE;
/* convert UCS4 to utf8 */
extern char *SDL_Ucs4ToUtf8(Uint32 ch, char *dst);
/* Initialize the keyboard subsystem */
extern int SDL_KeyboardInit(void);
@ -68,18 +60,23 @@ extern void SDL_DelKeyboard(int index);
/* Clear the state of a keyboard at an index */
extern void SDL_ResetKeyboard(int index);
/* Get the default keymap */
extern void SDL_GetDefaultKeymap(SDLKey * keymap);
/* Set the mapping of scancode to key codes for this keyboard */
extern void SDL_SetKeymap(int index, int start, SDLKey * keys, int length);
/* Set a platform-dependent key name, overriding the default platform-agnostic
name. Encoded as UTF-8. The string is not copied, thus the pointer given to
this function must stay valid forever (or at least until the call to
VideoQuit()). */
extern void SDL_SetKeyName(SDLKey physicalKey, const char *name);
extern void SDL_SetScancodeName(SDL_scancode scancode, const char *name);
/* Set the keyboard focus window */
extern void SDL_SetKeyboardFocus(int index, SDL_WindowID windowID);
/* Send a keyboard event for a keyboard at an index */
extern int SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode,
SDLKey key);
extern int SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode);
/* Send keyboard text input for a keyboard at an index */
extern int SDL_SendKeyboardText(int index, const char *text);

View file

@ -1,304 +0,0 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2007 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
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
Lesser General Public License for more details.
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
Christian Walther
cwalther@gmx.ch
*/
/* Names for the physical SDLKey constants, returned by SDL_GetKeyName().
The strings are in UTF-8 encoding.
This table can (and should) be modified by a video driver in its VideoInit()
function using SDL_SetKeyName() to account for platform-dependent (but
layout-independent) key names.
The physical SDLKey codes can be divided into two groups:
- codes that occur both as physical and as layout keys. These have their
real, "user-readable" display name here.
- codes that only occur as physical keys, i.e. are never returned by
SDL_GetLayoutKey() if the backend implements it properly. These names are
therefore never returned by SDL_GetKeyName(SDL_GetLayoutKey(
event.key.keysym.sym)), the proper way of displaying a key name to the
user, but only by SDL_GetKeyName(event.key.keysym.sym), which is only
useful for debugging purposes. To emphasize this, these codes are named by
their "programmer-readable" SDLK_ constants here rather than by a
"user-readable" display name.
*/
/* *INDENT-OFF* */
static const char *SDL_keynames[SDLK_LAST] = {
/* 0 */ "", /* SDL_PK_NONE */
/* 1 */ "unknown key",
/* 2 */ NULL, /* unused */
/* 3 */ NULL, /* unused */
/* 4 */ "SDLK_A",
/* 5 */ "SDLK_B",
/* 6 */ "SDLK_C",
/* 7 */ "SDLK_D",
/* 8 */ "SDLK_E",
/* 9 */ "SDLK_F",
/* 10 */ "SDLK_G",
/* 11 */ "SDLK_H",
/* 12 */ "SDLK_I",
/* 13 */ "SDLK_J",
/* 14 */ "SDLK_K",
/* 15 */ "SDLK_L",
/* 16 */ "SDLK_M",
/* 17 */ "SDLK_N",
/* 18 */ "SDLK_O",
/* 19 */ "SDLK_P",
/* 20 */ "SDLK_Q",
/* 21 */ "SDLK_R",
/* 22 */ "SDLK_S",
/* 23 */ "SDLK_T",
/* 24 */ "SDLK_U",
/* 25 */ "SDLK_V",
/* 26 */ "SDLK_W",
/* 27 */ "SDLK_X",
/* 28 */ "SDLK_Y",
/* 29 */ "SDLK_Z",
/* 30 */ "SDLK_1",
/* 31 */ "SDLK_2",
/* 32 */ "SDLK_3",
/* 33 */ "SDLK_4",
/* 34 */ "SDLK_5",
/* 35 */ "SDLK_6",
/* 36 */ "SDLK_7",
/* 37 */ "SDLK_8",
/* 38 */ "SDLK_9",
/* 39 */ "SDLK_0",
/* 40 */ "return",
/* 41 */ "escape",
/* 42 */ "backspace",
/* 43 */ "tab",
/* 44 */ "space",
/* 45 */ "SDLK_HYPHENMINUS",
/* 46 */ "SDLK_EQUALS",
/* 47 */ "SDLK_LEFTBRACKET",
/* 48 */ "SDLK_RIGHTBRACKET",
/* 49 */ "SDLK_BACKSLASH",
/* 50 */ "SDLK_NONUSHASH",
/* 51 */ "SDLK_SEMICOLON",
/* 52 */ "SDLK_APOSTROPHE",
/* 53 */ "SDLK_GRAVE",
/* 54 */ "SDLK_COMMA",
/* 55 */ "SDLK_PERIOD",
/* 56 */ "SDLK_SLASH",
/* 57 */ "caps lock",
/* 58 */ "F1",
/* 59 */ "F2",
/* 60 */ "F3",
/* 61 */ "F4",
/* 62 */ "F5",
/* 63 */ "F6",
/* 64 */ "F7",
/* 65 */ "F8",
/* 66 */ "F9",
/* 67 */ "F10",
/* 68 */ "F11",
/* 69 */ "F12",
/* 70 */ "print screen",
/* 71 */ "scroll lock",
/* 72 */ "pause",
/* 73 */ "insert",
/* 74 */ "home",
/* 75 */ "page up",
/* 76 */ "delete",
/* 77 */ "end",
/* 78 */ "page down",
/* 79 */ "right",
/* 80 */ "left",
/* 81 */ "down",
/* 82 */ "up",
/* 83 */ "num lock",
/* 84 */ "SDLK_KP_DIVIDE",
/* 85 */ "SDLK_KP_MULTIPLY",
/* 86 */ "SDLK_KP_MINUS",
/* 87 */ "SDLK_KP_PLUS",
/* 88 */ "enter",
/* 89 */ "SDLK_KP_1",
/* 90 */ "SDLK_KP_2",
/* 91 */ "SDLK_KP_3",
/* 92 */ "SDLK_KP_4",
/* 93 */ "SDLK_KP_5",
/* 94 */ "SDLK_KP_6",
/* 95 */ "SDLK_KP_7",
/* 96 */ "SDLK_KP_8",
/* 97 */ "SDLK_KP_9",
/* 98 */ "SDLK_KP_0",
/* 99 */ "SDLK_KP_PERIOD",
/* 100 */ "SDLK_NONUSBACKSLASH",
/* 101 */ "application",
/* 102 */ "power",
/* 103 */ "SDLK_KP_EQUALS",
/* 104 */ "F13",
/* 105 */ "F14",
/* 106 */ "F15",
/* 107 */ "F16",
/* 108 */ "F17",
/* 109 */ "F18",
/* 110 */ "F19",
/* 111 */ "F20",
/* 112 */ "F21",
/* 113 */ "F22",
/* 114 */ "F23",
/* 115 */ "F24",
/* 116 */ "execute",
/* 117 */ "help",
/* 118 */ "menu",
/* 119 */ "select",
/* 120 */ "stop",
/* 121 */ "again",
/* 122 */ "undo",
/* 123 */ "cut",
/* 124 */ "copy",
/* 125 */ "paste",
/* 126 */ "find",
/* 127 */ "mute",
/* 128 */ "volume up",
/* 129 */ "volume down",
/* 130 */ "caps lock", /* unused */
/* 131 */ "num lock", /* unused */
/* 132 */ "scroll lock", /* unused */
/* 133 */ "SDLK_KP_COMMA",
/* 134 */ "SDLK_KP_EQUALSAS400",
/* 135 */ "international 1",
/* 136 */ "international 2",
/* 137 */ "international 3",
/* 138 */ "international 4",
/* 139 */ "international 5",
/* 140 */ "international 6",
/* 141 */ "international 7",
/* 142 */ "international 8",
/* 143 */ "international 9",
/* 144 */ "lang 1",
/* 145 */ "lang 2",
/* 146 */ "lang 3",
/* 147 */ "lang 4",
/* 148 */ "lang 5",
/* 149 */ "lang 6",
/* 150 */ "lang 7",
/* 151 */ "lang 8",
/* 152 */ "lang 9",
/* 153 */ "alt erase",
/* 154 */ "sys req",
/* 155 */ "cancel",
/* 156 */ "clear",
/* 157 */ "prior",
/* 158 */ "return",
/* 159 */ "separator",
/* 160 */ "out",
/* 161 */ "oper",
/* 162 */ "clear/again",
/* 163 */ "crsel/props",
/* 164 */ "exsel",
/* 165 */ NULL, /* unused */
/* 166 */ NULL, /* unused */
/* 167 */ NULL, /* unused */
/* 168 */ NULL, /* unused */
/* 169 */ NULL, /* unused */
/* 170 */ NULL, /* unused */
/* 171 */ NULL, /* unused */
/* 172 */ NULL, /* unused */
/* 173 */ NULL, /* unused */
/* 174 */ NULL, /* unused */
/* 175 */ NULL, /* unused */
/* 176 */ "[00]",
/* 177 */ "[000]",
/* 178 */ "thousands separator",
/* 179 */ "decimal separator",
/* 180 */ "currency unit",
/* 181 */ "currency sub-unit",
/* 182 */ "[(]",
/* 183 */ "[)]",
/* 184 */ "[{]",
/* 185 */ "[}]",
/* 186 */ "[tab]",
/* 187 */ "[backspace]",
/* 188 */ "[A]",
/* 189 */ "[B]",
/* 190 */ "[C]",
/* 191 */ "[D]",
/* 192 */ "[E]",
/* 193 */ "[F]",
/* 194 */ "[XOR]",
/* 195 */ "[^]",
/* 196 */ "[%]",
/* 197 */ "[<]",
/* 198 */ "[>]",
/* 199 */ "[&]",
/* 200 */ "[&&]",
/* 201 */ "[|]",
/* 202 */ "[||]",
/* 203 */ "[:]",
/* 204 */ "[#]",
/* 205 */ "[space]",
/* 206 */ "[@]",
/* 207 */ "[!]",
/* 208 */ "[mem store]",
/* 209 */ "[mem recall]",
/* 210 */ "[mem clear]",
/* 211 */ "[mem +]",
/* 212 */ "[mem -]",
/* 213 */ "[mem *]",
/* 214 */ "[mem /]",
/* 215 */ "[+/-]",
/* 216 */ "[clear]",
/* 217 */ "[clear entry]",
/* 218 */ "[binary]",
/* 219 */ "[octal]",
/* 220 */ "[decimal]",
/* 221 */ "[hexadecimal]",
/* 222 */ NULL, /* unused */
/* 223 */ NULL, /* unused */
/* 224 */ "left ctrl",
/* 225 */ "left shift",
/* 226 */ "left alt",
/* 227 */ "left meta",
/* 228 */ "right ctrl",
/* 229 */ "right shift",
/* 230 */ "right alt",
/* 231 */ "right meta",
/* 232 */ "mode",
/* 233 */ NULL, /* unused */
/* 234 */ NULL, /* unused */
/* 235 */ NULL, /* unused */
/* 236 */ "brightness down",
/* 237 */ "brightness up",
/* 238 */ "display switch",
/* 239 */ "kbd illum toggle",
/* 240 */ "kbd illum down",
/* 241 */ "kbd illum up",
/* 242 */ "eject",
/* 243 */ "sleep",
/* 244 */ "play",
/* 245 */ "stop",
/* 246 */ "previous",
/* 247 */ "next",
/* 248 */ "calc",
/* 249 */ "www",
/* 250 */ "e-mail",
/* 251 */ "media",
/* 252 */ "computer",
/* 253 */ "search",
/* 254 */ "bookmarks",
/* 255 */ "back",
/* 256 */ "forward",
/* 257 */ "reload",
/* 258 */ "stop"
};
/* *INDENT-ON* */