| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 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. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 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. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#ifdef __EMSCRIPTEN__ |
| 23 |
#include <emscripten/emscripten.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "SDL.h" |
| 27 |
#include "SDL_test_font.h" |
| 28 |
|
| 29 |
static SDL_Window *window; |
| 30 |
static SDL_Renderer *renderer; |
| 31 |
static SDLTest_TextWindow *textwin; |
| 32 |
static int done; |
| 33 |
|
| 34 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
| 35 |
static void |
| 36 |
quit(int rc) |
| 37 |
{ |
| 38 |
SDL_Quit(); |
| 39 |
exit(rc); |
| 40 |
} |
| 41 |
|
| 42 |
static void |
| 43 |
print_string(char **text, size_t *maxlen, const char *fmt, ...) |
| 44 |
{ |
| 45 |
int len; |
| 46 |
va_list ap; |
| 47 |
|
| 48 |
va_start(ap, fmt); |
| 49 |
len = SDL_vsnprintf(*text, *maxlen, fmt, ap); |
| 50 |
if (len > 0) { |
| 51 |
*text += len; |
| 52 |
if (((size_t)len) < *maxlen) { |
| 53 |
*maxlen -= (size_t)len; |
| 54 |
} else { |
| 55 |
*maxlen = 0; |
| 56 |
} |
| 57 |
} |
| 58 |
va_end(ap); |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
print_modifiers(char **text, size_t *maxlen) |
| 63 |
{ |
| 64 |
int mod; |
| 65 |
print_string(text, maxlen, " modifiers:"); |
| 66 |
mod = SDL_GetModState(); |
| 67 |
if (!mod) { |
| 68 |
print_string(text, maxlen, " (none)"); |
| 69 |
return; |
| 70 |
} |
| 71 |
if (mod & KMOD_LSHIFT) { |
| 72 |
print_string(text, maxlen, " LSHIFT"); |
| 73 |
} |
| 74 |
if (mod & KMOD_RSHIFT) { |
| 75 |
print_string(text, maxlen, " RSHIFT"); |
| 76 |
} |
| 77 |
if (mod & KMOD_LCTRL) { |
| 78 |
print_string(text, maxlen, " LCTRL"); |
| 79 |
} |
| 80 |
if (mod & KMOD_RCTRL) { |
| 81 |
print_string(text, maxlen, " RCTRL"); |
| 82 |
} |
| 83 |
if (mod & KMOD_LALT) { |
| 84 |
print_string(text, maxlen, " LALT"); |
| 85 |
} |
| 86 |
if (mod & KMOD_RALT) { |
| 87 |
print_string(text, maxlen, " RALT"); |
| 88 |
} |
| 89 |
if (mod & KMOD_LGUI) { |
| 90 |
print_string(text, maxlen, " LGUI"); |
| 91 |
} |
| 92 |
if (mod & KMOD_RGUI) { |
| 93 |
print_string(text, maxlen, " RGUI"); |
| 94 |
} |
| 95 |
if (mod & KMOD_NUM) { |
| 96 |
print_string(text, maxlen, " NUM"); |
| 97 |
} |
| 98 |
if (mod & KMOD_CAPS) { |
| 99 |
print_string(text, maxlen, " CAPS"); |
| 100 |
} |
| 101 |
if (mod & KMOD_MODE) { |
| 102 |
print_string(text, maxlen, " MODE"); |
| 103 |
} |
| 104 |
if (mod & KMOD_SCROLL) { |
| 105 |
print_string(text, maxlen, " SCROLL"); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
PrintModifierState(void) |
| 111 |
{ |
| 112 |
char message[512]; |
| 113 |
char *spot; |
| 114 |
size_t left; |
| 115 |
|
| 116 |
spot = message; |
| 117 |
left = sizeof(message); |
| 118 |
|
| 119 |
print_modifiers(&spot, &left); |
| 120 |
SDL_Log("Initial state:%s\n", message); |
| 121 |
} |
| 122 |
|
| 123 |
static void |
| 124 |
PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat) |
| 125 |
{ |
| 126 |
char message[512]; |
| 127 |
char *spot; |
| 128 |
size_t left; |
| 129 |
|
| 130 |
spot = message; |
| 131 |
left = sizeof(message); |
| 132 |
|
| 133 |
/* Print the keycode, name and state */ |
| 134 |
if (sym->sym) { |
| 135 |
print_string(&spot, &left, |
| 136 |
"Key %s: scancode %d = %s, keycode 0x%08X = %s ", |
| 137 |
pressed ? "pressed " : "released", |
| 138 |
sym->scancode, |
| 139 |
SDL_GetScancodeName(sym->scancode), |
| 140 |
sym->sym, SDL_GetKeyName(sym->sym)); |
| 141 |
} else { |
| 142 |
print_string(&spot, &left, |
| 143 |
"Unknown Key (scancode %d = %s) %s ", |
| 144 |
sym->scancode, |
| 145 |
SDL_GetScancodeName(sym->scancode), |
| 146 |
pressed ? "pressed " : "released"); |
| 147 |
} |
| 148 |
print_modifiers(&spot, &left); |
| 149 |
if (repeat) { |
| 150 |
print_string(&spot, &left, " (repeat)"); |
| 151 |
} |
| 152 |
SDL_Log("%s\n", message); |
| 153 |
} |
| 154 |
|
| 155 |
static void |
| 156 |
PrintText(const char *eventtype, const char *text) |
| 157 |
{ |
| 158 |
const char *spot; |
| 159 |
char expanded[1024]; |
| 160 |
|
| 161 |
expanded[0] = '\0'; |
| 162 |
for (spot = text; *spot; ++spot) { |
| 163 |
size_t length = SDL_strlen(expanded); |
| 164 |
(void)SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot); |
| 165 |
} |
| 166 |
SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text); |
| 167 |
} |
| 168 |
|
| 169 |
void loop(void) |
| 170 |
{ |
| 171 |
SDL_Event event; |
| 172 |
/* Check for events */ |
| 173 |
/*SDL_WaitEvent(&event); emscripten does not like waiting*/ |
| 174 |
|
| 175 |
while (SDL_PollEvent(&event)) { |
| 176 |
switch (event.type) { |
| 177 |
case SDL_KEYDOWN: |
| 178 |
case SDL_KEYUP: |
| 179 |
PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE); |
| 180 |
if (event.type == SDL_KEYDOWN) { |
| 181 |
switch (event.key.keysym.sym) { |
| 182 |
case SDLK_BACKSPACE: |
| 183 |
SDLTest_TextWindowAddText(textwin, "\b"); |
| 184 |
break; |
| 185 |
case SDLK_RETURN: |
| 186 |
SDLTest_TextWindowAddText(textwin, "\n"); |
| 187 |
break; |
| 188 |
default: |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
break; |
| 193 |
case SDL_TEXTEDITING: |
| 194 |
PrintText("EDIT", event.edit.text); |
| 195 |
break; |
| 196 |
case SDL_TEXTEDITING_EXT: |
| 197 |
PrintText("EDIT_EXT", event.editExt.text); |
| 198 |
SDL_free(event.editExt.text); |
| 199 |
break; |
| 200 |
case SDL_TEXTINPUT: |
| 201 |
PrintText("INPUT", event.text.text); |
| 202 |
SDLTest_TextWindowAddText(textwin, "%s", event.text.text); |
| 203 |
break; |
| 204 |
case SDL_FINGERDOWN: |
| 205 |
if (SDL_IsTextInputActive()) { |
| 206 |
SDL_Log("Stopping text input\n"); |
| 207 |
SDL_StopTextInput(); |
| 208 |
} else { |
| 209 |
SDL_Log("Starting text input\n"); |
| 210 |
SDL_StartTextInput(); |
| 211 |
} |
| 212 |
break; |
| 213 |
case SDL_MOUSEBUTTONDOWN: |
| 214 |
/* Left button quits the app, other buttons toggles text input */ |
| 215 |
if (event.button.button == SDL_BUTTON_LEFT) { |
| 216 |
done = 1; |
| 217 |
} else { |
| 218 |
if (SDL_IsTextInputActive()) { |
| 219 |
SDL_Log("Stopping text input\n"); |
| 220 |
SDL_StopTextInput(); |
| 221 |
} else { |
| 222 |
SDL_Log("Starting text input\n"); |
| 223 |
SDL_StartTextInput(); |
| 224 |
} |
| 225 |
} |
| 226 |
break; |
| 227 |
case SDL_QUIT: |
| 228 |
done = 1; |
| 229 |
break; |
| 230 |
default: |
| 231 |
break; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
| 236 |
SDL_RenderClear(renderer); |
| 237 |
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 238 |
SDLTest_TextWindowDisplay(textwin, renderer); |
| 239 |
SDL_RenderPresent(renderer); |
| 240 |
|
| 241 |
/* Slow down framerate */ |
| 242 |
SDL_Delay(100); |
| 243 |
|
| 244 |
#ifdef __EMSCRIPTEN__ |
| 245 |
if (done) { |
| 246 |
emscripten_cancel_main_loop(); |
| 247 |
} |
| 248 |
#endif |
| 249 |
} |
| 250 |
|
| 251 |
int main(int argc, char *argv[]) |
| 252 |
{ |
| 253 |
/* Enable standard application logging */ |
| 254 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 255 |
|
| 256 |
/* Disable mouse emulation */ |
| 257 |
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0"); |
| 258 |
|
| 259 |
/* Enable extended text editing events */ |
| 260 |
SDL_SetHint(SDL_HINT_IME_SUPPORT_EXTENDED_TEXT, "1"); |
| 261 |
|
| 262 |
/* Initialize SDL */ |
| 263 |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
| 264 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
/* Set 640x480 video mode */ |
| 269 |
window = SDL_CreateWindow("CheckKeys Test", |
| 270 |
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, |
| 271 |
640, 480, 0); |
| 272 |
if (!window) { |
| 273 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n", |
| 274 |
SDL_GetError()); |
| 275 |
quit(2); |
| 276 |
} |
| 277 |
|
| 278 |
renderer = SDL_CreateRenderer(window, -1, 0); |
| 279 |
if (!renderer) { |
| 280 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", |
| 281 |
SDL_GetError()); |
| 282 |
quit(2); |
| 283 |
} |
| 284 |
|
| 285 |
textwin = SDLTest_TextWindowCreate(0, 0, 640, 480); |
| 286 |
|
| 287 |
#ifdef __IPHONEOS__ |
| 288 |
/* Creating the context creates the view, which we need to show keyboard */ |
| 289 |
SDL_GL_CreateContext(window); |
| 290 |
#endif |
| 291 |
|
| 292 |
SDL_StartTextInput(); |
| 293 |
|
| 294 |
/* Print initial modifier state */ |
| 295 |
SDL_PumpEvents(); |
| 296 |
PrintModifierState(); |
| 297 |
|
| 298 |
/* Watch keystrokes */ |
| 299 |
done = 0; |
| 300 |
|
| 301 |
#ifdef __EMSCRIPTEN__ |
| 302 |
emscripten_set_main_loop(loop, 0, 1); |
| 303 |
#else |
| 304 |
while (!done) { |
| 305 |
loop(); |
| 306 |
} |
| 307 |
#endif |
| 308 |
|
| 309 |
SDL_Quit(); |
| 310 |
return 0; |
| 311 |
} |
| 312 |
|
| 313 |
/* vi: set ts=4 sw=4 expandtab: */ |