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 "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_WindowSDL_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 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]
 

Function Documentation

§ SDL_GetDefaultKeymap()

void SDL_GetDefaultKeymap ( SDL_Keycode keymap)

Definition at line 580 of file SDL_keyboard.c.

References SDL_default_keymap, and SDL_memcpy.

581 {
583 }
#define SDL_memcpy
static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:49

§ SDL_GetKeyboardFocus()

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.

605 {
606  SDL_Keyboard *keyboard = &SDL_keyboard;
607 
608  return keyboard->focus;
609 }
SDL_Window * focus
Definition: SDL_keyboard.c:41
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

§ 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");
}

Definition at line 822 of file SDL_keyboard.c.

References SDL_Keyboard::keystate, SDL_keyboard, and SDL_NUM_SCANCODES.

823 {
824  SDL_Keyboard *keyboard = &SDL_keyboard;
825 
826  if (numkeys != (int *) 0) {
827  *numkeys = SDL_NUM_SCANCODES;
828  }
829  return keyboard->keystate;
830 }
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
Uint8 keystate[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:43

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

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

968 {
969  SDL_Keycode key;
970 
971  /* Check input */
972  if (name == NULL) return SDLK_UNKNOWN;
973 
974  /* If it's a single UTF-8 character, then that's the keycode itself */
975  key = *(const unsigned char *)name;
976  if (key >= 0xF0) {
977  if (SDL_strlen(name) == 4) {
978  int i = 0;
979  key = (Uint16)(name[i]&0x07) << 18;
980  key |= (Uint16)(name[++i]&0x3F) << 12;
981  key |= (Uint16)(name[++i]&0x3F) << 6;
982  key |= (Uint16)(name[++i]&0x3F);
983  return key;
984  }
985  return SDLK_UNKNOWN;
986  } else if (key >= 0xE0) {
987  if (SDL_strlen(name) == 3) {
988  int i = 0;
989  key = (Uint16)(name[i]&0x0F) << 12;
990  key |= (Uint16)(name[++i]&0x3F) << 6;
991  key |= (Uint16)(name[++i]&0x3F);
992  return key;
993  }
994  return SDLK_UNKNOWN;
995  } else if (key >= 0xC0) {
996  if (SDL_strlen(name) == 2) {
997  int i = 0;
998  key = (Uint16)(name[i]&0x1F) << 6;
999  key |= (Uint16)(name[++i]&0x3F);
1000  return key;
1001  }
1002  return SDLK_UNKNOWN;
1003  } else {
1004  if (SDL_strlen(name) == 1) {
1005  if (key >= 'A' && key <= 'Z') {
1006  key += 32;
1007  }
1008  return key;
1009  }
1010 
1011  /* Get the scancode for this name, and the associated keycode */
1013  }
1014 }
GLuint const GLchar * name
Sint32 SDL_Keycode
The SDL virtual key representation.
Definition: SDL_keycode.h:42
SDL_Scancode SDL_GetScancodeFromName(const char *name)
Get a scancode from a human-readable name.
Definition: SDL_keyboard.c:905
static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:49
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
#define NULL
Definition: begin_code.h:143
#define SDL_strlen
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:151

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

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

863 {
864  SDL_Keyboard *keyboard = &SDL_keyboard;
865 
866  if (scancode < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
867  SDL_InvalidParamError("scancode");
868  return 0;
869  }
870 
871  return keyboard->keymap[scancode];
872 }
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

§ 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 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.

929 {
930  static char name[8];
931  char *end;
932 
933  if (key & SDLK_SCANCODE_MASK) {
934  return
935  SDL_GetScancodeName((SDL_Scancode) (key & ~SDLK_SCANCODE_MASK));
936  }
937 
938  switch (key) {
939  case SDLK_RETURN:
941  case SDLK_ESCAPE:
943  case SDLK_BACKSPACE:
945  case SDLK_TAB:
947  case SDLK_SPACE:
949  case SDLK_DELETE:
951  default:
952  /* Unaccented letter keys on latin keyboards are normally
953  labeled in upper case (and probably on others like Greek or
954  Cyrillic too, so if you happen to know for sure, please
955  adapt this). */
956  if (key >= 'a' && key <= 'z') {
957  key -= 32;
958  }
959 
960  end = SDL_UCS4ToUTF8((Uint32) key, name);
961  *end = '\0';
962  return name;
963  }
964 }
GLuint GLuint end
Definition: SDL_opengl.h:1564
#define SDLK_SCANCODE_MASK
Definition: SDL_keycode.h:44
GLuint const GLchar * name
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:159
const char * SDL_GetScancodeName(SDL_Scancode scancode)
Get a human-readable name for a scancode.
Definition: SDL_keyboard.c:890
char * SDL_UCS4ToUTF8(Uint32 ch, char *dst)
Definition: SDL_keyboard.c:512
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:43

§ SDL_GetModState()

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.

834 {
835  SDL_Keyboard *keyboard = &SDL_keyboard;
836 
837  return keyboard->modstate;
838 }
Uint16 modstate
Definition: SDL_keyboard.c:42
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

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

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

876 {
877  SDL_Keyboard *keyboard = &SDL_keyboard;
878  SDL_Scancode scancode;
879 
880  for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
881  ++scancode) {
882  if (keyboard->keymap[scancode] == key) {
883  return scancode;
884  }
885  }
886  return SDL_SCANCODE_UNKNOWN;
887 }
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:43

