checkkeys.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Simple program: Loop, watching keystrokes
  11. Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
  12. pump the event loop and catch keystrokes.
  13. */
  14. #ifdef __EMSCRIPTEN__
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. #include <SDL3/SDL.h>
  18. #include <SDL3/SDL_main.h>
  19. #include <SDL3/SDL_test.h>
  20. static SDLTest_CommonState *state;
  21. static SDLTest_TextWindow *textwin;
  22. static int done;
  23. static void
  24. print_string(char **text, size_t *maxlen, const char *fmt, ...)
  25. {
  26. int len;
  27. va_list ap;
  28. va_start(ap, fmt);
  29. len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
  30. if (len > 0) {
  31. *text += len;
  32. if (((size_t)len) < *maxlen) {
  33. *maxlen -= (size_t)len;
  34. } else {
  35. *maxlen = 0;
  36. }
  37. }
  38. va_end(ap);
  39. }
  40. static void
  41. print_modifiers(char **text, size_t *maxlen)
  42. {
  43. int mod;
  44. print_string(text, maxlen, " modifiers:");
  45. mod = SDL_GetModState();
  46. if (!mod) {
  47. print_string(text, maxlen, " (none)");
  48. return;
  49. }
  50. if (mod & SDL_KMOD_LSHIFT) {
  51. print_string(text, maxlen, " LSHIFT");
  52. }
  53. if (mod & SDL_KMOD_RSHIFT) {
  54. print_string(text, maxlen, " RSHIFT");
  55. }
  56. if (mod & SDL_KMOD_LCTRL) {
  57. print_string(text, maxlen, " LCTRL");
  58. }
  59. if (mod & SDL_KMOD_RCTRL) {
  60. print_string(text, maxlen, " RCTRL");
  61. }
  62. if (mod & SDL_KMOD_LALT) {
  63. print_string(text, maxlen, " LALT");
  64. }
  65. if (mod & SDL_KMOD_RALT) {
  66. print_string(text, maxlen, " RALT");
  67. }
  68. if (mod & SDL_KMOD_LGUI) {
  69. print_string(text, maxlen, " LGUI");
  70. }
  71. if (mod & SDL_KMOD_RGUI) {
  72. print_string(text, maxlen, " RGUI");
  73. }
  74. if (mod & SDL_KMOD_NUM) {
  75. print_string(text, maxlen, " NUM");
  76. }
  77. if (mod & SDL_KMOD_CAPS) {
  78. print_string(text, maxlen, " CAPS");
  79. }
  80. if (mod & SDL_KMOD_MODE) {
  81. print_string(text, maxlen, " MODE");
  82. }
  83. if (mod & SDL_KMOD_SCROLL) {
  84. print_string(text, maxlen, " SCROLL");
  85. }
  86. }
  87. static void
  88. PrintModifierState(void)
  89. {
  90. char message[512];
  91. char *spot;
  92. size_t left;
  93. spot = message;
  94. left = sizeof(message);
  95. print_modifiers(&spot, &left);
  96. SDL_Log("Initial state:%s\n", message);
  97. }
  98. static void
  99. PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
  100. {
  101. char message[512];
  102. char *spot;
  103. size_t left;
  104. spot = message;
  105. left = sizeof(message);
  106. /* Print the keycode, name and state */
  107. if (sym->sym) {
  108. print_string(&spot, &left,
  109. "Key %s: scancode %d = %s, keycode 0x%08X = %s ",
  110. pressed ? "pressed " : "released",
  111. sym->scancode,
  112. SDL_GetScancodeName(sym->scancode),
  113. sym->sym, SDL_GetKeyName(sym->sym));
  114. } else {
  115. print_string(&spot, &left,
  116. "Unknown Key (scancode %d = %s) %s ",
  117. sym->scancode,
  118. SDL_GetScancodeName(sym->scancode),
  119. pressed ? "pressed " : "released");
  120. }
  121. print_modifiers(&spot, &left);
  122. if (repeat) {
  123. print_string(&spot, &left, " (repeat)");
  124. }
  125. SDL_Log("%s\n", message);
  126. }
  127. static void
  128. PrintText(const char *eventtype, const char *text)
  129. {
  130. const char *spot;
  131. char expanded[1024];
  132. expanded[0] = '\0';
  133. for (spot = text; *spot; ++spot) {
  134. size_t length = SDL_strlen(expanded);
  135. (void)SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
  136. }
  137. SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
  138. }
  139. static void loop(void)
  140. {
  141. SDL_Event event;
  142. int i;
  143. /* Check for events */
  144. /*SDL_WaitEvent(&event); emscripten does not like waiting*/
  145. while (SDL_PollEvent(&event)) {
  146. switch (event.type) {
  147. case SDL_EVENT_KEY_DOWN:
  148. case SDL_EVENT_KEY_UP:
  149. PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
  150. if (event.type == SDL_EVENT_KEY_DOWN) {
  151. switch (event.key.keysym.sym) {
  152. case SDLK_BACKSPACE:
  153. SDLTest_TextWindowAddText(textwin, "\b");
  154. break;
  155. case SDLK_RETURN:
  156. SDLTest_TextWindowAddText(textwin, "\n");
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. break;
  163. case SDL_EVENT_TEXT_EDITING:
  164. PrintText("EDIT", event.edit.text);
  165. break;
  166. case SDL_EVENT_TEXT_EDITING_EXT:
  167. PrintText("EDIT_EXT", event.editExt.text);
  168. SDL_free(event.editExt.text);
  169. break;
  170. case SDL_EVENT_TEXT_INPUT:
  171. PrintText("INPUT", event.text.text);
  172. SDLTest_TextWindowAddText(textwin, "%s", event.text.text);
  173. break;
  174. case SDL_EVENT_FINGER_DOWN:
  175. if (SDL_TextInputActive()) {
  176. SDL_Log("Stopping text input\n");
  177. SDL_StopTextInput();
  178. } else {
  179. SDL_Log("Starting text input\n");
  180. SDL_StartTextInput();
  181. }
  182. break;
  183. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  184. /* Left button quits the app, other buttons toggles text input */
  185. if (event.button.button == SDL_BUTTON_LEFT) {
  186. done = 1;
  187. } else {
  188. if (SDL_TextInputActive()) {
  189. SDL_Log("Stopping text input\n");
  190. SDL_StopTextInput();
  191. } else {
  192. SDL_Log("Starting text input\n");
  193. SDL_StartTextInput();
  194. }
  195. }
  196. break;
  197. case SDL_EVENT_QUIT:
  198. done = 1;
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. for (i = 0; i < state->num_windows; i++) {
  205. SDL_SetRenderDrawColor(state->renderers[i], 0, 0, 0, 255);
  206. SDL_RenderClear(state->renderers[i]);
  207. SDL_SetRenderDrawColor(state->renderers[i], 255, 255, 255, 255);
  208. SDLTest_TextWindowDisplay(textwin, state->renderers[i]);
  209. SDL_RenderPresent(state->renderers[i]);
  210. }
  211. /* Slow down framerate */
  212. SDL_Delay(100);
  213. #ifdef __EMSCRIPTEN__
  214. if (done) {
  215. emscripten_cancel_main_loop();
  216. }
  217. #endif
  218. }
  219. int main(int argc, char *argv[])
  220. {
  221. int w, h;
  222. /* Initialize test framework */
  223. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  224. if (state == NULL) {
  225. return 1;
  226. }
  227. state->window_title = "CheckKeys Test";
  228. /* Enable standard application logging */
  229. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  230. /* Parse commandline */
  231. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  232. return 1;
  233. }
  234. /* Disable mouse emulation */
  235. SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
  236. /* Enable extended text editing events */
  237. SDL_SetHint(SDL_HINT_IME_SUPPORT_EXTENDED_TEXT, "1");
  238. /* Initialize SDL */
  239. if (!SDLTest_CommonInit(state)) {
  240. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  241. return 1;
  242. }
  243. SDL_GetWindowSize(state->windows[0], &w, &h);
  244. textwin = SDLTest_TextWindowCreate(0.f, 0.f, (float)w, (float)h);
  245. #ifdef __IOS__
  246. {
  247. int i;
  248. /* Creating the context creates the view, which we need to show keyboard */
  249. for (i = 0; i < state->num_windows; i++) {
  250. SDL_GL_CreateContext(state->windows[i]);
  251. }
  252. }
  253. #endif
  254. SDL_StartTextInput();
  255. /* Print initial modifier state */
  256. SDL_PumpEvents();
  257. PrintModifierState();
  258. /* Watch keystrokes */
  259. done = 0;
  260. #ifdef __EMSCRIPTEN__
  261. emscripten_set_main_loop(loop, 0, 1);
  262. #else
  263. while (!done) {
  264. loop();
  265. }
  266. #endif
  267. SDLTest_TextWindowDestroy(textwin);
  268. SDLTest_CleanupTextDrawing();
  269. SDLTest_CommonQuit(state);
  270. return 0;
  271. }