Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
200 lines (178 loc) · 5.15 KB

checkkeys.c

File metadata and controls

200 lines (178 loc) · 5.15 KB
 
Jun 22, 2011
Jun 22, 2011
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
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* 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"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
static void
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;
Sep 9, 2011
Sep 9, 2011
42
43
if ( ((size_t) len) < *maxlen ) {
*maxlen -= (size_t) len;
Jun 22, 2011
Jun 22, 2011
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
} else {
*maxlen = 0;
}
}
va_end(ap);
}
static void
print_modifiers(char **text, size_t *maxlen)
{
int mod;
print_string(text, maxlen, " modifiers:");
mod = SDL_GetModState();
if (!mod) {
print_string(text, maxlen, " (none)");
return;
}
if (mod & KMOD_LSHIFT)
print_string(text, maxlen, " LSHIFT");
if (mod & KMOD_RSHIFT)
print_string(text, maxlen, " RSHIFT");
if (mod & KMOD_LCTRL)
print_string(text, maxlen, " LCTRL");
if (mod & KMOD_RCTRL)
print_string(text, maxlen, " RCTRL");
if (mod & KMOD_LALT)
print_string(text, maxlen, " LALT");
if (mod & KMOD_RALT)
print_string(text, maxlen, " RALT");
if (mod & KMOD_LGUI)
print_string(text, maxlen, " LGUI");
if (mod & KMOD_RGUI)
print_string(text, maxlen, " RGUI");
if (mod & KMOD_NUM)
print_string(text, maxlen, " NUM");
if (mod & KMOD_CAPS)
print_string(text, maxlen, " CAPS");
if (mod & KMOD_MODE)
print_string(text, maxlen, " MODE");
}
static void
PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
{
char message[512];
char *spot;
size_t left;
spot = message;
left = sizeof(message);
/* Print the keycode, name and state */
if (sym->sym) {
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));
} else {
print_string(&spot, &left,
"Unknown Key (scancode %d = %s) %s ",
sym->scancode,
SDL_GetScancodeName(sym->scancode),
pressed ? "pressed" : "released");
}
/* Print the translated character, if one exists */
if (sym->unicode) {
/* Is it a control-character? */
if (sym->unicode < ' ') {
print_string(&spot, &left, " (^%c)", sym->unicode + '@');
} else {
#ifdef UNICODE
print_string(&spot, &left, " (%c)", sym->unicode);
#else
/* This is a Latin-1 program, so only show 8-bits */
if (!(sym->unicode & 0xFF00))
print_string(&spot, &left, " (%c)", sym->unicode);
else
print_string(&spot, &left, " (0x%X)", sym->unicode);
#endif
}
}
print_modifiers(&spot, &left);
if (repeat) {
print_string(&spot, &left, " (repeat)");
}
SDL_Log("%s", message);
}
static void
PrintText(char *text)
{
SDL_Log("Text: %s", text);
}
int
main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Event event;
int done;
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
/* Set 640x480 video mode */
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",
SDL_GetError());
quit(2);
}
#if __IPHONEOS__
/* Creating the context creates the view, which we need to show keyboard */
SDL_GL_CreateContext(window);
#endif
Aug 11, 2012
Aug 11, 2012
168
169
170
171
if (SDL_HasScreenKeyboardSupport(window)) {
SDL_ShowScreenKeyboard(window);
}
Jun 22, 2011
Jun 22, 2011
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* Watch keystrokes */
done = 0;
while (!done) {
/* Check for events */
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
PrintKey(&event.key.keysym, event.key.state, event.key.repeat);
break;
case SDL_TEXTINPUT:
PrintText(event.text.text);
break;
case SDL_MOUSEBUTTONDOWN:
/* Any button press quits the app... */
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
SDL_Quit();
return (0);
}
/* vi: set ts=4 sw=4 expandtab: */