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

Latest commit

 

History

History
202 lines (179 loc) · 5.17 KB

checkkeys.c

File metadata and controls

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