checkkeys.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. Copyright (C) 1997-2024 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. #include <SDL3/SDL.h>
  15. #include <SDL3/SDL_main.h>
  16. #include <SDL3/SDL_test.h>
  17. #ifdef SDL_PLATFORM_EMSCRIPTEN
  18. #include <emscripten/emscripten.h>
  19. #endif
  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 CountKeysDown(void)
  140. {
  141. int i, count = 0, max_keys = 0;
  142. const Uint8 *keystate = SDL_GetKeyboardState(&max_keys);
  143. for (i = 0; i < max_keys; ++i) {
  144. if (keystate[i]) {
  145. ++count;
  146. }
  147. }
  148. SDL_Log("Keys down: %d\n", count);
  149. }
  150. static void loop(void)
  151. {
  152. SDL_Event event;
  153. int i;
  154. /* Check for events */
  155. /*SDL_WaitEvent(&event); emscripten does not like waiting*/
  156. while (SDL_PollEvent(&event)) {
  157. switch (event.type) {
  158. case SDL_EVENT_KEY_DOWN:
  159. case SDL_EVENT_KEY_UP:
  160. PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED), (event.key.repeat > 0));
  161. if (event.type == SDL_EVENT_KEY_DOWN) {
  162. switch (event.key.keysym.sym) {
  163. case SDLK_BACKSPACE:
  164. SDLTest_TextWindowAddText(textwin, "\b");
  165. break;
  166. case SDLK_RETURN:
  167. SDLTest_TextWindowAddText(textwin, "\n");
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. CountKeysDown();
  174. break;
  175. case SDL_EVENT_TEXT_EDITING:
  176. PrintText("EDIT", event.edit.text);
  177. break;
  178. case SDL_EVENT_TEXT_INPUT:
  179. PrintText("INPUT", event.text.text);
  180. SDLTest_TextWindowAddText(textwin, "%s", event.text.text);
  181. break;
  182. case SDL_EVENT_FINGER_DOWN:
  183. if (SDL_TextInputActive()) {
  184. SDL_Log("Stopping text input\n");
  185. SDL_StopTextInput();
  186. } else {
  187. SDL_Log("Starting text input\n");
  188. SDL_StartTextInput();
  189. }
  190. break;
  191. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  192. /* Left button quits the app, other buttons toggles text input */
  193. if (event.button.button == SDL_BUTTON_LEFT) {
  194. done = 1;
  195. } else {
  196. if (SDL_TextInputActive()) {
  197. SDL_Log("Stopping text input\n");
  198. SDL_StopTextInput();
  199. } else {
  200. SDL_Log("Starting text input\n");
  201. SDL_StartTextInput();
  202. }
  203. }
  204. break;
  205. case SDL_EVENT_QUIT:
  206. done = 1;
  207. break;
  208. default:
  209. break;
  210. }
  211. }
  212. for (i = 0; i < state->num_windows; i++) {
  213. SDL_SetRenderDrawColor(state->renderers[i], 0, 0, 0, 255);
  214. SDL_RenderClear(state->renderers[i]);
  215. SDL_SetRenderDrawColor(state->renderers[i], 255, 255, 255, 255);
  216. SDLTest_TextWindowDisplay(textwin, state->renderers[i]);
  217. SDL_RenderPresent(state->renderers[i]);
  218. }
  219. /* Slow down framerate */
  220. SDL_Delay(100);
  221. #ifdef SDL_PLATFORM_EMSCRIPTEN
  222. if (done) {
  223. emscripten_cancel_main_loop();
  224. }
  225. #endif
  226. }
  227. int main(int argc, char *argv[])
  228. {
  229. int w, h;
  230. SDL_SetHint(SDL_HINT_WINDOWS_RAW_KEYBOARD, "1");
  231. /* Initialize test framework */
  232. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  233. if (!state) {
  234. return 1;
  235. }
  236. state->window_title = "CheckKeys Test";
  237. /* Enable standard application logging */
  238. SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  239. /* Parse commandline */
  240. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  241. return 1;
  242. }
  243. /* Disable mouse emulation */
  244. SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
  245. /* Initialize SDL */
  246. if (!SDLTest_CommonInit(state)) {
  247. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  248. return 1;
  249. }
  250. SDL_GetWindowSize(state->windows[0], &w, &h);
  251. textwin = SDLTest_TextWindowCreate(0.f, 0.f, (float)w, (float)h);
  252. #ifdef SDL_PLATFORM_IOS
  253. {
  254. int i;
  255. /* Creating the context creates the view, which we need to show keyboard */
  256. for (i = 0; i < state->num_windows; i++) {
  257. SDL_GL_CreateContext(state->windows[i]);
  258. }
  259. }
  260. #endif
  261. SDL_StartTextInput();
  262. /* Print initial modifier state */
  263. SDL_PumpEvents();
  264. PrintModifierState();
  265. /* Watch keystrokes */
  266. done = 0;
  267. #ifdef SDL_PLATFORM_EMSCRIPTEN
  268. emscripten_set_main_loop(loop, 0, 1);
  269. #else
  270. while (!done) {
  271. loop();
  272. }
  273. #endif
  274. SDLTest_TextWindowDestroy(textwin);
  275. SDLTest_CleanupTextDrawing();
  276. SDLTest_CommonQuit(state);
  277. return 0;
  278. }