SDL  2.0
SDL_keyboard.c File Reference
#include "../SDL_internal.h"
#include "SDL_timer.h"
#include "SDL_events.h"
#include "SDL_events_c.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
 

Macros

#define KEYBOARD_HARDWARE   0x01
 
#define KEYBOARD_AUTORELEASE   0x02
 

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_WindowSDL_GetKeyboardFocus (void)
 Get the window which currently has keyboard focus. More...
 
void SDL_SetKeyboardFocus (SDL_Window *window)
 
static int SDL_SendKeyboardKeyInternal (Uint8 source, Uint8 state, SDL_Scancode scancode)
 
int SDL_SendKeyboardKey (Uint8 state, SDL_Scancode scancode)
 
int SDL_SendKeyboardKeyAutoRelease (SDL_Scancode scancode)
 
void SDL_ReleaseAutoReleaseKeys (void)
 
SDL_bool SDL_HardwareKeyboardKeyPressed (void)
 
int SDL_SendKeyboardText (const char *text)
 
int SDL_SendEditingText (const char *text, int start, int length)
 
void SDL_KeyboardQuit (void)
 
const Uint8SDL_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]
 

Macro Definition Documentation

◆ KEYBOARD_AUTORELEASE

#define KEYBOARD_AUTORELEASE   0x02

Definition at line 36 of file SDL_keyboard.c.

◆ KEYBOARD_HARDWARE

#define KEYBOARD_HARDWARE   0x01

Definition at line 35 of file SDL_keyboard.c.

Function Documentation

◆ SDL_GetDefaultKeymap()

void SDL_GetDefaultKeymap ( SDL_Keycode keymap)

Definition at line 592 of file SDL_keyboard.c.

593 {
595 }
#define SDL_memcpy
static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:53

References SDL_default_keymap, and SDL_memcpy.

◆ SDL_GetKeyboardFocus()

SDL_Window* SDL_GetKeyboardFocus ( void  )

Get the window which currently has keyboard focus.

Definition at line 626 of file SDL_keyboard.c.

627 {
628  SDL_Keyboard *keyboard = &SDL_keyboard;
629 
630  return keyboard->focus;
631 }
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:51
SDL_Window * focus
Definition: SDL_keyboard.c:43

References SDL_Keyboard::focus, and SDL_keyboard.

◆ SDL_GetKeyboardState()

const Uint8* SDL_GetKeyboardState ( int numkeys)

Get a snapshot of the current state of the keyboard.

Parameters
numkeysif 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:

printf("<RETURN> is pressed.\n");
}
#define SDL_GetKeyboardState
@ SDL_SCANCODE_RETURN
Definition: SDL_scancode.h:92
uint8_t Uint8
Definition: SDL_stdinc.h:185
struct xkb_state * state
#define NULL
Definition: begin_code.h:163

Definition at line 896 of file SDL_keyboard.c.

