From 682633d98b0121a90d94561d6472440dc06e04eb Mon Sep 17 00:00:00 2001 From: bjorn Date: Fri, 7 Aug 2020 14:15:45 -0600 Subject: [PATCH] Add scancode and key repeat arguments to key events; --- src/api/l_event.c | 8 +++++++- src/core/os.h | 2 +- src/core/os_android.c | 5 ++++- src/core/os_glfw.h | 5 +++-- src/modules/event/event.c | 6 ++++-- src/modules/event/event.h | 4 +++- 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/api/l_event.c b/src/api/l_event.c index 3795ca63..23bb0521 100644 --- a/src/api/l_event.c +++ b/src/api/l_event.c @@ -130,9 +130,15 @@ static int nextEvent(lua_State* L) { return 3; case EVENT_KEYPRESSED: + luax_pushenum(L, KeyCodes, event.data.key.code); + lua_pushinteger(L, event.data.key.scancode); + lua_pushboolean(L, event.data.key.repeat); + return 4; + case EVENT_KEYRELEASED: luax_pushenum(L, KeyCodes, event.data.key.code); - return 2; + lua_pushinteger(L, event.data.key.scancode); + return 3; #ifdef LOVR_ENABLE_THREAD case EVENT_THREAD_ERROR: diff --git a/src/core/os.h b/src/core/os.h index 35c8ca80..21ef2a63 100644 --- a/src/core/os.h +++ b/src/core/os.h @@ -54,7 +54,7 @@ typedef void (*quitCallback)(void); typedef void (*windowFocusCallback)(bool focused); typedef void (*windowResizeCallback)(int width, int height); typedef void (*mouseButtonCallback)(MouseButton button, ButtonAction action); -typedef void (*keyboardCallback)(KeyCode key, ButtonAction action); +typedef void (*keyboardCallback)(ButtonAction action, KeyCode key, uint32_t scancode, bool repeat); bool lovrPlatformInit(void); void lovrPlatformDestroy(void); diff --git a/src/core/os_android.c b/src/core/os_android.c index 4934aa0f..c6481f13 100644 --- a/src/core/os_android.c +++ b/src/core/os_android.c @@ -57,7 +57,10 @@ static int32_t onInputEvent(struct android_app* app, AInputEvent* event) { default: return 0; } - state.onKeyboardEvent(key, action); + uint32_t scancode = AKeyEvent_getScanCode(event); + bool repeat = AKeyEvent_getRepeatCount(event) > 0; + + state.onKeyboardEvent(action, key, scancode, repeat); return 1; } diff --git a/src/core/os_glfw.h b/src/core/os_glfw.h index cb59c61c..ee51360e 100644 --- a/src/core/os_glfw.h +++ b/src/core/os_glfw.h @@ -68,8 +68,9 @@ static void onKeyboardEvent(GLFWwindow* window, int k, int scancode, int a, int case GLFW_KEY_F5: key = KEY_F5; break; default: return; } - ButtonAction action = (a == GLFW_PRESS) ? BUTTON_PRESSED : BUTTON_RELEASED; - glfwState.onKeyboardEvent(key, action); + ButtonAction action = (a == GLFW_RELEASE) ? BUTTON_RELEASED : BUTTON_PRESSED; + bool repeat = (a == GLFW_REPEAT); + glfwState.onKeyboardEvent(action, key, scancode, repeat); } } diff --git a/src/modules/event/event.c b/src/modules/event/event.c index 1851c326..4969ec08 100644 --- a/src/modules/event/event.c +++ b/src/modules/event/event.c @@ -13,10 +13,12 @@ static struct { size_t head; } state; -static void onKeyboardEvent(KeyCode key, ButtonAction action) { +static void onKeyboardEvent(ButtonAction action, KeyCode key, uint32_t scancode, bool repeat) { lovrEventPush((Event) { .type = action == BUTTON_PRESSED ? EVENT_KEYPRESSED : EVENT_KEYRELEASED, - .data.key.code = key + .data.key.code = key, + .data.key.scancode = scancode, + .data.key.repeat = repeat }); } diff --git a/src/modules/event/event.h b/src/modules/event/event.h index d63117e5..33a49ac9 100644 --- a/src/modules/event/event.h +++ b/src/modules/event/event.h @@ -58,7 +58,9 @@ typedef struct { } ResizeEvent; typedef struct { - int code; + uint32_t code; + uint32_t scancode; + bool repeat; } KeyEvent; typedef struct {