SDL_keyboard.c File Reference
`#include "../SDL_internal.h"`
`#include "SDL_timer.h"`
`#include "SDL_events.h"`
`#include "SDL_events_c.h"`
`#include "SDL_assert.h"`
`#include "../video/SDL_sysvideo.h"`
Include dependency graph for SDL_keyboard.c:

Go to the source code of this file.

## Data Structures

struct | SDL_Keyboard

## Functions

char * | SDL_UCS4ToUTF8 (Uint32 ch, char *dst)
int | SDL_KeyboardInit (void)
void | SDL_ResetKeyboard (void)
void | SDL_GetDefaultKeymap (SDL_Keycode *keymap)
void | SDL_SetKeymap (int start, SDL_Keycode *keys, int length)
void | SDL_SetScancodeName (SDL_Scancode scancode, const char *name)
SDL_Window * | SDL_GetKeyboardFocus (void)
| Get the window which currently has keyboard focus. More...
void | SDL_SetKeyboardFocus (SDL_Window *window)
int | SDL_SendKeyboardKey (Uint8 state, SDL_Scancode scancode)
int | SDL_SendKeyboardText (const char *text)
int | SDL_SendEditingText (const char *text, int start, int length)
void | SDL_KeyboardQuit (void)
const Uint8 * | SDL_GetKeyboardState (int *numkeys)
| Get a snapshot of the current state of the keyboard. More...
SDL_Keymod | SDL_GetModState (void)
| Get the current key modifier state for the keyboard. More...
void | SDL_SetModState (SDL_Keymod modstate)
| Set the current key modifier state for the keyboard. More...
void | SDL_ToggleModState (const SDL_Keymod modstate, const SDL_bool toggle)
SDL_Keycode | SDL_GetKeyFromScancode (SDL_Scancode scancode)
| Get the key code corresponding to the given scancode according to the current keyboard layout. More...
SDL_Scancode | SDL_GetScancodeFromKey (SDL_Keycode key)
| Get the scancode corresponding to the given key code according to the current keyboard layout. More...
const char * | SDL_GetScancodeName (SDL_Scancode scancode)
| Get a human-readable name for a scancode. More...
SDL_Scancode | SDL_GetScancodeFromName (const char *name)
| Get a scancode from a human-readable name. More...
const char * | SDL_GetKeyName (SDL_Keycode key)
| Get a human-readable name for a key. More...
SDL_Keycode | SDL_GetKeyFromName (const char *name)
| Get a key code from a human-readable name. More...

## Variables

static SDL_Keyboard | SDL_keyboard
static const SDL_Keycode | SDL_default_keymap [SDL_NUM_SCANCODES]
static const char * | SDL_scancode_names [SDL_NUM_SCANCODES]

## Function Documentation

void SDL_GetDefaultKeymap | ( | SDL_Keycode * | keymap | ) |

Definition at line 580 of file SDL_keyboard.c.

References SDL_default_keymap, and SDL_memcpy.

{
SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
}
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:393
SDL_default_keymap
static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:49

SDL_Window* SDL_GetKeyboardFocus | ( | void |  | ) |

Get the window which currently has keyboard focus.

Definition at line 604 of file SDL_keyboard.c.

References SDL_Keyboard::focus, and SDL_keyboard.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
return keyboard->focus;
}
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_Keyboard::focus
SDL_Window * focus
Definition: SDL_keyboard.c:41
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

const Uint8* SDL_GetKeyboardState | ( | int * | numkeys | ) |

Get a snapshot of the current state of the keyboard.

Parameters

  numkeys | if non-NULL, receives the length of the returned array.

Returns
  An array of key states. Indexes into this array are obtained by using SDL_Scancode values.

Example:

const Uint8 *state = SDL_GetKeyboardState(NULL);
if ( state[SDL_SCANCODE_RETURN] ) {
printf("<RETURN> is pressed.\n");
}

Definition at line 822 of file SDL_keyboard.c.