897 {
898  SDL_Keyboard *keyboard = &SDL_keyboard;
899 
900  if (numkeys != (int *) 0) {
901  *numkeys = SDL_NUM_SCANCODES;
902  }
903  return keyboard->keystate;
904 }
@ SDL_NUM_SCANCODES
Definition: SDL_scancode.h:407
Uint8 keystate[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:46

References SDL_Keyboard::keystate, SDL_keyboard, and SDL_NUM_SCANCODES.

◆ SDL_GetKeyFromName()

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 1041 of file SDL_keyboard.c.

1042 {
1043  SDL_Keycode key;
1044 
1045  /* Check input */
1046  if (name == NULL) {
1047  return SDLK_UNKNOWN;
1048  }
1049 
1050  /* If it's a single UTF-8 character, then that's the keycode itself */
1051  key = *(const unsigned char *)name;
1052  if (key >= 0xF0) {
1053  if (SDL_strlen(name) == 4) {
1054  int i = 0;
1055  key = (Uint16)(name[i]&0x07) << 18;
1056  key |= (Uint16)(name[++i]&0x3F) << 12;
1057  key |= (Uint16)(name[++i]&0x3F) << 6;
1058  key |= (Uint16)(name[++i]&0x3F);
1059  return key;
1060  }
1061  return SDLK_UNKNOWN;
1062  } else if (key >= 0xE0) {
1063  if (SDL_strlen(name) == 3) {
1064  int i = 0;
1065  key = (Uint16)(name[i]&0x0F) << 12;
1066  key |= (Uint16)(name[++i]&0x3F) << 6;
1067  key |= (Uint16)(name[++i]&0x3F);
1068  return key;
1069  }
1070  return SDLK_UNKNOWN;
1071  } else if (key >= 0xC0) {
1072  if (SDL_strlen(name) == 2) {
1073  int i = 0;
1074  key = (Uint16)(name[i]&0x1F) << 6;
1075  key |= (Uint16)(name[++i]&0x3F);
1076  return key;
1077  }
1078  return SDLK_UNKNOWN;
1079  } else {
1080  if (SDL_strlen(name) == 1) {
1081  if (key >= 'A' && key <= 'Z') {
1082  key += 32;
1083  }
1084  return key;
1085  }
1086 
1087  /* Get the scancode for this name, and the associated keycode */
1089  }
1090 }
#define SDL_strlen
SDL_Scancode SDL_GetScancodeFromName(const char *name)
Get a scancode from a human-readable name.
Definition: SDL_keyboard.c:979
@ SDLK_UNKNOWN
Definition: SDL_keycode.h:52
Sint32 SDL_Keycode
The SDL virtual key representation.
Definition: SDL_keycode.h:45
GLuint const GLchar * name
uint16_t Uint16
Definition: SDL_stdinc.h:197
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:50
GLuint64 key
Definition: gl2ext.h:2192

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

◆ SDL_GetKeyFromScancode()

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 936 of file SDL_keyboard.c.

937 {
938  SDL_Keyboard *keyboard = &SDL_keyboard;
939 
940  if (((int)scancode) < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
941  SDL_InvalidParamError("scancode");
942  return 0;
943  }
944 
945  return keyboard->keymap[scancode];
946 }
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:90
@ SDL_SCANCODE_UNKNOWN
Definition: SDL_scancode.h:45
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:47

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

◆ SDL_GetKeyName()

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_Keycode

Definition at line 1002 of file SDL_keyboard.c.

1003 {
1004  static char name[8];
1005  char *end;
1006 
1007  if (key & SDLK_SCANCODE_MASK) {
1008  return
1010  }
1011 
1012  switch (key) {
1013  case SDLK_RETURN:
1015  case SDLK_ESCAPE:
1017  case SDLK_BACKSPACE:
1019  case SDLK_TAB:
1021  case SDLK_SPACE:
1023  case SDLK_DELETE:
1025  default:
1026  /* Unaccented letter keys on latin keyboards are normally
1027  labeled in upper case (and probably on others like Greek or
1028  Cyrillic too, so if you happen to know for sure, please
1029  adapt this). */
1030  if (key >= 'a' && key <= 'z') {
1031  key -= 32;
1032  }
1033 
1035  *end = '\0';
1036  return name;
1037  }
1038 }
char * SDL_UCS4ToUTF8(Uint32 ch, char *dst)
Definition: SDL_keyboard.c:524
const char * SDL_GetScancodeName(SDL_Scancode scancode)
Get a human-readable name for a scancode.
Definition: SDL_keyboard.c:964
@ SDLK_BACKSPACE
Definition: SDL_keycode.h:56
@ SDLK_TAB
Definition: SDL_keycode.h:57
@ SDLK_ESCAPE
Definition: SDL_keycode.h:55
@ SDLK_SPACE
Definition: SDL_keycode.h:58
@ SDLK_DELETE
Definition: SDL_keycode.h:150
@ SDLK_RETURN
Definition: SDL_keycode.h:54
#define SDLK_SCANCODE_MASK
Definition: SDL_keycode.h:47
GLuint GLuint end
Definition: SDL_opengl.h:1571
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:44
@ SDL_SCANCODE_ESCAPE
Definition: SDL_scancode.h:93
@ SDL_SCANCODE_DELETE
Definition: SDL_scancode.h:173
@ SDL_SCANCODE_TAB
Definition: SDL_scancode.h:95
@ SDL_SCANCODE_BACKSPACE
Definition: SDL_scancode.h:94
@ SDL_SCANCODE_SPACE
Definition: SDL_scancode.h:96
uint32_t Uint32
Definition: SDL_stdinc.h:209

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.

◆ SDL_GetModState()

SDL_Keymod SDL_GetModState ( void  )

Get the current key modifier state for the keyboard.

Definition at line 907 of file SDL_keyboard.c.

908 {
909  SDL_Keyboard *keyboard = &SDL_keyboard;
910 
911  return (SDL_Keymod) keyboard->modstate;
912 }
SDL_Keymod
Enumeration of valid key mods (possibly OR'd together).
Definition: SDL_keycode.h:328
Uint16 modstate
Definition: SDL_keyboard.c:44

References SDL_Keyboard::modstate, and SDL_keyboard.

◆ SDL_GetScancodeFromKey()

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 949 of file SDL_keyboard.c.

950 {
951  SDL_Keyboard *keyboard = &SDL_keyboard;
952  SDL_Scancode scancode;
953 
954  for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
955  ++scancode) {
956  if (keyboard->keymap[scancode] == key) {
957  return scancode;
958  }
959  }
960  return SDL_SCANCODE_UNKNOWN;
961 }

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

◆ SDL_GetScancodeFromName()

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 979 of file SDL_keyboard.c.

980 {
981  int i;
982 
983  if (!name || !*name) {
984  SDL_InvalidParamError("name");
985  return SDL_SCANCODE_UNKNOWN;
986  }
987 
988  for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
989  if (!SDL_scancode_names[i]) {
990  continue;
991  }
992  if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
993  return (SDL_Scancode)i;
994  }
995  }
996 
997  SDL_InvalidParamError("name");
998  return SDL_SCANCODE_UNKNOWN;
999 }
#define SDL_strcasecmp
static const char * SDL_scancode_names[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:286
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:121

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

Referenced by SDL_GetKeyFromName().

◆ SDL_GetScancodeName()

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 964 of file SDL_keyboard.c.

965 {
966  const char *name;
967  if (((int)scancode) < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
968  SDL_InvalidParamError("scancode");
969  return "";
970  }
971 
972  name = SDL_scancode_names[scancode];
973  if (name)
974  return name;
975  else
976  return "";
977 }

References SDL_InvalidParamError, SDL_NUM_SCANCODES, SDL_scancode_names, and SDL_SCANCODE_UNKNOWN.

Referenced by SDL_GetKeyName(), and SDL_SendKeyboardKeyInternal().

◆ SDL_HardwareKeyboardKeyPressed()

SDL_bool SDL_HardwareKeyboardKeyPressed ( void  )

Definition at line 834 of file SDL_keyboard.c.

835 {
836  SDL_Keyboard *keyboard = &SDL_keyboard;
837  SDL_Scancode scancode;
838 
839  for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES; ++scancode) {
840  if ((keyboard->keysource[scancode] & KEYBOARD_HARDWARE) != 0) {
841  return SDL_TRUE;
842  }
843  }
844  return SDL_FALSE;
845 }
#define KEYBOARD_HARDWARE
Definition: SDL_keyboard.c:35
@ SDL_TRUE
Definition: SDL_stdinc.h:170
@ SDL_FALSE
Definition: SDL_stdinc.h:169
Uint8 keysource[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:45

References KEYBOARD_HARDWARE, SDL_Keyboard::keysource, SDL_FALSE, SDL_keyboard, SDL_NUM_SCANCODES, SDL_SCANCODE_UNKNOWN, and SDL_TRUE.

◆ SDL_KeyboardInit()

int SDL_KeyboardInit ( void  )

Definition at line 566 of file SDL_keyboard.c.

567 {
568  SDL_Keyboard *keyboard = &SDL_keyboard;
569 
570  /* Set the default keymap */
572  return (0);
573 }

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

Referenced by SDL_VideoInit().

◆ SDL_KeyboardQuit()

void SDL_KeyboardQuit ( void  )

Definition at line 891 of file SDL_keyboard.c.

892 {
893 }

Referenced by SDL_VideoQuit().

◆ SDL_ReleaseAutoReleaseKeys()

void SDL_ReleaseAutoReleaseKeys ( void  )

Definition at line 818 of file SDL_keyboard.c.

819 {
820  SDL_Keyboard *keyboard = &SDL_keyboard;
821  SDL_Scancode scancode;
822 
823  if (keyboard->autorelease_pending) {
824  for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES; ++scancode) {
825  if (keyboard->keysource[scancode] == KEYBOARD_AUTORELEASE) {
827  }
828  }
829  keyboard->autorelease_pending = SDL_FALSE;
830  }
831 }
#define SDL_RELEASED
Definition: SDL_events.h:49
static int SDL_SendKeyboardKeyInternal(Uint8 source, Uint8 state, SDL_Scancode scancode)
Definition: SDL_keyboard.c:683
#define KEYBOARD_AUTORELEASE
Definition: SDL_keyboard.c:36
SDL_bool autorelease_pending
Definition: SDL_keyboard.c:48

