SDL  2.0
SDL_keyboard.h File Reference
#include "SDL_stdinc.h"
#include "SDL_error.h"
#include "SDL_keycode.h"
#include "SDL_video.h"
#include "begin_code.h"
#include "close_code.h"
+ Include dependency graph for SDL_keyboard.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_Keysym
 The SDL keysym structure, used in key events. More...
 

Functions

SDL_WindowSDL_GetKeyboardFocus (void)
 Get the window which currently has keyboard focus. More...
 
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...
 
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...
 
void SDL_StartTextInput (void)
 Start accepting Unicode text input events. This function will show the on-screen keyboard if supported. More...
 
SDL_bool SDL_IsTextInputActive (void)
 Return whether or not Unicode text input events are enabled. More...
 
void SDL_StopTextInput (void)
 Stop receiving any text input events. This function will hide the on-screen keyboard if supported. More...
 
void SDL_SetTextInputRect (SDL_Rect *rect)
 Set the rectangle used to type Unicode text inputs. This is used as a hint for IME and on-screen keyboard placement. More...
 
SDL_bool SDL_HasScreenKeyboardSupport (void)
 Returns whether the platform has some screen keyboard support. More...
 
SDL_bool SDL_IsScreenKeyboardShown (SDL_Window *window)
 Returns whether the screen keyboard is shown for given window. More...
 

Detailed Description

Include file for SDL keyboard event handling

Definition in file SDL_keyboard.h.

Function Documentation

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

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

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_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:42
#define NULL
Definition: begin_code.h:143
#define SDL_strlen
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.

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

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 }
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
GLuint GLuint end
Definition: SDL_opengl.h:1564
#define SDLK_SCANCODE_MASK
Definition: SDL_keycode.h:44
GLuint const GLchar * name
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_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_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_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:42
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
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().

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_bool SDL_HasScreenKeyboardSupport ( void  )

Returns whether the platform has some screen keyboard support.

Returns
SDL_TRUE if some keyboard support is available else SDL_FALSE.
Note
Not all screen keyboard functions are supported on all platforms.
See also
SDL_IsScreenKeyboardShown()

Definition at line 3453 of file SDL_video.c.

References SDL_VideoDevice::HasScreenKeyboardSupport, and SDL_FALSE.

Referenced by SDL_VideoInit().

3454 {
3457  }
3458  return SDL_FALSE;
3459 }
static SDL_VideoDevice * _this
Definition: SDL_video.c:114
SDL_bool(* HasScreenKeyboardSupport)(_THIS)
Definition: SDL_sysvideo.h:260
SDL_bool SDL_IsScreenKeyboardShown ( SDL_Window window)

Returns whether the screen keyboard is shown for given window.

Parameters
windowThe window for which screen keyboard should be queried.
Returns
SDL_TRUE if screen keyboard is shown else SDL_FALSE.
See also
SDL_HasScreenKeyboardSupport()

Definition at line 3462 of file SDL_video.c.

References SDL_VideoDevice::IsScreenKeyboardShown, and SDL_FALSE.

3463 {
3464  if (window && _this && _this->IsScreenKeyboardShown) {
3465  return _this->IsScreenKeyboardShown(_this, window);
3466  }
3467  return SDL_FALSE;
3468 }
SDL_bool(* IsScreenKeyboardShown)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:263
static SDL_VideoDevice * _this
Definition: SDL_video.c:114
SDL_bool SDL_IsTextInputActive ( void  )

Return whether or not Unicode text input events are enabled.

See also
SDL_StartTextInput()
SDL_StopTextInput()

Definition at line 3418 of file SDL_video.c.

References SDL_ENABLE, SDL_GetEventState, and SDL_TEXTINPUT.

3419 {
3421 }
#define SDL_ENABLE
Definition: SDL_events.h:718
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
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
void SDL_SetTextInputRect ( SDL_Rect rect)

Set the rectangle used to type Unicode text inputs. This is used as a hint for IME and on-screen keyboard placement.

See also
SDL_StartTextInput()

Definition at line 3445 of file SDL_video.c.

References SDL_VideoDevice::SetTextInputRect.

3446 {
3447  if (_this && _this->SetTextInputRect) {
3448  _this->SetTextInputRect(_this, rect);
3449  }
3450 }
static SDL_VideoDevice * _this
Definition: SDL_video.c:114
void(* SetTextInputRect)(_THIS, SDL_Rect *rect)
Definition: SDL_sysvideo.h:257
void SDL_StartTextInput ( void  )

Start accepting Unicode text input events. This function will show the on-screen keyboard if supported.

See also
SDL_StopTextInput()
SDL_SetTextInputRect()
SDL_HasScreenKeyboardSupport()

Definition at line 3397 of file SDL_video.c.

References SDL_ENABLE, SDL_EventState, SDL_GetFocusWindow(), SDL_TEXTEDITING, SDL_TEXTINPUT, SDL_VideoDevice::ShowScreenKeyboard, SDL_VideoDevice::StartTextInput, and window.

Referenced by SDL_VideoInit().

3398 {
3399  SDL_Window *window;
3400 
3401  /* First, enable text events */
3404 
3405  /* Then show the on-screen keyboard, if any */
3406  window = SDL_GetFocusWindow();
3407  if (window && _this && _this->ShowScreenKeyboard) {
3408  _this->ShowScreenKeyboard(_this, window);
3409  }
3410 
3411  /* Finally start the text input system */
3412  if (_this && _this->StartTextInput) {
3414  }
3415 }
SDL_Window * SDL_GetFocusWindow(void)
Definition: SDL_video.c:2403
SDL_Window * window
#define SDL_ENABLE
Definition: SDL_events.h:718
void(* StartTextInput)(_THIS)
Definition: SDL_sysvideo.h:255
static SDL_VideoDevice * _this
Definition: SDL_video.c:114
void(* ShowScreenKeyboard)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:261
The type used to identify a window.
Definition: SDL_sysvideo.h:71
#define SDL_EventState
void SDL_StopTextInput ( void  )

Stop receiving any text input events. This function will hide the on-screen keyboard if supported.

See also
SDL_StartTextInput()
SDL_HasScreenKeyboardSupport()

Definition at line 3424 of file SDL_video.c.

References SDL_VideoDevice::HideScreenKeyboard, SDL_DISABLE, SDL_EventState, SDL_GetFocusWindow(), SDL_TEXTEDITING, SDL_TEXTINPUT, SDL_VideoDevice::StopTextInput, and window.

3425 {
3426  SDL_Window *window;
3427 
3428  /* Stop the text input system */
3429  if (_this && _this->StopTextInput) {
3431  }
3432 
3433  /* Hide the on-screen keyboard, if any */
3434  window = SDL_GetFocusWindow();
3435  if (window && _this && _this->HideScreenKeyboard) {
3436  _this->HideScreenKeyboard(_this, window);
3437  }
3438 
3439  /* Finally disable text events */
3442 }
SDL_Window * SDL_GetFocusWindow(void)
Definition: SDL_video.c:2403
SDL_Window * window
static SDL_VideoDevice * _this
Definition: SDL_video.c:114
void(* StopTextInput)(_THIS)
Definition: SDL_sysvideo.h:256
#define SDL_DISABLE
Definition: SDL_events.h:717
The type used to identify a window.
Definition: SDL_sysvideo.h:71
#define SDL_EventState
void(* HideScreenKeyboard)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:262