Browse Source

Expose Scancodes for keyboard

pull/4481/head
Jeffery Myers 11 months ago
parent
commit
de1c9c44c3
3 changed files with 20 additions and 7 deletions
  1. +2
    -1
      src/platforms/rcore_desktop_glfw.c
  2. +1
    -0
      src/raylib.h
  3. +17
    -6
      src/rcore.c

+ 2
- 1
src/platforms/rcore_desktop_glfw.c View File

@ -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)) if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS))
{ {
// Add character to the queue // 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++; CORE.Input.Keyboard.keyPressedQueueCount++;
} }

+ 1
- 0
src/raylib.h View File

@ -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 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 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 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) RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC)
// Input-related functions: gamepads // Input-related functions: gamepads

+ 17
- 6
src/rcore.c View File

@ -268,6 +268,7 @@ __declspec(dllimport) unsigned int __stdcall timeEndPeriod(unsigned int uPeriod)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
typedef struct { int x; int y; } Point; typedef struct { int x; int y; } Point;
typedef struct { unsigned int width; unsigned int height; } Size; typedef struct { unsigned int width; unsigned int height; } Size;
typedef struct { int keycode; int scancode; } KeyCodes;
// Core global state context data // Core global state context data
typedef struct CoreData { 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 // 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 char keyRepeatInFrame[MAX_KEYBOARD_KEYS]; // Registers key repeats for current frame
kt">int keyPressedQueue[MAX_KEY_PRESSED_QUEUE]; // Input keys queue
n">KeyCodes keyPressedQueue[MAX_KEY_PRESSED_QUEUE]; // Input keys queue
int keyPressedQueueCount; // Input keys queue count int keyPressedQueueCount; // Input keys queue count
int charPressedQueue[MAX_CHAR_PRESSED_QUEUE]; // Input characters queue (unicode) 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) if (CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE)
{ {
// Add character to the 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++; CORE.Input.Keyboard.keyPressedQueueCount++;
} }
} }
@ -3163,28 +3165,37 @@ bool IsKeyUp(int key)
return up; return up;
} }
// Get the last key pressed
int GetKeyPressed(void)
// Get the last key and scancode pressed
int GetKeyPressedPro(int* scanCode)
{ {
int value = 0; int value = 0;
if (CORE.Input.Keyboard.keyPressedQueueCount > 0) if (CORE.Input.Keyboard.keyPressedQueueCount > 0)
{ {
// Get character from the queue head // 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 // Shift elements 1 step toward the head
for (int i = 0; i < (CORE.Input.Keyboard.keyPressedQueueCount - 1); i++) for (int i = 0; i < (CORE.Input.Keyboard.keyPressedQueueCount - 1); i++)
CORE.Input.Keyboard.keyPressedQueue[i] = CORE.Input.Keyboard.keyPressedQueue[i + 1]; CORE.Input.Keyboard.keyPressedQueue[i] = CORE.Input.Keyboard.keyPressedQueue[i + 1];
// Reset last character in the queue // 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--; CORE.Input.Keyboard.keyPressedQueueCount--;
} }
return value; return value;
} }
// Get the last key pressed
int GetKeyPressed(void)
{
return GetKeyPressedPro(NULL);
}
// Get the last char pressed // Get the last char pressed
int GetCharPressed(void) int GetCharPressed(void)
{ {

Loading…
Cancel
Save