References SDL_Keyboard::autorelease_pending, KEYBOARD_AUTORELEASE, SDL_Keyboard::keysource, SDL_FALSE, SDL_keyboard, SDL_NUM_SCANCODES, SDL_RELEASED, SDL_SCANCODE_UNKNOWN, and SDL_SendKeyboardKeyInternal().

Referenced by SDL_PumpEvents().

◆ SDL_ResetKeyboard()

void SDL_ResetKeyboard ( void  )

Definition at line 576 of file SDL_keyboard.c.

577 {
578  SDL_Keyboard *keyboard = &SDL_keyboard;
579  SDL_Scancode scancode;
580 
581 #ifdef DEBUG_KEYBOARD
582  printf("Resetting keyboard\n");
583 #endif
584  for (scancode = (SDL_Scancode) 0; scancode < SDL_NUM_SCANCODES; ++scancode) {
585  if (keyboard->keystate[scancode] == SDL_PRESSED) {
587  }
588  }
589 }
#define SDL_PRESSED
Definition: SDL_events.h:50
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
Definition: SDL_keyboard.c:806

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

Referenced by SDL_SetKeyboardFocus(), and SDL_ShowMessageBox().

◆ SDL_SendEditingText()

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

Definition at line 871 of file SDL_keyboard.c.

