Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
2 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Simple program: Loop, watching keystrokes
14 Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
15 pump the event loop and catch keystrokes.
24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
33 print_string(char **text, size_t *maxlen, const char *fmt, ...)
39 len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
42 if ( ((size_t) len) < *maxlen ) {
43 *maxlen -= (size_t) len;
52 print_modifiers(char **text, size_t *maxlen)
55 print_string(text, maxlen, " modifiers:");
56 mod = SDL_GetModState();
58 print_string(text, maxlen, " (none)");
61 if (mod & KMOD_LSHIFT)
62 print_string(text, maxlen, " LSHIFT");
63 if (mod & KMOD_RSHIFT)
64 print_string(text, maxlen, " RSHIFT");
66 print_string(text, maxlen, " LCTRL");
68 print_string(text, maxlen, " RCTRL");
70 print_string(text, maxlen, " LALT");
72 print_string(text, maxlen, " RALT");
74 print_string(text, maxlen, " LGUI");
76 print_string(text, maxlen, " RGUI");
78 print_string(text, maxlen, " NUM");
80 print_string(text, maxlen, " CAPS");
82 print_string(text, maxlen, " MODE");
86 PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
93 left = sizeof(message);
95 /* Print the keycode, name and state */
97 print_string(&spot, &left,
98 "Key %s: scancode %d = %s, keycode 0x%08X = %s ",
99 pressed ? "pressed " : "released",
101 SDL_GetScancodeName(sym->scancode),
102 sym->sym, SDL_GetKeyName(sym->sym));
104 print_string(&spot, &left,
105 "Unknown Key (scancode %d = %s) %s ",
107 SDL_GetScancodeName(sym->scancode),
108 pressed ? "pressed " : "released");
110 print_modifiers(&spot, &left);
112 print_string(&spot, &left, " (repeat)");
114 SDL_Log("%s\n", message);
118 PrintText(char *text)
120 char *spot, expanded[1024];
123 for ( spot = text; *spot; ++spot )
125 size_t length = SDL_strlen(expanded);
126 SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
128 SDL_Log("Text (%s): \"%s%s\"\n", expanded, *text == '"' ? "\\" : "", text);
132 main(int argc, char *argv[])
138 /* Enable standard application logging */
139 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
142 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
143 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
147 /* Set 640x480 video mode */
148 window = SDL_CreateWindow("CheckKeys Test",
149 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
152 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
158 /* Creating the context creates the view, which we need to show keyboard */
159 SDL_GL_CreateContext(window);
162 SDL_StartTextInput();
164 /* Watch keystrokes */
167 /* Check for events */
168 SDL_WaitEvent(&event);
169 switch (event.type) {
172 PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
175 PrintText(event.text.text);
177 case SDL_MOUSEBUTTONDOWN:
178 /* Any button press quits the app... */
191 /* vi: set ts=4 sw=4 expandtab: */