checkkeys.c 5.0 KB

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