872 {
873  SDL_Keyboard *keyboard = &SDL_keyboard;
874  int posted;
875 
876  /* Post the event, if desired */
877  posted = 0;
880  event.edit.type = SDL_TEXTEDITING;
881  event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
882  event.edit.start = start;
883  event.edit.length = length;
884  SDL_utf8strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text));
885  posted = (SDL_PushEvent(&event) > 0);
886  }
887  return (posted);
888 }
#define SDL_PushEvent
#define SDL_utf8strlcpy
@ SDL_TEXTEDITING
Definition: SDL_events.h:100
#define SDL_GetEventState(type)
Definition: SDL_events.h:808
#define SDL_ENABLE
Definition: SDL_events.h:795
GLuint start
Definition: SDL_opengl.h:1571
struct _cl_event * event
GLuint GLsizei GLsizei * length
Uint32 id
Definition: SDL_sysvideo.h:77
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
General event structure.
Definition: SDL_events.h:592

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

Referenced by DBus_MessageFilter().

◆ SDL_SendKeyboardKey()

int SDL_SendKeyboardKey ( Uint8  state,
SDL_Scancode  scancode 
)

◆ SDL_SendKeyboardKeyAutoRelease()

int SDL_SendKeyboardKeyAutoRelease ( SDL_Scancode  scancode)

Definition at line 812 of file SDL_keyboard.c.

813 {
815 }

References KEYBOARD_AUTORELEASE, SDL_PRESSED, and SDL_SendKeyboardKeyInternal().

◆ SDL_SendKeyboardKeyInternal()

static int SDL_SendKeyboardKeyInternal ( Uint8  source,
Uint8  state,
SDL_Scancode  scancode 
)
static

Definition at line 683 of file SDL_keyboard.c.

