2 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Simple program: Loop, watching keystrokes
14 Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
15 pump the event loop and catch keystrokes.
23 #include <emscripten/emscripten.h>
30 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
39 print_string(char **text, size_t *maxlen, const char *fmt, ...)
45 len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
48 if ( ((size_t) len) < *maxlen ) {
49 *maxlen -= (size_t) len;
58 print_modifiers(char **text, size_t *maxlen)
61 print_string(text, maxlen, " modifiers:");
62 mod = SDL_GetModState();
64 print_string(text, maxlen, " (none)");
67 if (mod & KMOD_LSHIFT)
68 print_string(text, maxlen, " LSHIFT");
69 if (mod & KMOD_RSHIFT)
70 print_string(text, maxlen, " RSHIFT");
72 print_string(text, maxlen, " LCTRL");
74 print_string(text, maxlen, " RCTRL");
76 print_string(text, maxlen, " LALT");
78 print_string(text, maxlen, " RALT");
80 print_string(text, maxlen, " LGUI");
82 print_string(text, maxlen, " RGUI");
84 print_string(text, maxlen, " NUM");
86 print_string(text, maxlen, " CAPS");
88 print_string(text, maxlen, " MODE");
99 left = sizeof(message);
101 print_modifiers(&spot, &left);
102 SDL_Log("Initial state:%s\n", message);
106 PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
113 left = sizeof(message);
115 /* Print the keycode, name and state */
117 print_string(&spot, &left,
118 "Key %s: scancode %d = %s, keycode 0x%08X = %s ",
119 pressed ? "pressed " : "released",
121 SDL_GetScancodeName(sym->scancode),
122 sym->sym, SDL_GetKeyName(sym->sym));
124 print_string(&spot, &left,
125 "Unknown Key (scancode %d = %s) %s ",
127 SDL_GetScancodeName(sym->scancode),
128 pressed ? "pressed " : "released");
130 print_modifiers(&spot, &left);
132 print_string(&spot, &left, " (repeat)");
134 SDL_Log("%s\n", message);
138 PrintText(char *eventtype, char *text)
140 char *spot, expanded[1024];
143 for ( spot = text; *spot; ++spot )
145 size_t length = SDL_strlen(expanded);
146 SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
148 SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
155 /* Check for events */
156 /*SDL_WaitEvent(&event); emscripten does not like waiting*/
158 while (SDL_PollEvent(&event)) {
159 switch (event.type) {
162 PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
164 case SDL_TEXTEDITING:
165 PrintText("EDIT", event.text.text);
168 PrintText("INPUT", event.text.text);
170 case SDL_MOUSEBUTTONDOWN:
171 /* Any button press quits the app... */
179 #ifdef __EMSCRIPTEN__
181 emscripten_cancel_main_loop();
187 main(int argc, char *argv[])
191 /* Enable standard application logging */
192 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
195 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
196 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
200 /* Set 640x480 video mode */
201 window = SDL_CreateWindow("CheckKeys Test",
202 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
205 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
211 /* Creating the context creates the view, which we need to show keyboard */
212 SDL_GL_CreateContext(window);
215 SDL_StartTextInput();
217 /* Print initial modifier state */
219 PrintModifierState();
221 /* Watch keystrokes */
224 #ifdef __EMSCRIPTEN__
225 emscripten_set_main_loop(loop, 0, 1);
236 /* vi: set ts=4 sw=4 expandtab: */