Skip to content

Commit 49a1891

Browse files
committed
Add IsKeyPressedRepeat (desktop only)
Since the key pressed are handle by comparing current vs previous state (ie frame), a special way is needed to handle key repeats.
1 parent a86c93e commit 49a1891

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/raylib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ RLAPI unsigned char *DecodeDataBase64(const unsigned char *data, int *outputSize
11151115

11161116
// Input-related functions: keyboard
11171117
RLAPI bool IsKeyPressed(int key); // Check if a key has been pressed once
1118+
RLAPI bool IsKeyPressedRepeat(int key); // Check if a key has been pressed again (Only PLATFORM_DESKTOP)
11181119
RLAPI bool IsKeyDown(int key); // Check if a key is being pressed
11191120
RLAPI bool IsKeyReleased(int key); // Check if a key has been released once
11201121
RLAPI bool IsKeyUp(int key); // Check if a key is NOT being pressed

src/rcore.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ typedef struct CoreData {
438438
int exitKey; // Default exit key
439439
char currentKeyState[MAX_KEYBOARD_KEYS]; // Registers current frame key state
440440
char previousKeyState[MAX_KEYBOARD_KEYS]; // Registers previous frame key state
441+
// NOTE: Since key press logic involves comparing prev vs cur key state, we need to handle key repeats specially
442+
char keyRepeatInFrame[MAX_KEYBOARD_KEYS]; // Registers key repeats for current frame.
441443

442444
int keyPressedQueue[MAX_KEY_PRESSED_QUEUE]; // Input keys queue
443445
int keyPressedQueueCount; // Input keys queue count
@@ -3755,6 +3757,12 @@ bool IsKeyPressed(int key)
37553757
return pressed;
37563758
}
37573759

3760+
// Check if a key has been pressed again (only PLATFORM_DESKTOP)
3761+
bool IsKeyPressedRepeat(int key)
3762+
{
3763+
return CORE.Input.Keyboard.keyRepeatInFrame[key] == 1;
3764+
}
3765+
37583766
// Check if a key is being pressed (key held down)
37593767
bool IsKeyDown(int key)
37603768
{
@@ -5170,6 +5178,8 @@ void PollInputEvents(void)
51705178
// Reset keys/chars pressed registered
51715179
CORE.Input.Keyboard.keyPressedQueueCount = 0;
51725180
CORE.Input.Keyboard.charPressedQueueCount = 0;
5181+
// Reset key repeats
5182+
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++) CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
51735183

51745184
#if !(defined(PLATFORM_RPI) || defined(PLATFORM_DRM))
51755185
// Reset last gamepad button/axis registered state
@@ -5179,7 +5189,11 @@ void PollInputEvents(void)
51795189

51805190
#if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
51815191
// Register previous keys states
5182-
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++) CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
5192+
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
5193+
{
5194+
CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
5195+
CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
5196+
}
51835197

51845198
PollKeyboardEvents();
51855199

@@ -5586,7 +5600,8 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i
55865600
// WARNING: GLFW could return GLFW_REPEAT, we need to consider it as 1
55875601
// to work properly with our implementation (IsKeyDown/IsKeyUp checks)
55885602
if (action == GLFW_RELEASE) CORE.Input.Keyboard.currentKeyState[key] = 0;
5589-
else CORE.Input.Keyboard.currentKeyState[key] = 1;
5603+
else if(action == GLFW_PRESS) CORE.Input.Keyboard.currentKeyState[key] = 1;
5604+
else if(action == GLFW_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1;
55905605

55915606
#if !defined(PLATFORM_WEB)
55925607
// WARNING: Check if CAPS/NUM key modifiers are enabled and force down state for those keys
@@ -6357,7 +6372,11 @@ static void ProcessKeyboard(void)
63576372
bufferByteCount = read(STDIN_FILENO, keysBuffer, MAX_KEYBUFFER_SIZE); // POSIX system call
63586373

63596374
// Reset pressed keys array (it will be filled below)
6360-
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++) CORE.Input.Keyboard.currentKeyState[i] = 0;
6375+
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
6376+
{
6377+
CORE.Input.Keyboard.currentKeyState[i] = 0;
6378+
CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
6379+
}
63616380

63626381
// Fill all read bytes (looking for keys)
63636382
for (int i = 0; i < bufferByteCount; i++)

0 commit comments

Comments
 (0)