684 {
685  SDL_Keyboard *keyboard = &SDL_keyboard;
686  int posted;
687  SDL_Keymod modifier;
688  SDL_Keycode keycode;
689  Uint32 type;
690  Uint8 repeat = SDL_FALSE;
691 
692  if (scancode == SDL_SCANCODE_UNKNOWN) {
693  return 0;
694  }
695 
696 #ifdef DEBUG_KEYBOARD
697  printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
698  state == SDL_PRESSED ? "pressed" : "released");
699 #endif
700 
701  /* Figure out what type of event this is */
702  switch (state) {
703  case SDL_PRESSED:
704  type = SDL_KEYDOWN;
705  break;
706  case SDL_RELEASED:
707  type = SDL_KEYUP;
708  break;
709  default:
710  /* Invalid state -- bail */
711  return 0;
712  }
713 
714  /* Drop events that don't change state */
715  if (state) {
716  if (keyboard->keystate[scancode]) {
717  if (!(keyboard->keysource[scancode] & source)) {
718  keyboard->keysource[scancode] |= source;
719  return 0;
720  }
721  repeat = SDL_TRUE;
722  }
723  keyboard->keysource[scancode] |= source;
724  } else {
725  if (!keyboard->keystate[scancode]) {
726  return 0;
727  }
728  keyboard->keysource[scancode] = 0;
729  }
730 
731  /* Update internal keyboard state */
732  keyboard->keystate[scancode] = state;
733 
734  keycode = keyboard->keymap[scancode];
735 
736  if (source == KEYBOARD_AUTORELEASE) {
737  keyboard->autorelease_pending = SDL_TRUE;
738  }
739 
740  /* Update modifiers state if applicable */
741  switch (keycode) {
742  case SDLK_LCTRL:
743  modifier = KMOD_LCTRL;
744  break;
745  case SDLK_RCTRL:
746  modifier = KMOD_RCTRL;
747  break;
748  case SDLK_LSHIFT:
749  modifier = KMOD_LSHIFT;
750  break;
751  case SDLK_RSHIFT:
752  modifier = KMOD_RSHIFT;
753  break;
754  case SDLK_LALT:
755  modifier = KMOD_LALT;
756  break;
757  case SDLK_RALT:
758  modifier = KMOD_RALT;
759  break;
760  case SDLK_LGUI:
761  modifier = KMOD_LGUI;
762  break;
763  case SDLK_RGUI:
764  modifier = KMOD_RGUI;
765  break;
766  case SDLK_MODE:
767  modifier = KMOD_MODE;
768  break;
769  default:
770  modifier = KMOD_NONE;
771  break;
772  }
773  if (SDL_KEYDOWN == type) {
774  switch (keycode) {
775  case SDLK_NUMLOCKCLEAR:
776  keyboard->modstate ^= KMOD_NUM;
777  break;
778  case SDLK_CAPSLOCK:
779  keyboard->modstate ^= KMOD_CAPS;
780  break;
781  default:
782  keyboard->modstate |= modifier;
783  break;
784  }
785  } else {
786  keyboard->modstate &= ~modifier;
787  }
788 
789  /* Post the event, if desired */
790  posted = 0;
793  event.key.type = type;
794  event.key.state = state;
795  event.key.repeat = repeat;
796  event.key.keysym.scancode = scancode;
797  event.key.keysym.sym = keycode;
798  event.key.keysym.mod = keyboard->modstate;
799  event.key.windowID = keyboard->focus ? keyboard->focus->id : 0;
800  posted = (SDL_PushEvent(&event) > 0);
801  }
802  return (posted);
803 }
@ SDL_KEYDOWN
Definition: SDL_events.h:98
@ SDL_KEYUP
Definition: SDL_events.h:99
@ SDLK_RSHIFT
Definition: SDL_keycode.h:283
@ SDLK_LALT
Definition: SDL_keycode.h:280
@ SDLK_LGUI
Definition: SDL_keycode.h:281
@ SDLK_MODE
Definition: SDL_keycode.h:287
@ SDLK_NUMLOCKCLEAR
Definition: SDL_keycode.h:158
@ SDLK_LCTRL
Definition: SDL_keycode.h:278
@ SDLK_LSHIFT
Definition: SDL_keycode.h:279
@ SDLK_CAPSLOCK
Definition: SDL_keycode.h:129
@ SDLK_RALT
Definition: SDL_keycode.h:284
@ SDLK_RCTRL
Definition: SDL_keycode.h:282
@ SDLK_RGUI
Definition: SDL_keycode.h:285
@ KMOD_MODE
Definition: SDL_keycode.h:340
@ KMOD_RALT
Definition: SDL_keycode.h:335
@ KMOD_LSHIFT
Definition: SDL_keycode.h:330
@ KMOD_LGUI
Definition: SDL_keycode.h:336
@ KMOD_CAPS
Definition: SDL_keycode.h:339
@ KMOD_LALT
Definition: SDL_keycode.h:334
@ KMOD_RCTRL
Definition: SDL_keycode.h:333
@ KMOD_NONE
Definition: SDL_keycode.h:329
@ KMOD_RGUI
Definition: SDL_keycode.h:337
@ KMOD_LCTRL
Definition: SDL_keycode.h:332
@ KMOD_RSHIFT
Definition: SDL_keycode.h:331
@ KMOD_NUM
Definition: SDL_keycode.h:338
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
GLsizei GLsizei GLchar * source

