checkkeys.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #include <stdlib.h>
  15. #ifdef __EMSCRIPTEN__
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. #include <SDL3/SDL.h>
  19. #include <SDL3/SDL_main.h>
  20. #include <SDL3/SDL_test_font.h>
  21. static SDL_Window *window;
  22. static SDL_Renderer *renderer;
  23. static SDLTest_TextWindow *textwin;
  24. static int done;
  25. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  26. static void
  27. quit(int rc)
  28. {
  29. SDL_Quit();
  30. exit(rc);
  31. }
  32. static void
  33. print_string(char **text, size_t *maxlen, const char *fmt, ...)
  34. {
  35. int len;
  36. va_list ap;
  37. va_start(ap, fmt);
  38. len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
  39. if (len > 0) {
  40. *text += len;
  41. if (((size_t)len) < *maxlen) {
  42. *maxlen -= (size_t)len;
  43. } else {
  44. *maxlen = 0;
  45. }
  46. }
  47. va_end(ap);
  48. }
  49. static void
  50. print_modifiers(char **text, size_t *maxlen)
  51. {
  52. int mod;
  53. print_string(text, maxlen, " modifiers:");
  54. mod = SDL_GetModState();
  55. if (!mod) {
  56. print_string(text, maxlen, " (none)");
  57. return;
  58. }
  59. if (mod & SDL_KMOD_LSHIFT) {
  60. print_string(text, maxlen, " LSHIFT");
  61. }
  62. if (mod & SDL_KMOD_RSHIFT) {
  63. print_string(text, maxlen, " RSHIFT");
  64. }
  65. if (mod & SDL_KMOD_LCTRL) {
  66. print_string(text, maxlen, " LCTRL");
  67. }
  68. if (mod & SDL_KMOD_RCTRL) {
  69. print_string(text, maxlen, " RCTRL");
  70. }
  71. if (mod & SDL_KMOD_LALT) {
  72. print_string(text, maxlen, " LALT");
  73. }
  74. if (mod & SDL_KMOD_RALT) {
  75. print_string(text, maxlen, " RALT");
  76. }
  77. if (mod & SDL_KMOD_LGUI) {
  78. print_string(text, maxlen, " LGUI");
  79. }
  80. if (mod & SDL_KMOD_RGUI) {
  81. print_string(text, maxlen, " RGUI");
  82. }
  83. if (mod & SDL_KMOD_NUM) {
  84. print_string(text, maxlen, " NUM");
  85. }
  86. if (mod & SDL_KMOD_CAPS) {
  87. print_string(text, maxlen, " CAPS");
  88. }
  89. if (mod & SDL_KMOD_MODE) {
  90. print_string(text, maxlen, " MODE");
  91. }
  92. if (mod & SDL_KMOD_SCROLL) {
  93. print_string(text, maxlen, " SCROLL");
  94. }
  95. }
  96. static void
  97. PrintModifierState(void)
  98. {
  99. char message[512];
  100. char *spot;
  101. size_t left;
  102. spot = message;
  103. left = sizeof(message);
  104. print_modifiers(&spot, &left);
  105. SDL_Log("Initial state:%s\n", message);
  106. }
  107. static void
  108. PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
  109. {
  110. char message[512];
  111. char *spot;
  112. size_t left;
  113. spot = message;
  114. left = sizeof(message);
  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,
  121. SDL_GetScancodeName(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,
  127. SDL_GetScancodeName(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. }
  136. static void
  137. PrintText(const char *eventtype, const char *text)
  138. {
  139. const char *spot;
  140. char expanded[1024];
  141. expanded[0] = '\0';
  142. for (spot = text; *spot; ++spot) {
  143. size_t length = SDL_strlen(expanded);
  144. (void)SDL_snprintf(expanded + length, sizeof expanded - length, "\\x%.2x", (unsigned char)*spot);
  145. }
  146. SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
  147. }
  148. static void loop(void)
  149. {
  150. SDL_Event event;
  151. /* Check for events */
  152. /*SDL_WaitEvent(&event); emscripten does not like waiting*/
  153. while (SDL_PollEvent(&event)) {
  154. switch (event.type) {
  155. case SDL_EVENT_KEY_DOWN:
  156. case SDL_EVENT_KEY_UP:
  157. PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
  158. if (event.type == SDL_EVENT_KEY_DOWN) {
  159. switch (event.key.keysym.sym) {
  160. case SDLK_BACKSPACE:
  161. SDLTest_TextWindowAddText(textwin, "\b");
  162. break;
  163. case SDLK_RETURN:
  164. SDLTest_TextWindowAddText(textwin, "\n");
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. break;
  171. case SDL_EVENT_TEXT_EDITING:
  172. PrintText("EDIT", event.edit.text);
  173. break;
  174. case SDL_EVENT_TEXT_EDITING_EXT:
  175. PrintText("EDIT_EXT", event.editExt.text);
  176. SDL_free(event.editExt.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. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  213. SDL_RenderClear(renderer);
  214. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  215. SDLTest_TextWindowDisplay(textwin, renderer);
  216. SDL_RenderPresent(renderer);
  217. /* Slow down framerate */
  218. SDL_Delay(100);
  219. #ifdef __EMSCRIPTEN__
  220. if (done) {
  221. emscripten_cancel_main_loop();
  222. }
  223. #endif
  224. }
  225. int main(int argc, char *argv[])
  226. {
  227. /* Enable standard application logging */
  228. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  229. /* Disable mouse emulation */
  230. SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
  231. /* Enable extended text editing events */
  232. SDL_SetHint(SDL_HINT_IME_SUPPORT_EXTENDED_TEXT, "1");
  233. /* Initialize SDL */
  234. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  235. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  236. return 1;
  237. }
  238. /* Set 640x480 video mode */
  239. window = SDL_CreateWindow("CheckKeys Test", 640, 480, 0);
  240. if (window == NULL) {
  241. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
  242. SDL_GetError());
  243. quit(2);
  244. }
  245. renderer = SDL_CreateRenderer(window, NULL, 0);
  246. if (renderer == NULL) {
  247. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n",
  248. SDL_GetError());
  249. quit(2);
  250. }
  251. textwin = SDLTest_TextWindowCreate(0, 0, 640, 480);
  252. #if __IOS__
  253. /* Creating the context creates the view, which we need to show keyboard */
  254. SDL_GL_CreateContext(window);
  255. #endif
  256. SDL_StartTextInput();
  257. /* Print initial modifier state */
  258. SDL_PumpEvents();
  259. PrintModifierState();
  260. /* Watch keystrokes */
  261. done = 0;
  262. #ifdef __EMSCRIPTEN__
  263. emscripten_set_main_loop(loop, 0, 1);
  264. #else
  265. while (!done) {
  266. loop();
  267. }
  268. #endif
  269. SDL_Quit();
  270. return 0;
  271. }