test/checkkeys.c
changeset 5461 94f742ce580a
parent 5218 572a73d71b5f
child 5535 96594ac5fd1a
equal deleted inserted replaced
5460:43582fccdc0d 5461:94f742ce580a
    17     SDL_Quit();
    17     SDL_Quit();
    18     exit(rc);
    18     exit(rc);
    19 }
    19 }
    20 
    20 
    21 static void
    21 static void
    22 print_modifiers(void)
    22 print_string(char **text, size_t *maxlen, const char *fmt, ...)
       
    23 {
       
    24     int len;
       
    25     va_list ap;
       
    26 
       
    27     va_start(ap, fmt);
       
    28     len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
       
    29     if (len > 0) {
       
    30         *text += len;
       
    31         if (len < *maxlen) {
       
    32             *maxlen -= len;
       
    33         } else {
       
    34             *maxlen = 0;
       
    35         }
       
    36     }
       
    37     va_end(ap);
       
    38 }
       
    39 
       
    40 static void
       
    41 print_modifiers(char **text, size_t *maxlen)
    23 {
    42 {
    24     int mod;
    43     int mod;
    25     printf(" modifiers:");
    44     print_string(text, maxlen, " modifiers:");
    26     mod = SDL_GetModState();
    45     mod = SDL_GetModState();
    27     if (!mod) {
    46     if (!mod) {
    28         printf(" (none)");
    47         print_string(text, maxlen, " (none)");
    29         return;
    48         return;
    30     }
    49     }
    31     if (mod & KMOD_LSHIFT)
    50     if (mod & KMOD_LSHIFT)
    32         printf(" LSHIFT");
    51         print_string(text, maxlen, " LSHIFT");
    33     if (mod & KMOD_RSHIFT)
    52     if (mod & KMOD_RSHIFT)
    34         printf(" RSHIFT");
    53         print_string(text, maxlen, " RSHIFT");
    35     if (mod & KMOD_LCTRL)
    54     if (mod & KMOD_LCTRL)
    36         printf(" LCTRL");
    55         print_string(text, maxlen, " LCTRL");
    37     if (mod & KMOD_RCTRL)
    56     if (mod & KMOD_RCTRL)
    38         printf(" RCTRL");
    57         print_string(text, maxlen, " RCTRL");
    39     if (mod & KMOD_LALT)
    58     if (mod & KMOD_LALT)
    40         printf(" LALT");
    59         print_string(text, maxlen, " LALT");
    41     if (mod & KMOD_RALT)
    60     if (mod & KMOD_RALT)
    42         printf(" RALT");
    61         print_string(text, maxlen, " RALT");
    43     if (mod & KMOD_LGUI)
    62     if (mod & KMOD_LGUI)
    44         printf(" LGUI");
    63         print_string(text, maxlen, " LGUI");
    45     if (mod & KMOD_RGUI)
    64     if (mod & KMOD_RGUI)
    46         printf(" RGUI");
    65         print_string(text, maxlen, " RGUI");
    47     if (mod & KMOD_NUM)
    66     if (mod & KMOD_NUM)
    48         printf(" NUM");
    67         print_string(text, maxlen, " NUM");
    49     if (mod & KMOD_CAPS)
    68     if (mod & KMOD_CAPS)
    50         printf(" CAPS");
    69         print_string(text, maxlen, " CAPS");
    51     if (mod & KMOD_MODE)
    70     if (mod & KMOD_MODE)
    52         printf(" MODE");
    71         print_string(text, maxlen, " MODE");
    53 }
    72 }
    54 
    73 
    55 static void
    74 static void
    56 PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
    75 PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
    57 {
    76 {
       
    77     char message[512];
       
    78     char *spot;
       
    79     size_t left;
       
    80 
       
    81     spot = message;
       
    82     left = sizeof(message);
       
    83 
    58     /* Print the keycode, name and state */
    84     /* Print the keycode, name and state */
    59     if (sym->sym) {
    85     if (sym->sym) {
    60         printf("Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
    86         print_string(&spot, &left,
    61                pressed ? "pressed " : "released",
    87                 "Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
    62                sym->scancode,
    88                 pressed ? "pressed " : "released",
    63                SDL_GetScancodeName(sym->scancode),
    89                 sym->scancode,
    64                sym->sym, SDL_GetKeyName(sym->sym));
    90                 SDL_GetScancodeName(sym->scancode),
       
    91                 sym->sym, SDL_GetKeyName(sym->sym));
    65     } else {
    92     } else {
    66         printf("Unknown Key (scancode %d = %s) %s ",
    93         print_string(&spot, &left,
    67                sym->scancode,
    94                 "Unknown Key (scancode %d = %s) %s ",
    68                SDL_GetScancodeName(sym->scancode),
    95                 sym->scancode,
    69                pressed ? "pressed" : "released");
    96                 SDL_GetScancodeName(sym->scancode),
       
    97                 pressed ? "pressed" : "released");
    70     }
    98     }
    71 
    99 
    72     /* Print the translated character, if one exists */
   100     /* Print the translated character, if one exists */
    73     if (sym->unicode) {
   101     if (sym->unicode) {
    74         /* Is it a control-character? */
   102         /* Is it a control-character? */
    75         if (sym->unicode < ' ') {
   103         if (sym->unicode < ' ') {
    76             printf(" (^%c)", sym->unicode + '@');
   104             print_string(&spot, &left, " (^%c)", sym->unicode + '@');
    77         } else {
   105         } else {
    78 #ifdef UNICODE
   106 #ifdef UNICODE
    79             printf(" (%c)", sym->unicode);
   107             print_string(&spot, &left, " (%c)", sym->unicode);
    80 #else
   108 #else
    81             /* This is a Latin-1 program, so only show 8-bits */
   109             /* This is a Latin-1 program, so only show 8-bits */
    82             if (!(sym->unicode & 0xFF00))
   110             if (!(sym->unicode & 0xFF00))
    83                 printf(" (%c)", sym->unicode);
   111                 print_string(&spot, &left, " (%c)", sym->unicode);
    84             else
   112             else
    85                 printf(" (0x%X)", sym->unicode);
   113                 print_string(&spot, &left, " (0x%X)", sym->unicode);
    86 #endif
   114 #endif
    87         }
   115         }
    88     }
   116     }
    89     print_modifiers();
   117     print_modifiers(&spot, &left);
    90     if (repeat) {
   118     if (repeat) {
    91         printf(" (repeat)");
   119         print_string(&spot, &left, " (repeat)");
    92     }
   120     }
    93     printf("\n");
   121     SDL_Log("%s", message);
    94 }
   122 }
    95 
   123 
    96 static void
   124 static void
    97 PrintText(char *text)
   125 PrintText(char *text)
    98 {
   126 {
    99     printf("Text: %s\n", text);
   127     SDL_Log("Text: %s", text);
   100 }
   128 }
       
   129 
       
   130 #if __IPHONEOS__
       
   131 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_Window * window);
       
   132 #endif
   101 
   133 
   102 int
   134 int
   103 main(int argc, char *argv[])
   135 main(int argc, char *argv[])
   104 {
   136 {
       
   137     SDL_Window *window;
   105     SDL_Event event;
   138     SDL_Event event;
   106     int done;
   139     int done;
   107     Uint32 videoflags;
       
   108 
   140 
   109     /* Initialize SDL */
   141     /* Initialize SDL */
   110     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   142     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   111         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   143         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   112         return (1);
   144         return (1);
   113     }
   145     }
   114 
   146 
   115     videoflags = SDL_SWSURFACE;
       
   116     while (argc > 1) {
       
   117         --argc;
       
   118         if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
       
   119             videoflags |= SDL_FULLSCREEN;
       
   120         } else {
       
   121             fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
       
   122             quit(1);
       
   123         }
       
   124     }
       
   125 
       
   126     /* Set 640x480 video mode */
   147     /* Set 640x480 video mode */
   127     if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
   148     window = SDL_CreateWindow("CheckKeys Test",
   128         fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
   149                               SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
       
   150                               640, 480, 0);
       
   151     if (!window) {
       
   152         fprintf(stderr, "Couldn't create 640x480 window: %s\n",
   129                 SDL_GetError());
   153                 SDL_GetError());
   130         quit(2);
   154         quit(2);
   131     }
   155     }
       
   156 
       
   157 #if __IPHONEOS__
       
   158     /* Creating the context creates the view, which we need to show keyboard */
       
   159     SDL_GL_CreateContext(window);
       
   160     SDL_iPhoneKeyboardShow(window);
       
   161 #endif
       
   162     
   132 
   163 
   133     /* Watch keystrokes */
   164     /* Watch keystrokes */
   134     done = 0;
   165     done = 0;
   135     while (!done) {
   166     while (!done) {
   136         /* Check for events */
   167         /* Check for events */
   154     }
   185     }
   155 
   186 
   156     SDL_Quit();
   187     SDL_Quit();
   157     return (0);
   188     return (0);
   158 }
   189 }
       
   190 
       
   191 /* vi: set ts=4 sw=4 expandtab: */