From de1c9c44c3239c3a10452fe9df3481884da84f8a Mon Sep 17 00:00:00 2001 From: Jeffery Myers Date: Sun, 10 Nov 2024 16:03:06 -0800 Subject: [PATCH] Expose Scancodes for keyboard --- src/platforms/rcore_desktop_glfw.c | 3 ++- src/raylib.h | 1 + src/rcore.c | 23 +++++++++++++++++------ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/platforms/rcore_desktop_glfw.c b/src/platforms/rcore_desktop_glfw.c index e765debc1..b909763c9 100644 --- a/src/platforms/rcore_desktop_glfw.c +++ b/src/platforms/rcore_desktop_glfw.c @@ -1806,7 +1806,8 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS)) { // Add character to the queue - CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key; + CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount].keycode = key; + CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount].scancode = scancode; CORE.Input.Keyboard.keyPressedQueueCount++; } diff --git a/src/raylib.h b/src/raylib.h index 02b3ee731..28e261329 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1177,6 +1177,7 @@ RLAPI bool IsKeyReleased(int key); // Check if a key RLAPI bool IsKeyUp(int key); // Check if a key is NOT being pressed RLAPI int GetKeyPressed(void); // Get key pressed (keycode), call it multiple times for keys queued, returns 0 when the queue is empty RLAPI int GetCharPressed(void); // Get char pressed (unicode), call it multiple times for chars queued, returns 0 when the queue is empty +RLAPI int GetKeyPressedPro(int* scanCode); // Get key pressed (keycode), and optional scancode, call it multiple times for keys queued, returns 0 when the queue is empty RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC) // Input-related functions: gamepads diff --git a/src/rcore.c b/src/rcore.c index 2d1808c14..392582611 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -268,6 +268,7 @@ __declspec(dllimport) unsigned int __stdcall timeEndPeriod(unsigned int uPeriod) //---------------------------------------------------------------------------------- typedef struct { int x; int y; } Point; typedef struct { unsigned int width; unsigned int height; } Size; +typedef struct { int keycode; int scancode; } KeyCodes; // Core global state context data typedef struct CoreData { @@ -310,7 +311,7 @@ typedef struct CoreData { // NOTE: Since key press logic involves comparing prev vs cur key state, we need to handle key repeats specially char keyRepeatInFrame[MAX_KEYBOARD_KEYS]; // Registers key repeats for current frame - int keyPressedQueue[MAX_KEY_PRESSED_QUEUE]; // Input keys queue + KeyCodes keyPressedQueue[MAX_KEY_PRESSED_QUEUE]; // Input keys queue int keyPressedQueueCount; // Input keys queue count int charPressedQueue[MAX_CHAR_PRESSED_QUEUE]; // Input characters queue (unicode) @@ -3035,7 +3036,8 @@ void PlayAutomationEvent(AutomationEvent event) if (CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) { // Add character to the queue - CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = event.params[0]; + CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount].keycode = event.params[0]; + CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount].scancode = -1; CORE.Input.Keyboard.keyPressedQueueCount++; } } @@ -3163,28 +3165,37 @@ bool IsKeyUp(int key) return up; } -// Get the last key pressed -int GetKeyPressed(void) +// Get the last key and scancode pressed +int GetKeyPressedPro(int* scanCode) { int value = 0; if (CORE.Input.Keyboard.keyPressedQueueCount > 0) { // Get character from the queue head - value = CORE.Input.Keyboard.keyPressedQueue[0]; + value = CORE.Input.Keyboard.keyPressedQueue[0].keycode; + if (scanCode != NULL) + *scanCode = CORE.Input.Keyboard.keyPressedQueue[0].scancode; // Shift elements 1 step toward the head for (int i = 0; i < (CORE.Input.Keyboard.keyPressedQueueCount - 1); i++) CORE.Input.Keyboard.keyPressedQueue[i] = CORE.Input.Keyboard.keyPressedQueue[i + 1]; // Reset last character in the queue - CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount - 1] = 0; + CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount - 1].keycode = 0; + CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount - 1].scancode = -1; CORE.Input.Keyboard.keyPressedQueueCount--; } return value; } +// Get the last key pressed +int GetKeyPressed(void) +{ + return GetKeyPressedPro(NULL); +} + // Get the last char pressed int GetCharPressed(void) {