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