References SDL_Keyboard::autorelease_pending, SDL_Keyboard::focus, SDL_Window::id, KEYBOARD_AUTORELEASE, SDL_Keyboard::keymap, SDL_Keyboard::keysource, 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_FALSE, SDL_GetEventState, SDL_GetScancodeName(), SDL_keyboard, SDL_KEYDOWN, SDL_KEYUP, SDL_PRESSED, SDL_PushEvent, SDL_RELEASED, SDL_SCANCODE_UNKNOWN, SDL_TRUE, 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_ReleaseAutoReleaseKeys(), SDL_SendKeyboardKey(), and SDL_SendKeyboardKeyAutoRelease().

◆ SDL_SendKeyboardText()

int SDL_SendKeyboardText ( const char *  text)

Definition at line 848 of file SDL_keyboard.c.

849 {
850  SDL_Keyboard *keyboard = &SDL_keyboard;
851  int posted;
852 
853  /* Don't post text events for unprintable characters */
854  if ((unsigned char)*text < ' ' || *text == 127) {
855  return 0;
856  }
857 
858  /* Post the event, if desired */
859  posted = 0;
862  event.text.type = SDL_TEXTINPUT;
863  event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
864  SDL_utf8strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
865  posted = (SDL_PushEvent(&event) > 0);
866  }
867  return (posted);
868 }
@ SDL_TEXTINPUT
Definition: SDL_events.h:101

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

Referenced by SDL_BApp::_HandleKey(), and DBus_MessageFilter().

◆ SDL_SetKeyboardFocus()

void SDL_SetKeyboardFocus ( SDL_Window window)

Definition at line 634 of file SDL_keyboard.c.

635 {
636  SDL_Keyboard *keyboard = &SDL_keyboard;
637 
638  if (keyboard->focus && !window) {
639  /* We won't get anymore keyboard messages, so reset keyboard state */
641  }
642 
643  /* See if the current window has lost focus */
644  if (keyboard->focus && keyboard->focus != window) {
645 
646  /* new window shouldn't think it has mouse captured. */
648 
649  /* old window must lose an existing mouse capture. */
650  if (keyboard->focus->flags & SDL_WINDOW_MOUSE_CAPTURE) {
651  SDL_CaptureMouse(SDL_FALSE); /* drop the capture. */
653  }
654 
656  0, 0);
657 
658  /* Ensures IME compositions are committed */
661  if (video && video->StopTextInput) {
662  video->StopTextInput(video);
663  }
664  }
665  }
666 
667  keyboard->focus = window;
668 
669  if (keyboard->focus) {
671  0, 0);
672 
675  if (video && video->StartTextInput) {
676  video->StartTextInput(video);
677  }
678  }
679  }
680 }
#define SDL_assert(condition)
Definition: SDL_assert.h:171
#define SDL_CaptureMouse
#define SDL_EventState
#define SDL_QUERY
Definition: SDL_events.h:792
void SDL_ResetKeyboard(void)
Definition: SDL_keyboard.c:576
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:587
@ SDL_WINDOW_MOUSE_CAPTURE
Definition: SDL_video.h:115
@ SDL_WINDOWEVENT_FOCUS_LOST
Definition: SDL_video.h:166
@ SDL_WINDOWEVENT_FOCUS_GAINED
Definition: SDL_video.h:165
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
void(* StartTextInput)(_THIS)
Definition: SDL_sysvideo.h:297
void(* StopTextInput)(_THIS)
Definition: SDL_sysvideo.h:298
Uint32 flags
Definition: SDL_sysvideo.h:84

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, and SDL_VideoDevice::StopTextInput.

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

