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

## loop()

void loop | ( |  | ) |

Definition at line 152 of file checkkeys.c.

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

References done, PrintKey(), PrintText(), 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, and SDL_TRUE.

Referenced by main().

## main()

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

Definition at line 199 of file checkkeys.c.

{
SDL_Window *window;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
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",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480, 0);
if (!window) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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
SDL_StartTextInput();
/* Print initial modifier state */
SDL_PumpEvents();
PrintModifierState();
/* Watch keystrokes */
done = 0;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while (!done) {
loop();
}
#endif
SDL_Quit();
return (0);
}

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.

## print_modifiers()

static void print_modifiers | ( | char ** | text,
|  | size_t * | maxlen
| ) |  |

static

Definition at line 58 of file checkkeys.c.

{
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");
}

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(), SDL_GetModState, and text.

Referenced by PrintKey(), and PrintModifierState().

## print_string()

static void print_string | ( | char ** | text,
|  | size_t * | maxlen,
|  | const char * | fmt,
|  |  | ...
| ) |  |

static

Definition at line 39 of file checkkeys.c.

{
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);
}

References SDL_vsnprintf, and text.

Referenced by print_modifiers(), and PrintKey().

## PrintKey()

static void PrintKey | ( | SDL_Keysym * | sym,
|  | SDL_bool | pressed,
|  | SDL_bool | repeat
| ) |  |

static

Definition at line 106 of file checkkeys.c.

{
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_modifiers(&spot, &left);
if (repeat) {
print_string(&spot, &left, " (repeat)");
}
SDL_Log("%s\n", message);
}

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

Referenced by loop().

## PrintModifierState()

static void PrintModifierState | ( |  | ) |

static

Definition at line 92 of file checkkeys.c.

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

References print_modifiers(), and SDL_Log.

Referenced by main().

## PrintText()

static void PrintText | ( | char * | eventtype,
|  | char * | text
| ) |  |

static

Definition at line 138 of file checkkeys.c.

{
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);
}

References SDL_Log, SDL_snprintf, SDL_strlen, and text.

Referenced by loop().

## quit()

static void quit | ( | int | rc | ) |

static

Definition at line 32 of file checkkeys.c.

{
SDL_Quit();
exit(rc);
}

References SDL_Quit.

Referenced by main().

## Variable Documentation

## done

int done

Definition at line 28 of file checkkeys.c.

Referenced by CPU_haveCPUID(), loop(), main(), run_automated_tests(), SDL_LoadBMP_RW(), SDL_LoadFile_RW(), SDL_RLEAlphaBlit(), SDL_RLEBlit(), SDL_vsnprintf(), SDL_vsscanf(), SDLTest_CommonEvent(), test_multi_audio(), and WatchJoystick().

SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
PrintKey
static void PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
Definition: checkkeys.c:106
SDL_WINDOWPOS_CENTERED
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
KMOD_LALT
@ KMOD_LALT
Definition: SDL_keycode.h:332
KMOD_LGUI
@ KMOD_LGUI
Definition: SDL_keycode.h:334
message
GLuint GLsizei const GLchar * message
Definition: SDL_opengl_glext.h:2483
SDL_KEYUP
@ SDL_KEYUP
Definition: SDL_events.h:97
SDL_Keysym::scancode
SDL_Scancode scancode
Definition: SDL_keyboard.h:49
SDL_GL_CreateContext
#define SDL_GL_CreateContext
Definition: SDL_dynapi_overrides.h:559
KMOD_NUM
@ KMOD_NUM
Definition: SDL_keycode.h:336
SDL_TEXTEDITING
@ SDL_TEXTEDITING
Definition: SDL_events.h:98
SDL_KEYDOWN
@ SDL_KEYDOWN
Definition: SDL_events.h:96
SDL_CreateWindow
#define SDL_CreateWindow
Definition: SDL_dynapi_overrides.h:514
length
GLuint GLsizei GLsizei * length
Definition: SDL_opengl_glext.h:669
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDL_PumpEvents
#define SDL_PumpEvents
Definition: SDL_dynapi_overrides.h:116
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
KMOD_RALT
@ KMOD_RALT
Definition: SDL_keycode.h:333
SDL_StopTextInput
#define SDL_StopTextInput
Definition: SDL_dynapi_overrides.h:228
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
done
int done
Definition: checkkeys.c:28
loop
void loop()
Definition: checkkeys.c:152
SDL_BUTTON_LEFT
#define SDL_BUTTON_LEFT
Definition: SDL_mouse.h:282
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
PrintText
static void PrintText(char *eventtype, char *text)
Definition: checkkeys.c:138
SDL_TEXTINPUT
@ SDL_TEXTINPUT
Definition: SDL_events.h:99
SDL_QUIT
@ SDL_QUIT
Definition: SDL_events.h:60
SDL_vsnprintf
#define SDL_vsnprintf
Definition: SDL_dynapi_overrides.h:421
KMOD_LCTRL
@ KMOD_LCTRL
Definition: SDL_keycode.h:330
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
KMOD_RSHIFT
@ KMOD_RSHIFT
Definition: SDL_keycode.h:329
text
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
KMOD_RCTRL
@ KMOD_RCTRL
Definition: SDL_keycode.h:331
SDL_StartTextInput
#define SDL_StartTextInput
Definition: SDL_dynapi_overrides.h:226
SDL_GetScancodeName
#define SDL_GetScancodeName
Definition: SDL_dynapi_overrides.h:222
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:79
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
KMOD_LSHIFT
@ KMOD_LSHIFT
Definition: SDL_keycode.h:328
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
PrintModifierState
static void PrintModifierState()
Definition: checkkeys.c:92
print_string
static void print_string(char **text, size_t *maxlen, const char *fmt,...)
Definition: checkkeys.c:39
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
KMOD_RGUI
@ KMOD_RGUI
Definition: SDL_keycode.h:335
SDL_GetKeyName
#define SDL_GetKeyName
Definition: SDL_dynapi_overrides.h:224
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:40
SDL_Keysym::sym
SDL_Keycode sym
Definition: SDL_keyboard.h:50
left
GLint left
Definition: SDL_opengl_glext.h:1949
SDL_MOUSEBUTTONDOWN
@ SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:106
SDL_strlen
#define SDL_strlen
Definition: SDL_dynapi_overrides.h:393
size_t
unsigned int size_t
Definition: SDL_config_windows.h:68
SDL_Event
General event structure.
Definition: SDL_events.h:557
print_modifiers
static void print_modifiers(char **text, size_t *maxlen)
Definition: checkkeys.c:58
SDL_IsTextInputActive
#define SDL_IsTextInputActive
Definition: SDL_dynapi_overrides.h:227
quit
static void quit(int rc)
Definition: checkkeys.c:32
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
SDL_GetModState
#define SDL_GetModState
Definition: SDL_dynapi_overrides.h:218
KMOD_MODE
@ KMOD_MODE
Definition: SDL_keycode.h:338
KMOD_CAPS
@ KMOD_CAPS
Definition: SDL_keycode.h:337