References SDL_Keyboard::keystate, SDL_keyboard, and SDL_NUM_SCANCODES.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
if (numkeys != (int *) 0) {
*numkeys = SDL_NUM_SCANCODES;
}
return keyboard->keystate;
}
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_NUM_SCANCODES
Definition: SDL_scancode.h:395
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
SDL_Keyboard::keystate
Uint8 keystate[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:43

SDL_Keycode SDL_GetKeyFromName | ( | const char * | name | ) |

Get a key code from a human-readable name.

Returns
  key code, or SDLK_UNKNOWN if the name wasn't recognized

See also
  SDL_Keycode

Definition at line 967 of file SDL_keyboard.c.

References i, NULL, SDL_default_keymap, SDL_GetScancodeFromName(), SDL_strlen, and SDLK_UNKNOWN.

{
SDL_Keycode key;
/* Check input */
if (name == NULL) return SDLK_UNKNOWN;
/* If it's a single UTF-8 character, then that's the keycode itself */
key = *(const unsigned char *)name;
if (key >= 0xF0) {
if (SDL_strlen(name) == 4) {
int i = 0;
key = (Uint16)(name[i]&0x07) << 18;
key |= (Uint16)(name[++i]&0x3F) << 12;
key |= (Uint16)(name[++i]&0x3F) << 6;
key |= (Uint16)(name[++i]&0x3F);
return key;
}
return SDLK_UNKNOWN;
} else if (key >= 0xE0) {
if (SDL_strlen(name) == 3) {
int i = 0;
key = (Uint16)(name[i]&0x0F) << 12;
key |= (Uint16)(name[++i]&0x3F) << 6;
key |= (Uint16)(name[++i]&0x3F);
return key;
}
return SDLK_UNKNOWN;
} else if (key >= 0xC0) {
if (SDL_strlen(name) == 2) {
int i = 0;
key = (Uint16)(name[i]&0x1F) << 6;
key |= (Uint16)(name[++i]&0x3F);
return key;
}
return SDLK_UNKNOWN;
} else {
if (SDL_strlen(name) == 1) {
if (key >= 'A' && key <= 'Z') {
key += 32;
}
return key;
}
/* Get the scancode for this name, and the associated keycode */
return SDL_default_keymap[SDL_GetScancodeFromName(name)];
}
}
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_Keycode
Sint32 SDL_Keycode
The SDL virtual key representation.
Definition: SDL_keycode.h:42
SDL_GetScancodeFromName
SDL_Scancode SDL_GetScancodeFromName(const char *name)
Get a scancode from a human-readable name.
Definition: SDL_keyboard.c:905
SDL_default_keymap
static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:49
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
NULL
#define NULL
Definition: begin_code.h:143
SDLK_UNKNOWN
Definition: SDL_keycode.h:49
SDL_strlen
#define SDL_strlen
Definition: SDL_dynapi_overrides.h:399
Uint16
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:147

SDL_Keycode SDL_GetKeyFromScancode | ( | SDL_Scancode | scancode | ) |

Get the key code corresponding to the given scancode according to the current keyboard layout.

See SDL_Keycode for details.

See also
  SDL_GetKeyName()

Definition at line 862 of file SDL_keyboard.c.

References SDL_Keyboard::keymap, SDL_InvalidParamError, SDL_keyboard, and SDL_NUM_SCANCODES.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
if (scancode < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
SDL_InvalidParamError("scancode");
return 0;
}
return keyboard->keymap[scancode];
}
SDL_Keyboard::keymap
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_NUM_SCANCODES
Definition: SDL_scancode.h:395
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

const char* SDL_GetKeyName | ( | SDL_Keycode | key | ) |

Get a human-readable name for a key.

Returns
  A pointer to a UTF-8 string that stays valid at least until the next call to this function. If you need it around any longer, you must copy it. If the key doesn't have a name, this function returns an empty string ("").

See also
  SDL_Key

Definition at line 928 of file SDL_keyboard.c.

References SDL_GetScancodeName(), SDL_SCANCODE_BACKSPACE, SDL_SCANCODE_DELETE, SDL_SCANCODE_ESCAPE, SDL_SCANCODE_RETURN, SDL_SCANCODE_SPACE, SDL_SCANCODE_TAB, SDL_UCS4ToUTF8(), SDLK_BACKSPACE, SDLK_DELETE, SDLK_ESCAPE, SDLK_RETURN, SDLK_SCANCODE_MASK, SDLK_SPACE, and SDLK_TAB.