§ 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 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().

906 {
907  int i;
908 
909  if (!name || !*name) {
910  SDL_InvalidParamError("name");
911  return SDL_SCANCODE_UNKNOWN;
912  }
913 
914  for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
915  if (!SDL_scancode_names[i]) {
916  continue;
917  }
918  if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
919  return (SDL_Scancode)i;
920  }
921  }
922 
923  SDL_InvalidParamError("name");
924  return SDL_SCANCODE_UNKNOWN;
925 }
GLuint const GLchar * name
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
#define SDL_strcasecmp
static const char * SDL_scancode_names[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:278
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
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:90
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:43

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

References SDL_InvalidParamError, SDL_NUM_SCANCODES, and SDL_scancode_names.

Referenced by SDL_GetKeyName(), and SDL_SendKeyboardKey().

891 {
892  const char *name;
893  if (scancode < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
894  SDL_InvalidParamError("scancode");
895  return "";
896  }
897 
898  name = SDL_scancode_names[scancode];
899  if (name)
900  return name;
901  else
902  return "";
903 }
GLuint const GLchar * name
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
static const char * SDL_scancode_names[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:278

§ SDL_KeyboardInit()

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().

555 {
556  SDL_Keyboard *keyboard = &SDL_keyboard;
557 
558  /* Set the default keymap */
560  return (0);
561 }
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
#define SDL_memcpy
static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:49
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

§ SDL_KeyboardQuit()

void SDL_KeyboardQuit ( void  )

Definition at line 817 of file SDL_keyboard.c.

Referenced by SDL_VideoQuit().

818 {
819 }

§ SDL_ResetKeyboard()

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().

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

§ SDL_SendEditingText()

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.

798 {
799  SDL_Keyboard *keyboard = &SDL_keyboard;
800  int posted;
801 
802  /* Post the event, if desired */
803  posted = 0;
806  event.edit.type = SDL_TEXTEDITING;
807  event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
808  event.edit.start = start;
809  event.edit.length = length;
811  posted = (SDL_PushEvent(&event) > 0);
812  }
813  return (posted);
814 }
#define SDL_utf8strlcpy
#define SDL_ENABLE
Definition: SDL_events.h:722
GLuint start
Definition: SDL_opengl.h:1564
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: SDL_events.h:211
SDL_TextEditingEvent edit
Definition: SDL_events.h:531
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
SDL_Window * focus
Definition: SDL_keyboard.c:41
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
Uint32 id
Definition: SDL_sysvideo.h:74
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:90
General event structure.
Definition: SDL_events.h:525
GLuint GLsizei GLsizei * length

§ SDL_SendKeyboardKey()

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().

662 {
663  SDL_Keyboard *keyboard = &SDL_keyboard;
664  int posted;
665  SDL_Keymod modifier;
666  SDL_Keycode keycode;
667  Uint16 modstate;
668  Uint32 type;
669  Uint8 repeat;
670 
671  if (!scancode) {
672  return 0;
673  }
674 #ifdef DEBUG_KEYBOARD
675  printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
676  state == SDL_PRESSED ? "pressed" : "released");
677 #endif
678 
679  /* Figure out what type of event this is */
680  switch (state) {
681  case SDL_PRESSED:
682  type = SDL_KEYDOWN;
683  break;
684  case SDL_RELEASED:
685  type = SDL_KEYUP;
686  break;
687  default:
688  /* Invalid state -- bail */
689  return 0;
690  }
691 
692  /* Drop events that don't change state */
693  repeat = (state && keyboard->keystate[scancode]);
694  if (keyboard->keystate[scancode] == state && !repeat) {
695 #if 0
696  printf("Keyboard event didn't change state - dropped!\n");
697 #endif
698  return 0;
699  }
700 
701  /* Update internal keyboard state */
702  keyboard->keystate[scancode] = state;
703 
704  keycode = keyboard->keymap[scancode];
705 
706  /* Update modifiers state if applicable */
707  switch (keycode) {
708  case SDLK_LCTRL:
709  modifier = KMOD_LCTRL;
710  break;
711  case SDLK_RCTRL:
712  modifier = KMOD_RCTRL;
713  break;
714  case SDLK_LSHIFT:
715  modifier = KMOD_LSHIFT;
716  break;
717  case SDLK_RSHIFT:
718  modifier = KMOD_RSHIFT;
719  break;
720  case SDLK_LALT:
721  modifier = KMOD_LALT;
722  break;
723  case SDLK_RALT:
724  modifier = KMOD_RALT;
725  break;
726  case SDLK_LGUI:
727  modifier = KMOD_LGUI;
728  break;
729  case SDLK_RGUI:
730  modifier = KMOD_RGUI;
731  break;
732  case SDLK_MODE:
733  modifier = KMOD_MODE;
734  break;
735  default:
736  modifier = KMOD_NONE;
737  break;
738  }
739  if (SDL_KEYDOWN == type) {
740  modstate = keyboard->modstate;
741  switch (keycode) {
742  case SDLK_NUMLOCKCLEAR:
743  keyboard->modstate ^= KMOD_NUM;
744  break;
745  case SDLK_CAPSLOCK:
746  keyboard->modstate ^= KMOD_CAPS;
747  break;
748  default:
749  keyboard->modstate |= modifier;
750  break;
751  }
752  } else {
753  keyboard->modstate &= ~modifier;
754  modstate = keyboard->modstate;
755  }
756 
757  /* Post the event, if desired */
758  posted = 0;
759  if (SDL_GetEventState(type) == SDL_ENABLE) {
761  event.key.type = type;
762  event.key.state = state;
763  event.key.repeat = repeat;
764  event.key.keysym.scancode = scancode;
765  event.key.keysym.sym = keycode;
766  event.key.keysym.mod = modstate;
767  event.key.windowID = keyboard->focus ? keyboard->focus->id : 0;
768  posted = (SDL_PushEvent(&event) > 0);
769  }
770  return (posted);
771 }
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:722
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:159
Sint32 SDL_Keycode
The SDL virtual key representation.
Definition: SDL_keycode.h:42
Uint16 modstate
Definition: SDL_keyboard.c:42
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1564
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
struct _cl_event * event
#define SDL_PushEvent
const char * SDL_GetScancodeName(SDL_Scancode scancode)
Get a human-readable name for a scancode.
Definition: SDL_keyboard.c:890
SDL_Keymod
Enumeration of valid key mods (possibly OR&#39;d together).
Definition: SDL_keycode.h:317
SDL_Window * focus
Definition: SDL_keyboard.c:41
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
Uint32 id
Definition: SDL_sysvideo.h:74
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:151
General event structure.
Definition: SDL_events.h:525
#define SDL_PRESSED
Definition: SDL_events.h:50
Uint8 keystate[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:43
#define SDL_RELEASED
Definition: SDL_events.h:49

§ SDL_SendKeyboardText()

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.

Referenced by SDL_BApp::_HandleKey().

775 {
776  SDL_Keyboard *keyboard = &SDL_keyboard;
777  int posted;
778 
779  /* Don't post text events for unprintable characters */
780  if ((unsigned char)*text < ' ' || *text == 127) {
781  return 0;
782  }
783 
784  /* Post the event, if desired */
785  posted = 0;
788  event.text.type = SDL_TEXTINPUT;
789  event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
791  posted = (SDL_PushEvent(&event) > 0);
792  }
793  return (posted);
794 }
#define SDL_utf8strlcpy
#define SDL_ENABLE
Definition: SDL_events.h:722
SDL_TextInputEvent text
Definition: SDL_events.h:532
char text[SDL_TEXTINPUTEVENT_TEXT_SIZE]
Definition: SDL_events.h:226
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
SDL_Window * focus
Definition: SDL_keyboard.c:41
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
Uint32 id
Definition: SDL_sysvideo.h:74
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:90
General event structure.
Definition: SDL_events.h:525

§ SDL_SetKeyboardFocus()

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().

613 {
614  SDL_Keyboard *keyboard = &SDL_keyboard;
615 
616  if (keyboard->focus && !window) {
617  /* We won't get anymore keyboard messages, so reset keyboard state */
619  }
620 
621  /* See if the current window has lost focus */
622  if (keyboard->focus && keyboard->focus != window) {
623 
624  /* new window shouldn't think it has mouse captured. */
625  SDL_assert(!window || !(window->flags & SDL_WINDOW_MOUSE_CAPTURE));
626 
627  /* old window must lose an existing mouse capture. */
628  if (keyboard->focus->flags & SDL_WINDOW_MOUSE_CAPTURE) {
629  SDL_CaptureMouse(SDL_FALSE); /* drop the capture. */
631  }
632 
634  0, 0);
635 
636  /* Ensures IME compositions are committed */
639  if (video && video->StopTextInput) {
640  video->StopTextInput(video);
641  }
642  }
643  }
644 
645  keyboard->focus = window;
646 
647  if (keyboard->focus) {
649  0, 0);
650 
653  if (video && video->StartTextInput) {
654  video->StartTextInput(video);
655  }
656  }
657  }
658 }
static SDL_Window * window
void(* StartTextInput)(_THIS)
Definition: SDL_sysvideo.h:268
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
void(* StopTextInput)(_THIS)
Definition: SDL_sysvideo.h:269
#define SDL_assert(condition)
Definition: SDL_assert.h:167
SDL_Window * focus
Definition: SDL_keyboard.c:41
#define SDL_EventState
void SDL_ResetKeyboard(void)
Definition: SDL_keyboard.c:564
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
#define SDL_CaptureMouse
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:571
#define SDL_QUERY
Definition: SDL_events.h:719
Uint32 flags
Definition: SDL_sysvideo.h:81

