SDL 2.0
checkkeys.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
+ Include dependency graph for checkkeys.c:

Go to the source code of this file.

Functions

static void quit (int rc)
 
static void print_string (char **text, size_t *maxlen, const char *fmt,...)
 
static void print_modifiers (char **text, size_t *maxlen)
 
static void PrintModifierState ()
 
static void PrintKey (SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
 
static void PrintText (char *eventtype, char *text)
 
void loop ()
 
int main (int argc, char *argv[])
 

Variables

int done
 

Function Documentation

◆ loop()

void loop ( void  )

Definition at line 152 of file checkkeys.c.

153{
155 /* Check for events */
156 /*SDL_WaitEvent(&event); emscripten does not like waiting*/
157
158 while (SDL_PollEvent(&event)) {
159 switch (event.type) {
160 case SDL_KEYDOWN:
161 case SDL_KEYUP:
162 PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
163 break;
164 case SDL_TEXTEDITING:
165 PrintText("EDIT", event.text.text);
166 break;
167 case SDL_TEXTINPUT:
168 PrintText("INPUT", event.text.text);
169 break;
171 /* Left button quits the app, other buttons toggles text input */
172 if (event.button.button == SDL_BUTTON_LEFT) {
173 done = 1;
174 } else {
175 if (SDL_IsTextInputActive()) {
176 SDL_Log("Stopping text input\n");
178 } else {
179 SDL_Log("Starting text input\n");
181 }
182 }
183 break;
184 case SDL_QUIT:
185 done = 1;
186 break;
187 default:
188 break;
189 }
190 }
191#ifdef __EMSCRIPTEN__
192 if (done) {
193 emscripten_cancel_main_loop();
194 }
195#endif
196}
#define SDL_PollEvent
#define SDL_StartTextInput
#define SDL_StopTextInput
#define SDL_IsTextInputActive
#define SDL_Log
@ SDL_TEXTEDITING
Definition: SDL_events.h:98
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_TEXTINPUT
Definition: SDL_events.h:99
@ SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:106
@ SDL_KEYDOWN
Definition: SDL_events.h:96
@ SDL_KEYUP
Definition: SDL_events.h:97
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_BUTTON_LEFT
Definition: SDL_mouse.h:282
struct _cl_event * event
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
static void PrintText(char *eventtype, char *text)
Definition: checkkeys.c:138
int done
Definition: checkkeys.c:28
static void PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
Definition: checkkeys.c:106
General event structure.
Definition: SDL_events.h:558

References done, PrintKey(), PrintText(), SDL_BUTTON_LEFT, SDL_FALSE, SDL_IsTextInputActive, SDL_KEYDOWN, SDL_KEYUP, SDL_Log, SDL_MOUSEBUTTONDOWN, SDL_PollEvent, SDL_PRESSED, SDL_QUIT, SDL_StartTextInput, SDL_StopTextInput, SDL_TEXTEDITING, SDL_TEXTINPUT, and SDL_TRUE.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 199 of file checkkeys.c.

200{
202
203 /* Enable standard application logging */
205
206 /* Initialize SDL */
207 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
208 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
209 return (1);
210 }
211
212 /* Set 640x480 video mode */
213 window = SDL_CreateWindow("CheckKeys Test",
215 640, 480, 0);
216 if (!window) {
217 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
218 SDL_GetError());
219 quit(2);
220 }
221
222#if __IPHONEOS__
223 /* Creating the context creates the view, which we need to show keyboard */
225#endif
226
228
229 /* Print initial modifier state */
232
233 /* Watch keystrokes */
234 done = 0;
235
236#ifdef __EMSCRIPTEN__
237 emscripten_set_main_loop(loop, 0, 1);
238#else
239 while (!done) {
240 loop();
241 }
242#endif
243
244 SDL_Quit();
245 return (0);
246}
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_PumpEvents
#define SDL_GetError
#define SDL_CreateWindow
#define SDL_LogSetPriority
#define SDL_LogError
#define SDL_Quit
#define SDL_Init
#define SDL_GL_CreateContext
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
static void quit(int rc)
Definition: checkkeys.c:32
static void PrintModifierState()
Definition: checkkeys.c:92
void loop()
Definition: checkkeys.c:152
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
The type used to identify a window.
Definition: SDL_sysvideo.h:74

References done, loop(), PrintModifierState(), quit(), SDL_CreateWindow, SDL_GetError, SDL_GL_CreateContext, SDL_Init, SDL_INIT_VIDEO, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_PumpEvents, SDL_Quit, SDL_StartTextInput, and SDL_WINDOWPOS_CENTERED.

◆ print_modifiers()

static void print_modifiers ( char **  text,
size_t maxlen 
)
static

Definition at line 58 of file checkkeys.c.

