keyboard.c
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 2017 BlackBerry Limited
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../../events/SDL_keyboard_c.h"
#include "SDL_scancode.h"
#include "SDL_events.h"
#include "sdl_qnx.h"
#include <sys/keycodes.h>
/**
* A map thta translates Screen key names to SDL scan codes.
* This map is incomplete, but should include most major keys.
*/
static int key_to_sdl[] = {
[KEYCODE_SPACE] = SDL_SCANCODE_SPACE,
[KEYCODE_APOSTROPHE] = SDL_SCANCODE_APOSTROPHE,
[KEYCODE_COMMA] = SDL_SCANCODE_COMMA,
[KEYCODE_MINUS] = SDL_SCANCODE_MINUS,
[KEYCODE_PERIOD] = SDL_SCANCODE_PERIOD,
[KEYCODE_SLASH] = SDL_SCANCODE_SLASH,
[KEYCODE_ZERO] = SDL_SCANCODE_0,
[KEYCODE_ONE] = SDL_SCANCODE_1,
[KEYCODE_TWO] = SDL_SCANCODE_2,
[KEYCODE_THREE] = SDL_SCANCODE_3,
[KEYCODE_FOUR] = SDL_SCANCODE_4,
[KEYCODE_FIVE] = SDL_SCANCODE_5,
[KEYCODE_SIX] = SDL_SCANCODE_6,
[KEYCODE_SEVEN] = SDL_SCANCODE_7,
[KEYCODE_EIGHT] = SDL_SCANCODE_8,
[KEYCODE_NINE] = SDL_SCANCODE_9,
[KEYCODE_SEMICOLON] = SDL_SCANCODE_SEMICOLON,
[KEYCODE_EQUAL] = SDL_SCANCODE_EQUALS,
[KEYCODE_LEFT_BRACKET] = SDL_SCANCODE_LEFTBRACKET,
[KEYCODE_BACK_SLASH] = SDL_SCANCODE_BACKSLASH,
[KEYCODE_RIGHT_BRACKET] = SDL_SCANCODE_RIGHTBRACKET,
[KEYCODE_GRAVE] = SDL_SCANCODE_GRAVE,
[KEYCODE_A] = SDL_SCANCODE_A,
[KEYCODE_B] = SDL_SCANCODE_B,
[KEYCODE_C] = SDL_SCANCODE_C,
[KEYCODE_D] = SDL_SCANCODE_D,
[KEYCODE_E] = SDL_SCANCODE_E,
[KEYCODE_F] = SDL_SCANCODE_F,
[KEYCODE_G] = SDL_SCANCODE_G,
[KEYCODE_H] = SDL_SCANCODE_H,
[KEYCODE_I] = SDL_SCANCODE_I,
[KEYCODE_J] = SDL_SCANCODE_J,
[KEYCODE_K] = SDL_SCANCODE_K,
[KEYCODE_L] = SDL_SCANCODE_L,
[KEYCODE_M] = SDL_SCANCODE_M,
[KEYCODE_N] = SDL_SCANCODE_N,
[KEYCODE_O] = SDL_SCANCODE_O,
[KEYCODE_P] = SDL_SCANCODE_P,
[KEYCODE_Q] = SDL_SCANCODE_Q,
[KEYCODE_R] = SDL_SCANCODE_R,
[KEYCODE_S] = SDL_SCANCODE_S,
[KEYCODE_T] = SDL_SCANCODE_T,
[KEYCODE_U] = SDL_SCANCODE_U,
[KEYCODE_V] = SDL_SCANCODE_V,
[KEYCODE_W] = SDL_SCANCODE_W,
[KEYCODE_X] = SDL_SCANCODE_X,
[KEYCODE_Y] = SDL_SCANCODE_Y,
[KEYCODE_Z] = SDL_SCANCODE_Z,
[KEYCODE_UP] = SDL_SCANCODE_UP,
[KEYCODE_DOWN] = SDL_SCANCODE_DOWN,
[KEYCODE_LEFT] = SDL_SCANCODE_LEFT,
[KEYCODE_PG_UP] = SDL_SCANCODE_PAGEUP,
[KEYCODE_PG_DOWN] = SDL_SCANCODE_PAGEDOWN,
[KEYCODE_RIGHT] = SDL_SCANCODE_RIGHT,
[KEYCODE_RETURN] = SDL_SCANCODE_RETURN,
[KEYCODE_TAB] = SDL_SCANCODE_TAB,
[KEYCODE_ESCAPE] = SDL_SCANCODE_ESCAPE,
};
/**
* Called from the event dispatcher when a keyboard event is encountered.
* Translates the event such that it can be handled by SDL.
* @param event Screen keyboard event
*/
void
handleKeyboardEvent(screen_event_t event)
{
int val;
SDL_Scancode scancode;
// Get the key value.
if (screen_get_event_property_iv(event, SCREEN_PROPERTY_SYM, &val) < 0) {
return;
}
// Skip unrecognized keys.
if ((val < 0) || (val >= SDL_TABLESIZE(key_to_sdl))) {
return;
}
// Translate to an SDL scan code.
scancode = key_to_sdl[val];
if (scancode == 0) {
return;
}
// Get event flags (key state).
if (screen_get_event_property_iv(event, SCREEN_PROPERTY_FLAGS, &val) < 0) {
return;
}
// Propagate the event to SDL.
// FIXME:
// Need to handle more key states (such as key combinations).
if (val & KEY_DOWN) {
SDL_SendKeyboardKey(SDL_PRESSED, scancode);
} else {
SDL_SendKeyboardKey(SDL_RELEASED, scancode);
}
}
SDL_SCANCODE_3
@ SDL_SCANCODE_3
Definition: SDL_scancode.h:83
SDL_SCANCODE_BACKSLASH
@ SDL_SCANCODE_BACKSLASH
Definition: SDL_scancode.h:102
SDL_events.h
SDL_SCANCODE_6
@ SDL_SCANCODE_6
Definition: SDL_scancode.h:86
SDL_SCANCODE_LEFT
@ SDL_SCANCODE_LEFT
Definition: SDL_scancode.h:177
SDL_SCANCODE_PERIOD
@ SDL_SCANCODE_PERIOD
Definition: SDL_scancode.h:148
SDL_SCANCODE_5
@ SDL_SCANCODE_5
Definition: SDL_scancode.h:85
SDL_SCANCODE_K
@ SDL_SCANCODE_K
Definition: SDL_scancode.h:64
SDL_SCANCODE_SLASH
@ SDL_SCANCODE_SLASH
Definition: SDL_scancode.h:149
SDL_SCANCODE_4
@ SDL_SCANCODE_4
Definition: SDL_scancode.h:84
SDL_SCANCODE_RIGHT
@ SDL_SCANCODE_RIGHT
Definition: SDL_scancode.h:176
SDL_SCANCODE_U
@ SDL_SCANCODE_U
Definition: SDL_scancode.h:74
SDL_Scancode
SDL_Scancode
The SDL keyboard scancode representation.
Definition: SDL_scancode.h:43
SDL_SCANCODE_N
@ SDL_SCANCODE_N
Definition: SDL_scancode.h:67
SDL_SCANCODE_UP
@ SDL_SCANCODE_UP
Definition: SDL_scancode.h:179
SDL_SCANCODE_TAB
@ SDL_SCANCODE_TAB
Definition: SDL_scancode.h:95
SDL_SCANCODE_7
@ SDL_SCANCODE_7
Definition: SDL_scancode.h:87
SDL_SCANCODE_GRAVE
@ SDL_SCANCODE_GRAVE
Definition: SDL_scancode.h:130
SDL_SCANCODE_EQUALS
@ SDL_SCANCODE_EQUALS
Definition: SDL_scancode.h:99
handleKeyboardEvent
void handleKeyboardEvent(screen_event_t event)
Definition: keyboard.c:99
SDL_SCANCODE_PAGEDOWN
@ SDL_SCANCODE_PAGEDOWN
Definition: SDL_scancode.h:175
SDL_SCANCODE_SEMICOLON
@ SDL_SCANCODE_SEMICOLON
Definition: SDL_scancode.h:128
SDL_SCANCODE_W
@ SDL_SCANCODE_W
Definition: SDL_scancode.h:76
SDL_RELEASED
#define SDL_RELEASED
Definition: SDL_events.h:49
SDL_SendKeyboardKey
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
Definition: SDL_keyboard.c:679
SDL_SCANCODE_Z
@ SDL_SCANCODE_Z
Definition: SDL_scancode.h:79
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_SCANCODE_RIGHTBRACKET
@ SDL_SCANCODE_RIGHTBRACKET
Definition: SDL_scancode.h:101
SDL_SCANCODE_H
@ SDL_SCANCODE_H
Definition: SDL_scancode.h:61
SDL_SCANCODE_I
@ SDL_SCANCODE_I
Definition: SDL_scancode.h:62
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_SCANCODE_MINUS
@ SDL_SCANCODE_MINUS
Definition: SDL_scancode.h:98
SDL_SCANCODE_SPACE
@ SDL_SCANCODE_SPACE
Definition: SDL_scancode.h:96
SDL_SCANCODE_9
@ SDL_SCANCODE_9
Definition: SDL_scancode.h:89
SDL_SCANCODE_V
@ SDL_SCANCODE_V
Definition: SDL_scancode.h:75
SDL_SCANCODE_R
@ SDL_SCANCODE_R
Definition: SDL_scancode.h:71
SDL_SCANCODE_T
@ SDL_SCANCODE_T
Definition: SDL_scancode.h:73
SDL_SCANCODE_P
@ SDL_SCANCODE_P
Definition: SDL_scancode.h:69
SDL_SCANCODE_E
@ SDL_SCANCODE_E
Definition: SDL_scancode.h:58
SDL_SCANCODE_C
@ SDL_SCANCODE_C
Definition: SDL_scancode.h:56
SDL_SCANCODE_A
@ SDL_SCANCODE_A
Definition: SDL_scancode.h:54
key_to_sdl
static int key_to_sdl[]
Definition: keyboard.c:33
SDL_SCANCODE_0
@ SDL_SCANCODE_0
Definition: SDL_scancode.h:90
SDL_SCANCODE_ESCAPE
@ SDL_SCANCODE_ESCAPE
Definition: SDL_scancode.h:93
SDL_SCANCODE_S
@ SDL_SCANCODE_S
Definition: SDL_scancode.h:72
sdl_qnx.h
SDL_scancode.h
SDL_SCANCODE_Y
@ SDL_SCANCODE_Y
Definition: SDL_scancode.h:78
SDL_SCANCODE_LEFTBRACKET
@ SDL_SCANCODE_LEFTBRACKET
Definition: SDL_scancode.h:100
SDL_SCANCODE_F
@ SDL_SCANCODE_F
Definition: SDL_scancode.h:59
SDL_TABLESIZE
#define SDL_TABLESIZE(table)
Definition: SDL_stdinc.h:116
val
GLuint GLfloat * val
Definition: SDL_opengl_glext.h:1492
SDL_SCANCODE_COMMA
@ SDL_SCANCODE_COMMA
Definition: SDL_scancode.h:147
SDL_SCANCODE_X
@ SDL_SCANCODE_X
Definition: SDL_scancode.h:77
SDL_SCANCODE_B
@ SDL_SCANCODE_B
Definition: SDL_scancode.h:55
SDL_SCANCODE_2
@ SDL_SCANCODE_2
Definition: SDL_scancode.h:82
SDL_SCANCODE_M
@ SDL_SCANCODE_M
Definition: SDL_scancode.h:66
SDL_SCANCODE_PAGEUP
@ SDL_SCANCODE_PAGEUP
Definition: SDL_scancode.h:172
SDL_SCANCODE_APOSTROPHE
@ SDL_SCANCODE_APOSTROPHE
Definition: SDL_scancode.h:129
SDL_SCANCODE_Q
@ SDL_SCANCODE_Q
Definition: SDL_scancode.h:70
SDL_SCANCODE_G
@ SDL_SCANCODE_G
Definition: SDL_scancode.h:60
SDL_SCANCODE_1
@ SDL_SCANCODE_1
Definition: SDL_scancode.h:81
SDL_SCANCODE_DOWN
@ SDL_SCANCODE_DOWN
Definition: SDL_scancode.h:178
SDL_SCANCODE_J
@ SDL_SCANCODE_J
Definition: SDL_scancode.h:63
SDL_SCANCODE_D
@ SDL_SCANCODE_D
Definition: SDL_scancode.h:57
SDL_SCANCODE_L
@ SDL_SCANCODE_L
Definition: SDL_scancode.h:65
SDL_SCANCODE_8
@ SDL_SCANCODE_8
Definition: SDL_scancode.h:88
SDL_SCANCODE_RETURN
@ SDL_SCANCODE_RETURN
Definition: SDL_scancode.h:92
SDL_SCANCODE_O
@ SDL_SCANCODE_O
Definition: SDL_scancode.h:68