🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
6.2 KiB

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