This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
checkkeys.c
191 lines (170 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
/* Simple program: Loop, watching keystrokes
Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
pump the event loop and catch keystrokes.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
13
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
14
15
static void
quit(int rc)
16
{
17
18
SDL_Quit();
exit(rc);
19
20
}
21
static void
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
print_string(char **text, size_t *maxlen, const char *fmt, ...)
{
int len;
va_list ap;
va_start(ap, fmt);
len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
if (len > 0) {
*text += len;
if (len < *maxlen) {
*maxlen -= len;
} else {
*maxlen = 0;
}
}
va_end(ap);
}
static void
print_modifiers(char **text, size_t *maxlen)
42
{
43
int mod;
44
print_string(text, maxlen, " modifiers:");
45
46
mod = SDL_GetModState();
if (!mod) {
47
print_string(text, maxlen, " (none)");
48
49
50
return;
}
if (mod & KMOD_LSHIFT)
51
print_string(text, maxlen, " LSHIFT");
52
if (mod & KMOD_RSHIFT)
53
print_string(text, maxlen, " RSHIFT");
54
if (mod & KMOD_LCTRL)
55
print_string(text, maxlen, " LCTRL");
56
if (mod & KMOD_RCTRL)
57
print_string(text, maxlen, " RCTRL");
58
if (mod & KMOD_LALT)
59
print_string(text, maxlen, " LALT");
60
if (mod & KMOD_RALT)
61
print_string(text, maxlen, " RALT");
62
if (mod & KMOD_LGUI)
63
print_string(text, maxlen, " LGUI");
64
if (mod & KMOD_RGUI)
65
print_string(text, maxlen, " RGUI");
66
if (mod & KMOD_NUM)
67
print_string(text, maxlen, " NUM");
68
if (mod & KMOD_CAPS)
69
print_string(text, maxlen, " CAPS");
70
if (mod & KMOD_MODE)
71
print_string(text, maxlen, " MODE");
72
73
}
74
static void
75
PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
76
{
77
78
79
80
81
82
83
char message[512];
char *spot;
size_t left;
spot = message;
left = sizeof(message);
84
85
/* Print the keycode, name and state */
if (sym->sym) {
86
87
88
89
90
91
print_string(&spot, &left,
"Key %s: scancode %d = %s, keycode 0x%08X = %s ",
pressed ? "pressed " : "released",
sym->scancode,
SDL_GetScancodeName(sym->scancode),
sym->sym, SDL_GetKeyName(sym->sym));
92
} else {
93
94
95
96
97
print_string(&spot, &left,
"Unknown Key (scancode %d = %s) %s ",
sym->scancode,
SDL_GetScancodeName(sym->scancode),
pressed ? "pressed" : "released");
98
}
99
100
101
102
103
/* Print the translated character, if one exists */
if (sym->unicode) {
/* Is it a control-character? */
if (sym->unicode < ' ') {
104
print_string(&spot, &left, " (^%c)", sym->unicode + '@');
105
} else {
106
#ifdef UNICODE
107
print_string(&spot, &left, " (%c)", sym->unicode);
108
#else
109
110
/* This is a Latin-1 program, so only show 8-bits */
if (!(sym->unicode & 0xFF00))
111
print_string(&spot, &left, " (%c)", sym->unicode);
112
else
113
print_string(&spot, &left, " (0x%X)", sym->unicode);
114
#endif
115
116
}
}
117
print_modifiers(&spot, &left);
118
if (repeat) {
119
print_string(&spot, &left, " (repeat)");
120
}
121
SDL_Log("%s", message);
122
123
}
124
125
126
static void
PrintText(char *text)
{
127
SDL_Log("Text: %s", text);
128
129
}
130
131
132
133
#if __IPHONEOS__
extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_Window * window);
#endif
134
135
int
main(int argc, char *argv[])
136
{
137
SDL_Window *window;
138
139
SDL_Event event;
int done;
140
141
142
143
144
145
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
146
147
/* Set 640x480 video mode */
148
149
150
151
152
window = SDL_CreateWindow("CheckKeys Test",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480, 0);
if (!window) {
fprintf(stderr, "Couldn't create 640x480 window: %s\n",
153
154
155
SDL_GetError());
quit(2);
}
156
157
158
159
160
161
162
163
#if __IPHONEOS__
/* Creating the context creates the view, which we need to show keyboard */
SDL_GL_CreateContext(window);
SDL_iPhoneKeyboardShow(window);
#endif
164
165
166
167
168
169
170
171
/* Watch keystrokes */
done = 0;
while (!done) {
/* Check for events */
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
172
PrintKey(&event.key.keysym, event.key.state, event.key.repeat);
173
break;
174
175
176
case SDL_TEXTINPUT:
PrintText(event.text.text);
break;
177
178
179
180
181
182
183
184
185
case SDL_MOUSEBUTTONDOWN:
/* Any button press quits the app... */
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
186
187
188
SDL_Quit();
return (0);
189
}
190
191
/* vi: set ts=4 sw=4 expandtab: */