§ SDL_SetKeymap()

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.

587 {
588  SDL_Keyboard *keyboard = &SDL_keyboard;
589 
591  return;
592  }
593 
594  SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length);
595 }
GLuint start
Definition: SDL_opengl.h:1564
SDL_Keycode keymap[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:44
#define SDL_memcpy
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47
GLuint GLsizei GLsizei * length

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

References SDL_Keyboard::modstate, and SDL_keyboard.

842 {
843  SDL_Keyboard *keyboard = &SDL_keyboard;
844 
845  keyboard->modstate = modstate;
846 }
Uint16 modstate
Definition: SDL_keyboard.c:42
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

§ SDL_SetScancodeName()

void SDL_SetScancodeName ( SDL_Scancode  scancode,
const char *  name 
)

Definition at line 598 of file SDL_keyboard.c.

References SDL_scancode_names.

599 {
600  SDL_scancode_names[scancode] = name;
601 }
GLuint const GLchar * name
static const char * SDL_scancode_names[SDL_NUM_SCANCODES]
Definition: SDL_keyboard.c:278

§ SDL_ToggleModState()

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.

851 {
852  SDL_Keyboard *keyboard = &SDL_keyboard;
853  if (toggle) {
854  keyboard->modstate |= modstate;
855  } else {
856  keyboard->modstate &= ~modstate;
857  }
858 }
Uint16 modstate
Definition: SDL_keyboard.c:42
static SDL_Keyboard SDL_keyboard
Definition: SDL_keyboard.c:47

§ SDL_UCS4ToUTF8()

char* SDL_UCS4ToUTF8 ( Uint32  ch,
char *  dst 
)

Definition at line 512 of file SDL_keyboard.c.

Referenced by SDL_GetKeyName().

513 {
514  Uint8 *p = (Uint8 *) dst;
515  if (ch <= 0x7F) {
516  *p = (Uint8) ch;
517  ++dst;
518  } else if (ch <= 0x7FF) {
519  p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
520  p[1] = 0x80 | (Uint8) (ch & 0x3F);
521  dst += 2;
522  } else if (ch <= 0xFFFF) {
523  p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
524  p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
525  p[2] = 0x80 | (Uint8) (ch & 0x3F);
526  dst += 3;
527  } else if (ch <= 0x1FFFFF) {
528  p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
529  p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
530  p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
531  p[3] = 0x80 | (Uint8) (ch & 0x3F);
532  dst += 4;
533  } else if (ch <= 0x3FFFFFF) {
534  p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
535  p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
536  p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
537  p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
538  p[4] = 0x80 | (Uint8) (ch & 0x3F);
539  dst += 5;
540  } else {
541  p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
542  p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
543  p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
544  p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
545  p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
546  p[5] = 0x80 | (Uint8) (ch & 0x3F);
547  dst += 6;
548  }
549  return dst;
550 }
GLenum GLenum dst
GLfloat GLfloat p
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143

Variable Documentation

§ SDL_default_keymap

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_scancode_names

const char* SDL_scancode_names[SDL_NUM_SCANCODES]
static