{
static char name[8];
char *end;
if (key & SDLK_SCANCODE_MASK) {
return
SDL_GetScancodeName((SDL_Scancode) (key & ~SDLK_SCANCODE_MASK));
}
switch (key) {
case SDLK_RETURN:
return SDL_GetScancodeName(SDL_SCANCODE_RETURN);
case SDLK_ESCAPE:
return SDL_GetScancodeName(SDL_SCANCODE_ESCAPE);
case SDLK_BACKSPACE:
return SDL_GetScancodeName(SDL_SCANCODE_BACKSPACE);
case SDLK_TAB:
return SDL_GetScancodeName(SDL_SCANCODE_TAB);
case SDLK_SPACE:
return SDL_GetScancodeName(SDL_SCANCODE_SPACE);
case SDLK_DELETE:
return SDL_GetScancodeName(SDL_SCANCODE_DELETE);
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;
}
end = SDL_UCS4ToUTF8((Uint32) key, name);
*end = '\0';
return name;
}
}
SDL_SCANCODE_RETURN
Definition: SDL_scancode.h:92
SDLK_DELETE
Definition: SDL_keycode.h:145
SDLK_BACKSPACE
Definition: SDL_keycode.h:53
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:157
end
GLuint GLuint end
Definition: SDL_opengl.h:1564
SDLK_TAB
Definition: SDL_keycode.h:54
SDLK_SCANCODE_MASK
#define SDLK_SCANCODE_MASK
Definition: SDL_keycode.h:44
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_SCANCODE_TAB
Definition: SDL_scancode.h:95
SDLK_RETURN
Definition: SDL_keycode.h:51
SDL_SCANCODE_ESCAPE
Definition: SDL_scancode.h:93
SDL_SCANCODE_SPACE
Definition: SDL_scancode.h:96
SDL_GetScancodeName
const char * SDL_GetScancodeName(SDL_Scancode scancode)
Get a human-readable name for a scancode.
Definition: SDL_keyboard.c:890
SDL_SCANCODE_BACKSPACE
Definition: SDL_scancode.h:94
SDL_UCS4ToUTF8
char * SDL_UCS4ToUTF8(Uint32 ch, char *dst)
Definition: SDL_keyboard.c:512
SDL_SCANCODE_DELETE
Definition: SDL_scancode.h:173
SDLK_ESCAPE
Definition: SDL_keycode.h:52
SDL_Scancode
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:43
SDLK_SPACE
Definition: SDL_keycode.h:55

SDL_Keymod SDL_GetModState | ( | void |  | ) |

Get the current key modifier state for the keyboard.

Definition at line 833 of file SDL_keyboard.c.

References SDL_Keyboard::modstate, and SDL_keyboard.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
return keyboard->modstate;
}
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_Keyboard::modstate
Uint16 modstate
Definition: SDL_keyboard.c:42
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

SDL_Scancode SDL_GetScancodeFromKey | ( | SDL_Keycode | key | ) |

Get the scancode corresponding to the given key code according to the current keyboard layout.

See SDL_Scancode for details.

See also
  SDL_GetScancodeName()

Definition at line 875 of file SDL_keyboard.c.

References SDL_Keyboard::keymap, SDL_keyboard, SDL_NUM_SCANCODES, and SDL_SCANCODE_UNKNOWN.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
SDL_Scancode scancode;
for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
++scancode) {
if (keyboard->keymap[scancode] == key) {
return scancode;
}
}
return SDL_SCANCODE_UNKNOWN;
}
SDL_Keyboard::keymap
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_SCANCODE_UNKNOWN
Definition: SDL_scancode.h:45
SDL_NUM_SCANCODES
Definition: SDL_scancode.h:395
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
SDL_Scancode
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:43

SDL_Scancode SDL_GetScancodeFromName | ( | const char * | name | ) |

Get a scancode from a human-readable name.

Returns
  scancode, or SDL_SCANCODE_UNKNOWN if the name wasn't recognized

