|
|
1.1 root 1: /*
2: * Simple program: Loop, watching keystrokes
3: *
4: * Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
5: * pump the event loop and catch keystrokes.
6: */
7:
8: #include <stdio.h>
9: #include <stdlib.h>
10: #include <string.h>
11:
12: #include "SDL.h"
13:
1.1.1.2 ! root 14: #if SDL_MAJOR_VERSION > 1
! 15: #define SDL_FULLSCREEN SDL_WINDOW_FULLSCREEN
! 16: #define SDL_keysym SDL_Keysym
! 17: #define KMOD_LMETA KMOD_LGUI
! 18: #define KMOD_RMETA KMOD_RGUI
! 19: #endif
! 20:
! 21:
1.1 root 22: /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
23: static void quit(int rc)
24: {
25: SDL_Quit();
26: exit(rc);
27: }
28:
29: static void print_modifiers(void)
30: {
31: int mod;
32: printf(" modifiers:");
33: mod = SDL_GetModState();
34: if(!mod) {
35: printf(" (none)");
36: return;
37: }
38: if(mod & KMOD_LSHIFT)
39: printf(" LSHIFT");
40: if(mod & KMOD_RSHIFT)
41: printf(" RSHIFT");
42: if(mod & KMOD_LCTRL)
43: printf(" LCTRL");
44: if(mod & KMOD_RCTRL)
45: printf(" RCTRL");
46: if(mod & KMOD_LALT)
47: printf(" LALT");
48: if(mod & KMOD_RALT)
49: printf(" RALT");
50: if(mod & KMOD_LMETA)
51: printf(" LMETA");
52: if(mod & KMOD_RMETA)
53: printf(" RMETA");
54: if(mod & KMOD_NUM)
55: printf(" NUMLOCK");
56: if(mod & KMOD_CAPS)
57: printf(" CAPS");
58: if(mod & KMOD_MODE)
59: printf(" MODE");
60: }
61:
62: static void PrintKey(SDL_keysym *sym, int pressed)
63: {
64: /* Print the keycode, name and state */
65: if ( sym->sym ) {
1.1.1.2 ! root 66: printf("Key %s: 0x%2x - %s ",
1.1 root 67: pressed ? "pressed " : "released",
68: sym->sym, SDL_GetKeyName(sym->sym));
69: } else {
1.1.1.2 ! root 70: printf("Unknown Key (scancode = 0x%2x) %s ",
1.1 root 71: sym->scancode,
72: pressed ? "pressed" : "released");
73: }
74: print_modifiers();
75:
1.1.1.2 ! root 76: #if SDL_MAJOR_VERSION < 2
1.1 root 77: /* Print the translated character, if one exists */
78: if ( sym->unicode ) {
79: /* Is it a control-character? */
80: if ( sym->unicode < ' ' ) {
81: printf(" (^%c)", sym->unicode+'@');
82: } else {
1.1.1.2 ! root 83: # ifdef UNICODE
1.1 root 84: printf(" (%c)", sym->unicode);
1.1.1.2 ! root 85: # else
1.1 root 86: /* This is a Latin-1 program, so only show 8-bits */
87: if ( !(sym->unicode & 0xFF00) )
88: printf(" (%c)", sym->unicode);
89: else
90: printf(" (0x%X)", sym->unicode);
1.1.1.2 ! root 91: # endif
1.1 root 92: }
93: }
1.1.1.2 ! root 94: #endif
1.1 root 95: printf("\n");
96: }
97:
98: int main(int argc, char *argv[])
99: {
1.1.1.2 ! root 100: #if SDL_MAJOR_VERSION > 1
! 101: SDL_Window *window;
! 102: #endif
! 103: Uint32 videoflags = 0;
1.1 root 104: SDL_Event event;
1.1.1.2 ! root 105: int wd = 640;
! 106: int ht = 480;
1.1 root 107: int done;
108:
109: /* Initialize SDL */
110: if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
111: fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
112: return(1);
113: }
114:
115: while( argc > 1 ) {
116: --argc;
117: if ( argv[argc] && !strcmp(argv[argc], "-fullscreen") ) {
1.1.1.2 ! root 118: videoflags = SDL_FULLSCREEN;
1.1 root 119: } else {
120: fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
121: quit(1);
122: }
123: }
124:
1.1.1.2 ! root 125: #if SDL_MAJOR_VERSION > 1
! 126: if (videoflags & SDL_WINDOW_FULLSCREEN) {
! 127: SDL_DisplayMode dm;
! 128: videoflags |= SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_BORDERLESS | SDL_WINDOW_INPUT_GRABBED;
! 129: if (SDL_GetDesktopDisplayMode(0, &dm)) {
! 130: fprintf(stderr, "SDL_GetDesktopDisplayMode failed: %s", SDL_GetError());
! 131: quit(1);
! 132: }
! 133: wd = dm.w;
! 134: ht = dm.h;
! 135: } else {
! 136: videoflags = SDL_WINDOW_RESIZABLE;
! 137: }
! 138:
! 139: window = SDL_CreateWindow("CheckKeys",
! 140: SDL_WINDOWPOS_UNDEFINED,
! 141: SDL_WINDOWPOS_UNDEFINED,
! 142: wd, ht, videoflags);
! 143: if (!window) {
! 144: fprintf(stderr, "Failed to create %dx%d window: %s\n",
! 145: wd, ht, SDL_GetError());
! 146: quit(2);
! 147: }
! 148: #else /* SDL1 */
! 149: videoflags |= SDL_SWSURFACE;
! 150: if ( SDL_SetVideoMode(wd, ht, 0, videoflags) == NULL ) {
! 151: fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
! 152: wd, ht, SDL_GetError());
1.1 root 153: quit(2);
154: }
155:
156: /* Enable UNICODE translation for keyboard input */
157: SDL_EnableUNICODE(1);
158:
159: /* Enable auto repeat for keyboard input */
160: SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
161: SDL_DEFAULT_REPEAT_INTERVAL);
1.1.1.2 ! root 162: #endif
1.1 root 163:
164: puts("Click to the window to quit.\n");
165:
166: /* Watch keystrokes */
167: done = 0;
168: while ( !done ) {
169: /* Check for events */
170: SDL_WaitEvent(&event);
171: switch (event.type) {
172: case SDL_KEYDOWN:
173: PrintKey(&event.key.keysym, 1);
174: break;
175: case SDL_KEYUP:
176: PrintKey(&event.key.keysym, 0);
177: break;
178: case SDL_MOUSEBUTTONDOWN:
179: /* Any button press quits the app... */
180: case SDL_QUIT:
181: done = 1;
182: break;
183: default:
184: break;
185: }
186: }
187:
188: SDL_Quit();
189: return 0;
190: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.