checkkeys.c 8.0 KB

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