See also
  SDL_Scancode

Definition at line 905 of file SDL_keyboard.c.

References i, SDL_arraysize, SDL_InvalidParamError, SDL_scancode_names, SDL_SCANCODE_UNKNOWN, and SDL_strcasecmp.

Referenced by SDL_GetKeyFromName().

{
int i;
if (!name || !*name) {
SDL_InvalidParamError("name");
return SDL_SCANCODE_UNKNOWN;
}
for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
if (!SDL_scancode_names[i]) {
continue;
}
if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
return (SDL_Scancode)i;
}
}
SDL_InvalidParamError("name");
return SDL_SCANCODE_UNKNOWN;
}
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_strcasecmp
#define SDL_strcasecmp
Definition: SDL_dynapi_overrides.h:425
SDL_scancode_names
static const char * SDL_scancode_names[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:278
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
SDL_SCANCODE_UNKNOWN
Definition: SDL_scancode.h:45
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
SDL_Scancode
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:43

const char* SDL_GetScancodeName | ( | SDL_Scancode | scancode | ) |

Get a human-readable name for a scancode.

Returns
  A pointer to the name for the scancode. If the scancode doesn't have a name, this function returns an empty string ("").

See also
  SDL_Scancode

Definition at line 890 of file SDL_keyboard.c.

References SDL_InvalidParamError, SDL_NUM_SCANCODES, and SDL_scancode_names.

Referenced by SDL_GetKeyName(), and SDL_SendKeyboardKey().

{
const char *name;
if (scancode < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
SDL_InvalidParamError("scancode");
return "";
}
name = SDL_scancode_names[scancode];
if (name)
return name;
else
return "";
}
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_scancode_names
static const char * SDL_scancode_names[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:278
SDL_NUM_SCANCODES
Definition: SDL_scancode.h:395

int SDL_KeyboardInit | ( | void |  | ) |

Definition at line 554 of file SDL_keyboard.c.

References SDL_Keyboard::keymap, SDL_default_keymap, SDL_keyboard, and SDL_memcpy.

Referenced by SDL_VideoInit().

{
SDL_Keyboard *keyboard = &SDL_keyboard;
/* Set the default keymap */
SDL_memcpy(keyboard->keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
return (0);
}
SDL_Keyboard::keymap
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:393
SDL_default_keymap
static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:49
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

void SDL_KeyboardQuit | ( | void |  | ) |

Definition at line 817 of file SDL_keyboard.c.

Referenced by SDL_VideoQuit().

{
}

void SDL_ResetKeyboard | ( | void |  | ) |

Definition at line 564 of file SDL_keyboard.c.

References SDL_Keyboard::keystate, SDL_keyboard, SDL_NUM_SCANCODES, SDL_PRESSED, SDL_RELEASED, and SDL_SendKeyboardKey().

Referenced by SDL_SetKeyboardFocus(), and SDL_ShowMessageBox().

{
SDL_Keyboard *keyboard = &SDL_keyboard;
SDL_Scancode scancode;
#ifdef DEBUG_KEYBOARD
printf("Resetting keyboard\n");
#endif
for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) {
if (keyboard->keystate[scancode] == SDL_PRESSED) {
SDL_SendKeyboardKey(SDL_RELEASED, scancode);
}
}
}
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_SendKeyboardKey
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
Definition: SDL_keyboard.c:661
SDL_NUM_SCANCODES
Definition: SDL_scancode.h:395
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_Keyboard::keystate
Uint8 keystate[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:43
SDL_RELEASED
#define SDL_RELEASED
Definition: SDL_events.h:49
SDL_Scancode
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:43

int SDL_SendEditingText | ( | const char * | text,
|  | int | start,
|  | int | length
| ) |  |

Definition at line 797 of file SDL_keyboard.c.

References SDL_Event::edit, SDL_Keyboard::focus, SDL_Window::id, SDL_arraysize, SDL_ENABLE, SDL_GetEventState, SDL_keyboard, SDL_PushEvent, SDL_TEXTEDITING, SDL_utf8strlcpy, and SDL_TextEditingEvent::text.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
int posted;
/* Post the event, if desired */
posted = 0;
if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) {
SDL_Event event;
event.edit.type = SDL_TEXTEDITING;
event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
event.edit.start = start;
event.edit.length = length;
SDL_utf8strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text));
posted = (SDL_PushEvent(&event) > 0);
}
return (posted);
}
SDL_utf8strlcpy
#define SDL_utf8strlcpy
Definition: SDL_dynapi_overrides.h:401
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
start
GLuint start
Definition: SDL_opengl.h:1564
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
SDL_TextEditingEvent::text
char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: SDL_events.h:208
SDL_Event::edit
SDL_TextEditingEvent edit
Definition: SDL_events.h:527
text
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:34
SDL_Keyboard::focus
SDL_Window * focus
Definition: SDL_keyboard.c:41
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
SDL_Window::id
Uint32 id
Definition: SDL_sysvideo.h:74
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
SDL_Event
General event structure.
Definition: SDL_events.h:521
length
GLuint GLsizei GLsizei * length
Definition: SDL_opengl_glext.h:669
SDL_TEXTEDITING
Definition: SDL_events.h:95