◆ SDL_SetKeymap()

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

Definition at line 598 of file SDL_keyboard.c.

599 {
600  SDL_Keyboard *keyboard = &SDL_keyboard;
601  SDL_Scancode scancode;
602 
604  return;
605  }
606 
607  SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length);
608 
609  /* The number key scancodes always map to the number key keycodes.
610  * On AZERTY layouts these technically are symbols, but users (and games)
611  * always think of them and view them in UI as number keys.
612  */
613  keyboard->keymap[SDL_SCANCODE_0] = SDLK_0;
614  for (scancode = SDL_SCANCODE_1; scancode <= SDL_SCANCODE_9; ++scancode) {
615  keyboard->keymap[scancode] = SDLK_1 + (scancode - SDL_SCANCODE_1);
616  }
617 }
@ SDLK_0
Definition: SDL_keycode.h:74
@ SDLK_1
Definition: SDL_keycode.h:75
@ SDL_SCANCODE_9
Definition: SDL_scancode.h:89
@ SDL_SCANCODE_0
Definition: SDL_scancode.h:90
@ SDL_SCANCODE_1
Definition: SDL_scancode.h:81

References SDL_Keyboard::keymap, SDL_keyboard, SDL_memcpy, SDL_NUM_SCANCODES, SDL_SCANCODE_0, SDL_SCANCODE_1, SDL_SCANCODE_9, SDLK_0, and SDLK_1.

◆ SDL_SetModState()

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 915 of file SDL_keyboard.c.

916 {
917  SDL_Keyboard *keyboard = &SDL_keyboard;
918 
919  keyboard->modstate = modstate;
920 }

References SDL_Keyboard::modstate, and SDL_keyboard.

◆ SDL_SetScancodeName()

void SDL_SetScancodeName ( SDL_Scancode  scancode,
const char *  name 
)

Definition at line 620 of file SDL_keyboard.c.

621 {
622  SDL_scancode_names[scancode] = name;
623 }

References SDL_scancode_names.

◆ SDL_ToggleModState()

void SDL_ToggleModState ( const SDL_Keymod  modstate,
const SDL_bool  toggle 
)

Definition at line 924 of file SDL_keyboard.c.

925 {
926  SDL_Keyboard *keyboard = &SDL_keyboard;
927  if (toggle) {
928  keyboard->modstate |= modstate;
929  } else {
930  keyboard->modstate &= ~modstate;
931  }
932 }

References SDL_Keyboard::modstate, and SDL_keyboard.

◆ SDL_UCS4ToUTF8()

char* SDL_UCS4ToUTF8 ( Uint32  ch,
char *  dst 
)

Definition at line 524 of file SDL_keyboard.c.

525 {
526  Uint8 *p = (Uint8 *) dst;
527  if (ch <= 0x7F) {
528  *p = (Uint8) ch;
529  ++dst;
530  } else if (ch <= 0x7FF) {
531  p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
532  p[1] = 0x80 | (Uint8) (ch & 0x3F);
533  dst += 2;
534  } else if (ch <= 0xFFFF) {
535  p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
536  p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
537  p[2] = 0x80 | (Uint8) (ch & 0x3F);
538  dst += 3;
539  } else if (ch <= 0x1FFFFF) {
540  p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
541  p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
542  p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
543  p[3] = 0x80 | (Uint8) (ch & 0x3F);
544  dst += 4;
545  } else if (ch <= 0x3FFFFFF) {
546  p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
547  p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
548  p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
549  p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
550  p[4] = 0x80 | (Uint8) (ch & 0x3F);
551  dst += 5;
552  } else {
553  p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
554  p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
555  p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
556  p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
557  p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
558  p[5] = 0x80 | (Uint8) (ch & 0x3F);
559  dst += 6;
560  }
561  return dst;
562 }
GLenum GLenum dst
GLfloat GLfloat p

Referenced by SDL_GetKeyName().

Variable Documentation

◆ SDL_default_keymap

const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES]
static

Definition at line 53 of file SDL_keyboard.c.

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

◆ SDL_keyboard

◆ SDL_scancode_names

const char* SDL_scancode_names[SDL_NUM_SCANCODES]
static