/
events-waitevent-keyboard.c
executable file
·93 lines (77 loc) · 2.68 KB
/
events-waitevent-keyboard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* events-waitevent-keyboard.c
*
* WaitEvent:
* Waits indefinitely for the next available event, returning 0 if there was an error
* while waiting for events, 1 otherwise.
* If event is not NULL, the next event is removed from the queue and stored in that area.
* As this function implicitly calls SDL_PumpEvents, you can only call this function
* in the thread that set the video mode.
*/
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
void print_kmod(int modifier)
{
printf("LSHIFT=%d ", modifier & KMOD_LSHIFT);
printf("RSHIFT=%d ", modifier & KMOD_RSHIFT);
printf("LCTRL=%d ", modifier & KMOD_LCTRL);
printf("RCTRL=%d ", modifier & KMOD_RCTRL);
printf("LALT=%d ", modifier & KMOD_LALT);
printf("RALT=%d ", modifier & KMOD_RALT);
printf("LMETA=%d ", modifier & KMOD_LMETA);
printf("RMETA=%d ", modifier & KMOD_RMETA);
printf("NUM=%d ", modifier & KMOD_NUM);
printf("CAPS=%d ", modifier & KMOD_CAPS);
printf("MODE=%d\n", modifier & KMOD_MODE);
}
int main()
{
SDL_Surface *screen;
SDL_Event event;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
/* Attempt to set a 256x256 hicolor (16-bit) video mode. */
screen = SDL_SetVideoMode(256, 256, 16, 0);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 2;
}
printf("Press 'Q' to quit.\n");
/* Start the event loop. Keep reading events until there
is an error, or the user presses a mouse button. */
while (SDL_WaitEvent(&event) != 0) {
SDL_keysym keysym;
/* SDL_WaitEvent has filled in our event structure
with the next event. We check its type field to
find out what happened. */
switch (event.type) {
case SDL_KEYDOWN:
printf("Key pressed (state = %d). ", event.key.state);
keysym = event.key.keysym;
printf("SDL keysym is %i. ", keysym.sym);
printf("(%s) ", SDL_GetKeyName(keysym.sym));
/* Report the left shift modifier */
print_kmod(event.key.keysym.mod);
/* Did the user press Q? */
if (keysym.sym == SDLK_q) {
printf("'Q' pressed, exiting.\n");
exit(0);
}
break;
case SDL_KEYUP:
printf("Key released (state = %d). ", event.key.state);
printf("SDL keysym is %i. ", keysym.sym);
printf("(%s) ", SDL_GetKeyName(keysym.sym));
print_kmod(event.key.keysym.mod);
break;
case SDL_QUIT:
printf("Quit event. Bye.\n");
exit(0);
}
}
return 0;
}