int SDL_SendKeyboardKey | ( | Uint8 | state,
|  | SDL_Scancode | scancode
| ) |  |

Definition at line 661 of file SDL_keyboard.c.

References SDL_Keyboard::focus, SDL_Window::id, SDL_Keyboard::keymap, SDL_Keyboard::keystate, KMOD_CAPS, KMOD_LALT, KMOD_LCTRL, KMOD_LGUI, KMOD_LSHIFT, KMOD_MODE, KMOD_NONE, KMOD_NUM, KMOD_RALT, KMOD_RCTRL, KMOD_RGUI, KMOD_RSHIFT, SDL_Keyboard::modstate, SDL_ENABLE, SDL_GetEventState, SDL_GetScancodeName(), SDL_keyboard, SDL_KEYDOWN, SDL_KEYUP, SDL_PRESSED, SDL_PushEvent, SDL_RELEASED, SDLK_CAPSLOCK, SDLK_LALT, SDLK_LCTRL, SDLK_LGUI, SDLK_LSHIFT, SDLK_MODE, SDLK_NUMLOCKCLEAR, SDLK_RALT, SDLK_RCTRL, SDLK_RGUI, SDLK_RSHIFT, and state.

Referenced by SDL_BApp::_HandleKey(), SDL_ResetKeyboard(), and WINRT_OnBackButtonPressed().

