2015-06-21 23:33:46 +08:00
|
|
|
/*
|
2024-01-02 05:15:26 +08:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-11-27 12:43:38 +08:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 12:58:20 +08:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-17 07:25:39 +08:00
|
|
|
#include <SDL3/SDL_test.h>
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2023-08-23 00:44:28 +08:00
|
|
|
static SDLTest_CommonState *state;
|
2022-07-02 03:56:47 +08:00
|
|
|
static SDLTest_TextWindow *textwin;
|
|
|
|
static int done;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
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;
|
2022-12-01 04:51:59 +08:00
|
|
|
if (((size_t)len) < *maxlen) {
|
|
|
|
*maxlen -= (size_t)len;
|
2015-06-21 23:33:46 +08:00
|
|
|
} 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;
|
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_LSHIFT) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " LSHIFT");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_RSHIFT) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " RSHIFT");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_LCTRL) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " LCTRL");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_RCTRL) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " RCTRL");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_LALT) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " LALT");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_RALT) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " RALT");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_LGUI) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " LGUI");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_RGUI) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " RGUI");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_NUM) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " NUM");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_CAPS) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " CAPS");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_MODE) {
|
2015-06-21 23:33:46 +08:00
|
|
|
print_string(text, maxlen, " MODE");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2022-12-24 09:52:46 +08:00
|
|
|
if (mod & SDL_KMOD_SCROLL) {
|
2021-08-11 08:50:17 +08:00
|
|
|
print_string(text, maxlen, " SCROLL");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-03-08 18:40:07 +08:00
|
|
|
PrintModifierState(void)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
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
|
2022-12-01 04:51:59 +08:00
|
|
|
PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
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,
|
2022-12-01 04:51:59 +08:00
|
|
|
"Key %s: scancode %d = %s, keycode 0x%08X = %s ",
|
|
|
|
pressed ? "pressed " : "released",
|
|
|
|
sym->scancode,
|
|
|
|
SDL_GetScancodeName(sym->scancode),
|
|
|
|
sym->sym, SDL_GetKeyName(sym->sym));
|
2015-06-21 23:33:46 +08:00
|
|
|
} else {
|
|
|
|
print_string(&spot, &left,
|
2022-12-01 04:51:59 +08:00
|
|
|
"Unknown Key (scancode %d = %s) %s ",
|
|
|
|
sym->scancode,
|
|
|
|
SDL_GetScancodeName(sym->scancode),
|
|
|
|
pressed ? "pressed " : "released");
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
print_modifiers(&spot, &left);
|
|
|
|
if (repeat) {
|
|
|
|
print_string(&spot, &left, " (repeat)");
|
|
|
|
}
|
|
|
|
SDL_Log("%s\n", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-11-20 06:02:02 +08:00
|
|
|
PrintText(const char *eventtype, const char *text)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
2021-11-20 06:02:02 +08:00
|
|
|
const char *spot;
|
|
|
|
char expanded[1024];
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
expanded[0] = '\0';
|
2022-12-01 04:51:59 +08:00
|
|
|
for (spot = text; *spot; ++spot) {
|
2015-06-21 23:33:46 +08:00
|
|
|
size_t length = SDL_strlen(expanded);
|
2023-03-10 07:10:00 +08:00
|
|
|
(void)SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
|
|
|
|
}
|
|
|
|
|
2023-03-08 18:40:07 +08:00
|
|
|
static void loop(void)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
2023-08-23 00:44:28 +08:00
|
|
|
int i;
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Check for events */
|
|
|
|
/*SDL_WaitEvent(&event); emscripten does not like waiting*/
|
|
|
|
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
switch (event.type) {
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_KEY_DOWN:
|
|
|
|
case SDL_EVENT_KEY_UP:
|
2023-11-04 00:27:29 +08:00
|
|
|
PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED), (event.key.repeat > 0));
|
2023-01-24 09:54:09 +08:00
|
|
|
if (event.type == SDL_EVENT_KEY_DOWN) {
|
2022-07-02 03:56:47 +08:00
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_BACKSPACE:
|
|
|
|
SDLTest_TextWindowAddText(textwin, "\b");
|
|
|
|
break;
|
|
|
|
case SDLK_RETURN:
|
|
|
|
SDLTest_TextWindowAddText(textwin, "\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
break;
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_TEXT_EDITING:
|
2022-05-03 06:40:42 +08:00
|
|
|
PrintText("EDIT", event.edit.text);
|
2015-06-21 23:33:46 +08:00
|
|
|
break;
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_TEXT_INPUT:
|
2015-06-21 23:33:46 +08:00
|
|
|
PrintText("INPUT", event.text.text);
|
2022-07-02 03:56:47 +08:00
|
|
|
SDLTest_TextWindowAddText(textwin, "%s", event.text.text);
|
2015-06-21 23:33:46 +08:00
|
|
|
break;
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_FINGER_DOWN:
|
2022-12-29 11:34:01 +08:00
|
|
|
if (SDL_TextInputActive()) {
|
2022-06-30 08:40:45 +08:00
|
|
|
SDL_Log("Stopping text input\n");
|
|
|
|
SDL_StopTextInput();
|
|
|
|
} else {
|
|
|
|
SDL_Log("Starting text input\n");
|
|
|
|
SDL_StartTextInput();
|
|
|
|
}
|
|
|
|
break;
|
2023-01-30 11:06:08 +08:00
|
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
2017-08-12 01:32:47 +08:00
|
|
|
/* Left button quits the app, other buttons toggles text input */
|
|
|
|
if (event.button.button == SDL_BUTTON_LEFT) {
|
|
|
|
done = 1;
|
|
|
|
} else {
|
2022-12-29 11:34:01 +08:00
|
|
|
if (SDL_TextInputActive()) {
|
2017-08-12 01:32:47 +08:00
|
|
|
SDL_Log("Stopping text input\n");
|
|
|
|
SDL_StopTextInput();
|
|
|
|
} else {
|
|
|
|
SDL_Log("Starting text input\n");
|
|
|
|
SDL_StartTextInput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_QUIT:
|
2015-06-21 23:33:46 +08:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-07-02 03:56:47 +08:00
|
|
|
|
2023-08-23 00:44:28 +08:00
|
|
|
for (i = 0; i < state->num_windows; i++) {
|
|
|
|
SDL_SetRenderDrawColor(state->renderers[i], 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(state->renderers[i]);
|
|
|
|
SDL_SetRenderDrawColor(state->renderers[i], 255, 255, 255, 255);
|
|
|
|
SDLTest_TextWindowDisplay(textwin, state->renderers[i]);
|
|
|
|
SDL_RenderPresent(state->renderers[i]);
|
|
|
|
}
|
2022-07-02 03:56:47 +08:00
|
|
|
|
2022-07-02 04:35:41 +08:00
|
|
|
/* Slow down framerate */
|
|
|
|
SDL_Delay(100);
|
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 23:33:46 +08:00
|
|
|
if (done) {
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
2023-08-23 00:44:28 +08:00
|
|
|
int w, h;
|
2023-03-17 07:25:39 +08:00
|
|
|
|
|
|
|
/* Initialize test framework */
|
2023-08-23 00:44:28 +08:00
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!state) {
|
2023-03-17 07:25:39 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-08-23 00:44:28 +08:00
|
|
|
state->window_title = "CheckKeys Test";
|
2023-03-17 07:25:39 +08:00
|
|
|
|
2015-11-26 04:39:28 +08:00
|
|
|
/* Enable standard application logging */
|
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2023-03-17 07:25:39 +08:00
|
|
|
/* Parse commandline */
|
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-06-30 08:40:45 +08:00
|
|
|
/* Disable mouse emulation */
|
|
|
|
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
|
|
|
|
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Initialize SDL */
|
2023-08-23 00:44:28 +08:00
|
|
|
if (!SDLTest_CommonInit(state)) {
|
2015-11-26 04:39:28 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
2022-11-28 00:38:43 +08:00
|
|
|
return 1;
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
2023-08-23 00:44:28 +08:00
|
|
|
SDL_GetWindowSize(state->windows[0], &w, &h);
|
|
|
|
textwin = SDLTest_TextWindowCreate(0.f, 0.f, (float)w, (float)h);
|
2021-08-13 21:25:35 +08:00
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_IOS
|
2023-08-23 00:44:28 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
/* Creating the context creates the view, which we need to show keyboard */
|
|
|
|
for (i = 0; i < state->num_windows; i++) {
|
|
|
|
SDL_GL_CreateContext(state->windows[i]);
|
|
|
|
}
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_StartTextInput();
|
|
|
|
|
|
|
|
/* Print initial modifier state */
|
|
|
|
SDL_PumpEvents();
|
|
|
|
PrintModifierState();
|
|
|
|
|
|
|
|
/* Watch keystrokes */
|
|
|
|
done = 0;
|
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 23:33:46 +08:00
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
|
|
|
while (!done) {
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-08-23 00:44:28 +08:00
|
|
|
SDLTest_TextWindowDestroy(textwin);
|
|
|
|
SDLTest_CleanupTextDrawing();
|
|
|
|
SDLTest_CommonQuit(state);
|
2022-11-28 00:38:43 +08:00
|
|
|
return 0;
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|