SDL  2.0
checkkeys.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
+ Include dependency graph for checkkeys.c:

Go to the source code of this file.

Functions

static void quit (int rc)
static void print_string (char **text, size_t *maxlen, const char *fmt,...)
static void print_modifiers (char **text, size_t *maxlen)
static void PrintModifierState ()
static void PrintKey (SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
static void PrintText (char *eventtype, char *text)
void loop ()
int main (int argc, char *argv[])

Variables

int done

Function Documentation

void loop ( )

Definition at line 152 of file checkkeys.c.

References SDL_MouseButtonEvent::button, SDL_Event::button, done, SDL_Event::key, SDL_KeyboardEvent::keysym, PrintKey(), PrintText(), SDL_KeyboardEvent::repeat, SDL_BUTTON_LEFT, SDL_FALSE, SDL_IsTextInputActive, SDL_KEYDOWN, SDL_KEYUP, SDL_Log, SDL_MOUSEBUTTONDOWN, SDL_PollEvent, SDL_PRESSED, SDL_QUIT, SDL_StartTextInput, SDL_StopTextInput, SDL_TEXTEDITING, SDL_TEXTINPUT, SDL_TRUE, SDL_KeyboardEvent::state, SDL_TextInputEvent::text, SDL_Event::text, and SDL_Event::type.

Referenced by main(), test_multi_audio(), WatchGameController(), and WatchJoystick().

{
/* Check for events */
/*SDL_WaitEvent(&event); emscripten does not like waiting*/
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYUP:
PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
break;
PrintText("EDIT", event.text.text);
break;
PrintText("INPUT", event.text.text);
break;
/* Left button quits the app, other buttons toggles text input */
if (event.button.button == SDL_BUTTON_LEFT) {
done = 1;
} else {
SDL_Log("Stopping text input\n");
} else {
SDL_Log("Starting text input\n");
}
}
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
#ifdef __EMSCRIPTEN__
if (done) {
emscripten_cancel_main_loop();
}
#endif
}
int main ( int  argc,
char *  argv[] 
)

Definition at line 199 of file checkkeys.c.

References done, loop(), PrintModifierState(), quit(), SDL_CreateWindow, SDL_GetError, SDL_GL_CreateContext, SDL_Init, SDL_INIT_VIDEO, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_PumpEvents, SDL_Quit, SDL_StartTextInput, and SDL_WINDOWPOS_CENTERED.

{
/* Enable standard application logging */
/* Initialize SDL */
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
/* Set 640x480 video mode */
window = SDL_CreateWindow("CheckKeys Test",
640, 480, 0);
if (!window) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
quit(2);
}
#if __IPHONEOS__
/* Creating the context creates the view, which we need to show keyboard */
#endif
/* Print initial modifier state */
/* Watch keystrokes */
done = 0;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while (!done) {
loop();
}
#endif
return (0);
}
static void print_modifiers ( char **  text,
size_t maxlen 
)
static

Definition at line 58 of file checkkeys.c.

References KMOD_CAPS, KMOD_LALT, KMOD_LCTRL, KMOD_LGUI, KMOD_LSHIFT, KMOD_MODE, KMOD_NUM, KMOD_RALT, KMOD_RCTRL, KMOD_RGUI, KMOD_RSHIFT, print_string(), and SDL_GetModState.

Referenced by PrintKey(), and PrintModifierState().

{
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 print_string ( char **  text,
size_t maxlen,
const char *  fmt,
  ... 
)
static

Definition at line 39 of file checkkeys.c.

References SDL_vsnprintf.

Referenced by print_modifiers(), and PrintKey().

{
int len;
va_list ap;
va_start(ap, fmt);
len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
if (len > 0) {
*text += len;
if ( ((size_t) len) < *maxlen ) {
*maxlen -= (size_t) len;
} else {
*maxlen = 0;
}
}
va_end(ap);
}
static void PrintKey ( SDL_Keysym sym,
SDL_bool  pressed,
SDL_bool  repeat 
)
static

Definition at line 106 of file checkkeys.c.

References print_modifiers(), print_string(), SDL_Keysym::scancode, SDL_GetKeyName, SDL_GetScancodeName, SDL_Log, and SDL_Keysym::sym.

Referenced by loop().

{
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,
sym->sym, SDL_GetKeyName(sym->sym));
} else {
print_string(&spot, &left,
"Unknown Key (scancode %d = %s) %s ",
sym->scancode,
pressed ? "pressed " : "released");
}
print_modifiers(&spot, &left);
if (repeat) {
print_string(&spot, &left, " (repeat)");
}
SDL_Log("%s\n", message);
}
static void PrintModifierState ( )
static

Definition at line 92 of file checkkeys.c.

References print_modifiers(), and SDL_Log.

Referenced by main().

{
char message[512];
char *spot;
size_t left;
spot = message;
left = sizeof(message);
print_modifiers(&spot, &left);
SDL_Log("Initial state:%s\n", message);
}
static void PrintText ( char *  eventtype,
char *  text 
)
static

Definition at line 138 of file checkkeys.c.

References SDL_Log, SDL_snprintf, and SDL_strlen.

Referenced by loop().

{
char *spot, expanded[1024];
expanded[0] = '\0';
for ( spot = text; *spot; ++spot )
{
size_t length = SDL_strlen(expanded);
SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
}
SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
}

Variable Documentation