59{
60 int mod;
61 print_string(text, maxlen, " modifiers:");
62 mod = SDL_GetModState();
63 if (!mod) {
64 print_string(text, maxlen, " (none)");
65 return;
66 }
67 if (mod & KMOD_LSHIFT)
68 print_string(text, maxlen, " LSHIFT");
69 if (mod & KMOD_RSHIFT)
70 print_string(text, maxlen, " RSHIFT");
71 if (mod & KMOD_LCTRL)
72 print_string(text, maxlen, " LCTRL");
73 if (mod & KMOD_RCTRL)
74 print_string(text, maxlen, " RCTRL");
75 if (mod & KMOD_LALT)
76 print_string(text, maxlen, " LALT");
77 if (mod & KMOD_RALT)
78 print_string(text, maxlen, " RALT");
79 if (mod & KMOD_LGUI)
80 print_string(text, maxlen, " LGUI");
81 if (mod & KMOD_RGUI)
82 print_string(text, maxlen, " RGUI");
83 if (mod & KMOD_NUM)
84 print_string(text, maxlen, " NUM");
85 if (mod & KMOD_CAPS)
86 print_string(text, maxlen, " CAPS");
87 if (mod & KMOD_MODE)
88 print_string(text, maxlen, " MODE");
89}
#define SDL_GetModState
@ KMOD_MODE
Definition: SDL_keycode.h:338
@ KMOD_RALT
Definition: SDL_keycode.h:333
@ KMOD_LSHIFT
Definition: SDL_keycode.h:328
@ KMOD_LGUI
Definition: SDL_keycode.h:334
@ KMOD_CAPS
Definition: SDL_keycode.h:337
@ KMOD_LALT
Definition: SDL_keycode.h:332
@ KMOD_RCTRL
Definition: SDL_keycode.h:331
@ KMOD_RGUI
Definition: SDL_keycode.h:335
@ KMOD_LCTRL
Definition: SDL_keycode.h:330
@ KMOD_RSHIFT
Definition: SDL_keycode.h:329
@ KMOD_NUM
Definition: SDL_keycode.h:336
static void print_string(char **text, size_t *maxlen, const char *fmt,...)
Definition: checkkeys.c:39
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47

References KMOD_CAPS, KMOD_LALT, KMOD_LCTRL, KMOD_LGUI, KMOD_LSHIFT, KMOD_MODE, KMOD_NUM, KMOD_RALT, KMOD_RCTRL, KMOD_RGUI, KMOD_RSHIFT, print_string(), SDL_GetModState, and text.

Referenced by PrintKey(), and PrintModifierState().

◆ print_string()

static void print_string ( char **  text,
size_t maxlen,
const char *  fmt,
  ... 
)
static

Definition at line 39 of file checkkeys.c.

40{
41 int len;
42 va_list ap;
43
44 va_start(ap, fmt);
45 len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
46 if (len > 0) {
47 *text += len;
48 if ( ((size_t) len) < *maxlen ) {
49 *maxlen -= (size_t) len;
50 } else {
51 *maxlen = 0;
52 }
53 }
54 va_end(ap);
55}
unsigned int size_t
#define SDL_vsnprintf
GLenum GLsizei len

References SDL_vsnprintf, and text.

Referenced by print_modifiers(), and PrintKey().

◆ PrintKey()

static void PrintKey ( SDL_Keysym sym,
SDL_bool  pressed,
SDL_bool  repeat 
)
static

Definition at line 106 of file checkkeys.c.

107{
108 char message[512];
109 char *spot;
110 size_t left;
111
112 spot = message;
113 left = sizeof(message);
114
115 /* Print the keycode, name and state */
116 if (sym->sym) {
117 print_string(&spot, &left,
118 "Key %s: scancode %d = %s, keycode 0x%08X = %s ",
119 pressed ? "pressed " : "released",
120 sym->scancode,
122 sym->sym, SDL_GetKeyName(sym->sym));
123 } else {
124 print_string(&spot, &left,
125 "Unknown Key (scancode %d = %s) %s ",
126 sym->scancode,
128 pressed ? "pressed " : "released");
129 }
130 print_modifiers(&spot, &left);
131 if (repeat) {
132 print_string(&spot, &left, " (repeat)");
133 }
134 SDL_Log("%s\n", message);
135}
#define SDL_GetScancodeName
#define SDL_GetKeyName
GLint left
GLuint GLsizei const GLchar * message
static void print_modifiers(char **text, size_t *maxlen)
Definition: checkkeys.c:58
SDL_Keycode sym
Definition: SDL_keyboard.h:50
SDL_Scancode scancode
Definition: SDL_keyboard.h:49

References print_modifiers(), print_string(), SDL_Keysym::scancode, SDL_GetKeyName, SDL_GetScancodeName, SDL_Log, and SDL_Keysym::sym.

Referenced by loop().

◆ PrintModifierState()

static void PrintModifierState ( )
static

Definition at line 92 of file checkkeys.c.

93{
94 char message[512];
95 char *spot;
96 size_t left;
97
98 spot = message;
99 left = sizeof(message);
100
101 print_modifiers(&spot, &left);
102 SDL_Log("Initial state:%s\n", message);
103}

References print_modifiers(), and SDL_Log.

Referenced by main().

◆ PrintText()

static void PrintText ( char *  eventtype,
char *  text 
)
static

Definition at line 138 of file checkkeys.c.

139{
140 char *spot, expanded[1024];
141
142 expanded[0] = '\0';
143 for ( spot = text; *spot; ++spot )
144 {
145 size_t length = SDL_strlen(expanded);
146 SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
147 }
148 SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
149}
#define SDL_strlen
#define SDL_snprintf
GLuint GLsizei GLsizei * length

References SDL_Log, SDL_snprintf, SDL_strlen, and text.

Referenced by loop().

◆ quit()

static void quit ( int  rc)
static

Definition at line 32 of file checkkeys.c.

33{
34 SDL_Quit();
35 exit(rc);
36}

References SDL_Quit.

Referenced by main().

Variable Documentation

◆ done