{
SDL_Keyboard *keyboard = &SDL_keyboard;
int posted;
SDL_Keymod modifier;
SDL_Keycode keycode;
Uint16 modstate;
Uint32 type;
Uint8 repeat;
if (!scancode) {
return 0;
}
#ifdef DEBUG_KEYBOARD
printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
state == SDL_PRESSED ? "pressed" : "released");
#endif
/* 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;
}
/* Drop events that don't change state */
repeat = (state && keyboard->keystate[scancode]);
if (keyboard->keystate[scancode] == state && !repeat) {
#if 0
printf("Keyboard event didn't change state - dropped!\n");
#endif
return 0;
}
/* Update internal keyboard state */
keyboard->keystate[scancode] = state;
keycode = keyboard->keymap[scancode];
/* Update modifiers state if applicable */
switch (keycode) {
case SDLK_LCTRL:
modifier = KMOD_LCTRL;
break;
case SDLK_RCTRL:
modifier = KMOD_RCTRL;
break;
case SDLK_LSHIFT:
modifier = KMOD_LSHIFT;
break;
case SDLK_RSHIFT:
modifier = KMOD_RSHIFT;
break;
case SDLK_LALT:
modifier = KMOD_LALT;
break;
case SDLK_RALT:
modifier = KMOD_RALT;
break;
case SDLK_LGUI:
modifier = KMOD_LGUI;
break;
case SDLK_RGUI:
modifier = KMOD_RGUI;
break;
case SDLK_MODE:
modifier = KMOD_MODE;
break;
default:
modifier = KMOD_NONE;
break;
}
if (SDL_KEYDOWN == type) {
modstate = keyboard->modstate;
switch (keycode) {
case SDLK_NUMLOCKCLEAR:
keyboard->modstate ^= KMOD_NUM;
break;
case SDLK_CAPSLOCK:
keyboard->modstate ^= KMOD_CAPS;
break;
default:
keyboard->modstate |= modifier;
break;
}
} else {
keyboard->modstate &= ~modifier;
modstate = keyboard->modstate;
}
/* Post the event, if desired */
posted = 0;
if (SDL_GetEventState(type) == SDL_ENABLE) {
SDL_Event event;
event.key.type = type;
event.key.state = state;
event.key.repeat = repeat;
event.key.keysym.scancode = scancode;
event.key.keysym.sym = keycode;
event.key.keysym.mod = modstate;
event.key.windowID = keyboard->focus ? keyboard->focus->id : 0;
posted = (SDL_PushEvent(&event) > 0);
}
return (posted);
}
SDLK_RGUI
Definition: SDL_keycode.h:280
SDL_KEYDOWN
Definition: SDL_events.h:93
KMOD_LCTRL
Definition: SDL_keycode.h:322
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:157
SDLK_RSHIFT
Definition: SDL_keycode.h:278
KMOD_RCTRL
Definition: SDL_keycode.h:323
KMOD_LSHIFT
Definition: SDL_keycode.h:320
state
struct xkb_state * state
Definition: SDL_waylandsym.h:97
SDLK_MODE
Definition: SDL_keycode.h:282
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
SDLK_LALT
Definition: SDL_keycode.h:275
SDL_Keyboard::keymap
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDLK_CAPSLOCK
Definition: SDL_keycode.h:124
SDL_Keycode
Sint32 SDL_Keycode
The SDL virtual key representation.
Definition: SDL_keycode.h:42
KMOD_NONE
Definition: SDL_keycode.h:319
SDLK_RCTRL
Definition: SDL_keycode.h:277
KMOD_RGUI
Definition: SDL_keycode.h:327
SDL_Keyboard::modstate
Uint16 modstate
Definition: SDL_keyboard.c:42
SDLK_RALT
Definition: SDL_keycode.h:279
type
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1564
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
KMOD_CAPS
Definition: SDL_keycode.h:329
KMOD_MODE
Definition: SDL_keycode.h:330
SDL_KEYUP
Definition: SDL_events.h:94
KMOD_NUM
Definition: SDL_keycode.h:328
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
SDL_GetScancodeName
const char * SDL_GetScancodeName(SDL_Scancode scancode)
Get a human-readable name for a scancode.
Definition: SDL_keyboard.c:890
SDLK_LCTRL
Definition: SDL_keycode.h:273
SDL_Keymod
SDL_Keymod
Enumeration of valid key mods (possibly OR&#39;d together).
Definition: SDL_keycode.h:317
SDL_Keyboard::focus
SDL_Window * focus
Definition: SDL_keyboard.c:41
KMOD_LGUI
Definition: SDL_keycode.h:326
KMOD_LALT
Definition: SDL_keycode.h:324
SDLK_LSHIFT
Definition: SDL_keycode.h:274
SDLK_NUMLOCKCLEAR
Definition: SDL_keycode.h:153
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
SDL_Window::id
Uint32 id
Definition: SDL_sysvideo.h:74
KMOD_RALT
Definition: SDL_keycode.h:325
Uint16
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:147
SDL_Event
General event structure.
Definition: SDL_events.h:521
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_Keyboard::keystate
Uint8 keystate[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:43
SDL_RELEASED
#define SDL_RELEASED
Definition: SDL_events.h:49
SDLK_LGUI
Definition: SDL_keycode.h:276
KMOD_RSHIFT
Definition: SDL_keycode.h:321

int SDL_SendKeyboardText | ( | const char * | text | ) |

Definition at line 774 of file SDL_keyboard.c.

References SDL_Keyboard::focus, SDL_Window::id, SDL_arraysize, SDL_ENABLE, SDL_GetEventState, SDL_keyboard, SDL_PushEvent, SDL_TEXTINPUT, SDL_utf8strlcpy, SDL_TextInputEvent::text, and SDL_Event::text.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
int posted;
/* Don't post text events for unprintable characters */
if ((unsigned char)*text < ' ' || *text == 127) {
return 0;
}
/* Post the event, if desired */
posted = 0;
if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
SDL_Event event;
event.text.type = SDL_TEXTINPUT;
event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
SDL_utf8strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
posted = (SDL_PushEvent(&event) > 0);
}
return (posted);
}
SDL_utf8strlcpy
#define SDL_utf8strlcpy
Definition: SDL_dynapi_overrides.h:401
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_Event::text
SDL_TextInputEvent text
Definition: SDL_events.h:528
SDL_TextInputEvent::text
char text[SDL_TEXTINPUTEVENT_TEXT_SIZE]
Definition: SDL_events.h:223
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
text
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:34
SDL_Keyboard::focus
SDL_Window * focus
Definition: SDL_keyboard.c:41
SDL_TEXTINPUT
Definition: SDL_events.h:96
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
SDL_Window::id
Uint32 id
Definition: SDL_sysvideo.h:74
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
SDL_Event
General event structure.
Definition: SDL_events.h:521

