|
|
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:
14: /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
15: static void quit(int rc)
16: {
17: SDL_Quit();
18: exit(rc);
19: }
20:
21: static void print_modifiers(void)
22: {
23: int mod;
24: printf(" modifiers:");
25: mod = SDL_GetModState();
26: if(!mod) {
27: printf(" (none)");
28: return;
29: }
30: if(mod & KMOD_LSHIFT)
31: printf(" LSHIFT");
32: if(mod & KMOD_RSHIFT)
33: printf(" RSHIFT");
34: if(mod & KMOD_LCTRL)
35: printf(" LCTRL");
36: if(mod & KMOD_RCTRL)
37: printf(" RCTRL");
38: if(mod & KMOD_LALT)
39: printf(" LALT");
40: if(mod & KMOD_RALT)
41: printf(" RALT");
42: if(mod & KMOD_LMETA)
43: printf(" LMETA");
44: if(mod & KMOD_RMETA)
45: printf(" RMETA");
46: if(mod & KMOD_NUM)
47: printf(" NUMLOCK");
48: if(mod & KMOD_CAPS)
49: printf(" CAPS");
50: if(mod & KMOD_MODE)
51: printf(" MODE");
52: }
53:
54: static void PrintKey(SDL_keysym *sym, int pressed)
55: {
56: /* Print the keycode, name and state */
57: if ( sym->sym ) {
58: printf("Key %s: %3d - %s ",
59: pressed ? "pressed " : "released",
60: sym->sym, SDL_GetKeyName(sym->sym));
61: } else {
62: printf("Unknown Key (scancode = %d) %s ",
63: sym->scancode,
64: pressed ? "pressed" : "released");
65: }
66: print_modifiers();
67:
68: /* Print the translated character, if one exists */
69: if ( sym->unicode ) {
70: /* Is it a control-character? */
71: if ( sym->unicode < ' ' ) {
72: printf(" (^%c)", sym->unicode+'@');
73: } else {
74: #ifdef UNICODE
75: printf(" (%c)", sym->unicode);
76: #else
77: /* This is a Latin-1 program, so only show 8-bits */
78: if ( !(sym->unicode & 0xFF00) )
79: printf(" (%c)", sym->unicode);
80: else
81: printf(" (0x%X)", sym->unicode);
82: #endif
83: }
84: }
85: printf("\n");
86: }
87:
88: int main(int argc, char *argv[])
89: {
90: SDL_Event event;
91: int done;
92: Uint32 videoflags;
93:
94: /* Initialize SDL */
95: if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
96: fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
97: return(1);
98: }
99:
100: videoflags = SDL_SWSURFACE;
101: while( argc > 1 ) {
102: --argc;
103: if ( argv[argc] && !strcmp(argv[argc], "-fullscreen") ) {
104: videoflags |= SDL_FULLSCREEN;
105: } else {
106: fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
107: quit(1);
108: }
109: }
110:
111: /* Set 640x480 video mode */
112: if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) {
113: fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
114: SDL_GetError());
115: quit(2);
116: }
117:
118: /* Enable UNICODE translation for keyboard input */
119: SDL_EnableUNICODE(1);
120:
121: /* Enable auto repeat for keyboard input */
122: SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
123: SDL_DEFAULT_REPEAT_INTERVAL);
124:
125: puts("Click to the window to quit.\n");
126:
127: /* Watch keystrokes */
128: done = 0;
129: while ( !done ) {
130: /* Check for events */
131: SDL_WaitEvent(&event);
132: switch (event.type) {
133: case SDL_KEYDOWN:
134: PrintKey(&event.key.keysym, 1);
135: break;
136: case SDL_KEYUP:
137: PrintKey(&event.key.keysym, 0);
138: break;
139: case SDL_MOUSEBUTTONDOWN:
140: /* Any button press quits the app... */
141: case SDL_QUIT:
142: done = 1;
143: break;
144: default:
145: break;
146: }
147: }
148:
149: SDL_Quit();
150: return 0;
151: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.