void SDL_SetKeyboardFocus | ( | SDL_Window * | window | ) |

Definition at line 612 of file SDL_keyboard.c.

References SDL_Window::flags, SDL_Keyboard::focus, SDL_assert, SDL_CaptureMouse, SDL_EventState, SDL_FALSE, SDL_GetVideoDevice(), SDL_keyboard, SDL_QUERY, SDL_ResetKeyboard(), SDL_SendWindowEvent(), SDL_TEXTINPUT, SDL_WINDOW_MOUSE_CAPTURE, SDL_WINDOWEVENT_FOCUS_GAINED, SDL_WINDOWEVENT_FOCUS_LOST, SDL_VideoDevice::StartTextInput, SDL_VideoDevice::StopTextInput, and window.

Referenced by SDL_BApp::_HandleKeyboardFocus(), IsSDLWindowEventPending(), and SDL_DestroyWindow().

{
SDL_Keyboard *keyboard = &SDL_keyboard;
if (keyboard->focus && !window) {
/* We won't get anymore keyboard messages, so reset keyboard state */
SDL_ResetKeyboard();
}
/* See if the current window has lost focus */
if (keyboard->focus && keyboard->focus != window) {
/* new window shouldn't think it has mouse captured. */
SDL_assert(!window || !(window->flags & SDL_WINDOW_MOUSE_CAPTURE));
/* old window must lose an existing mouse capture. */
if (keyboard->focus->flags & SDL_WINDOW_MOUSE_CAPTURE) {
SDL_CaptureMouse(SDL_FALSE); /* drop the capture. */
SDL_assert(!(keyboard->focus->flags & SDL_WINDOW_MOUSE_CAPTURE));
}
SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST,
0, 0);
/* Ensures IME compositions are committed */
if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
SDL_VideoDevice *video = SDL_GetVideoDevice();
if (video && video->StopTextInput) {
video->StopTextInput(video);
}
}
}
keyboard->focus = window;
if (keyboard->focus) {
SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_GAINED,
0, 0);
if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
SDL_VideoDevice *video = SDL_GetVideoDevice();
if (video && video->StartTextInput) {
video->StartTextInput(video);
}
}
}
}
window
SDL_Window * window
Definition: testautomation_render.c:26
SDL_VideoDevice::StartTextInput
void(* StartTextInput)(_THIS)
Definition: SDL_sysvideo.h:255
SDL_SendWindowEvent
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
Definition: SDL_windowevents.c:74
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_WINDOWEVENT_FOCUS_GAINED
Definition: SDL_video.h:155
SDL_WINDOW_MOUSE_CAPTURE
Definition: SDL_video.h:112
SDL_WINDOWEVENT_FOCUS_LOST
Definition: SDL_video.h:156
SDL_VideoDevice::StopTextInput
void(* StopTextInput)(_THIS)
Definition: SDL_sysvideo.h:256
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:167
SDL_Keyboard::focus
SDL_Window * focus
Definition: SDL_keyboard.c:41
SDL_TEXTINPUT
Definition: SDL_events.h:96
SDL_EventState
#define SDL_EventState
Definition: SDL_dynapi_overrides.h:137
SDL_ResetKeyboard
void SDL_ResetKeyboard(void)
Definition: SDL_keyboard.c:564
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
SDL_FALSE
Definition: SDL_stdinc.h:128
SDL_CaptureMouse
#define SDL_CaptureMouse
Definition: SDL_dynapi_overrides.h:588
SDL_VideoDevice
Definition: SDL_sysvideo.h:142
SDL_GetVideoDevice
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:573
SDL_QUERY
#define SDL_QUERY
Definition: SDL_events.h:715
SDL_Window::flags
Uint32 flags
Definition: SDL_sysvideo.h:81

void SDL_SetKeymap | ( | int | start,
|  | SDL_Keycode * | keys,
|  | int | length
| ) |  |

Definition at line 586 of file SDL_keyboard.c.

References SDL_Keyboard::keymap, SDL_keyboard, SDL_memcpy, and SDL_NUM_SCANCODES.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
if (start < 0 || start + length > SDL_NUM_SCANCODES) {
return;
}
SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length);
}
start
GLuint start
Definition: SDL_opengl.h:1564
SDL_Keyboard::keymap
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:393
SDL_NUM_SCANCODES
Definition: SDL_scancode.h:395
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
length
GLuint GLsizei GLsizei * length
Definition: SDL_opengl_glext.h:669

void SDL_SetModState | ( | SDL_Keymod | modstate | ) |

Set the current key modifier state for the keyboard.

Note
  This does not change the keyboard state, only the key modifier flags.

Definition at line 841 of file SDL_keyboard.c.

References SDL_Keyboard::modstate, and SDL_keyboard.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
keyboard->modstate = modstate;
}
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_Keyboard::modstate
Uint16 modstate
Definition: SDL_keyboard.c:42
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

void SDL_SetScancodeName | ( | SDL_Scancode | scancode,
|  | const char * | name
| ) |  |

Definition at line 598 of file SDL_keyboard.c.

References SDL_scancode_names.

{
SDL_scancode_names[scancode] = name;
}
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_scancode_names
static const char * SDL_scancode_names[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:278

void SDL_ToggleModState | ( | const SDL_Keymod | modstate,
|  | const SDL_bool | toggle
| ) |  |

Definition at line 850 of file SDL_keyboard.c.

References SDL_Keyboard::modstate, and SDL_keyboard.

{
SDL_Keyboard *keyboard = &SDL_keyboard;
if (toggle) {
keyboard->modstate |= modstate;
} else {
keyboard->modstate &= ~modstate;
}
}
SDL_Keyboard
Definition: SDL_keyboard.c:38
SDL_Keyboard::modstate
Uint16 modstate
Definition: SDL_keyboard.c:42
SDL_keyboard
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

char* SDL_UCS4ToUTF8 | ( | Uint32 | ch,
|  | char * | dst
| ) |  |

Definition at line 512 of file SDL_keyboard.c.

Referenced by SDL_GetKeyName().

{
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;
}
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1737
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11090
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139

## Variable Documentation

const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES]

static

Definition at line 49 of file SDL_keyboard.c.

Referenced by SDL_GetDefaultKeymap(), SDL_GetKeyFromName(), and SDL_KeyboardInit().

SDL_Keyboard SDL_keyboard

static

Definition at line 47 of file SDL_keyboard.c.

Referenced by SDL_GetKeyboardFocus(), SDL_GetKeyboardState(), SDL_GetKeyFromScancode(), SDL_GetModState(), SDL_GetScancodeFromKey(), SDL_KeyboardInit(), SDL_ResetKeyboard(), SDL_SendEditingText(), SDL_SendKeyboardKey(), SDL_SendKeyboardText(), SDL_SetKeyboardFocus(), SDL_SetKeymap(), SDL_SetModState(), and SDL_ToggleModState().

const char* SDL_scancode_names[SDL_NUM_SCANCODES]

static

Definition at line 278 of file SDL_keyboard.c.

Referenced by SDL_GetScancodeFromName(), SDL_GetScancodeName(), and SDL_SetScancodeName().