Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] IsKey... safety checks and more #3256

Merged
merged 27 commits into from
Aug 26, 2023
Merged
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/rcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -3750,6 +3750,7 @@ void OpenURL(const char *url)
// Check if a key has been pressed once
bool IsKeyPressed(int key)
{
if ((key < 0) || (key >= MAX_KEYBOARD_KEYS)) return false;
bool pressed = false;

if ((CORE.Input.Keyboard.previousKeyState[key] == 0) && (CORE.Input.Keyboard.currentKeyState[key] == 1)) pressed = true;
Expand All @@ -3760,20 +3761,23 @@ bool IsKeyPressed(int key)
// Check if a key has been pressed again (only PLATFORM_DESKTOP)
bool IsKeyPressedRepeat(int key)
{
if ((key < 0) || (key >= MAX_KEYBOARD_KEYS)) return false;
if (CORE.Input.Keyboard.keyRepeatInFrame[key] == 1) return true;
else return false;
}

// Check if a key is being pressed (key held down)
bool IsKeyDown(int key)
{
if ((key < 0) || (key >= MAX_KEYBOARD_KEYS)) return false;
if (CORE.Input.Keyboard.currentKeyState[key] == 1) return true;
else return false;
}

// Check if a key has been released once
bool IsKeyReleased(int key)
{
if ((key < 0) || (key >= MAX_KEYBOARD_KEYS)) return false;
bool released = false;

if ((CORE.Input.Keyboard.previousKeyState[key] == 1) && (CORE.Input.Keyboard.currentKeyState[key] == 0)) released = true;
Expand All @@ -3784,6 +3788,7 @@ bool IsKeyReleased(int key)
// Check if a key is NOT being pressed (key not held down)
bool IsKeyUp(int key)
{
if ((key < 0) || (key >= MAX_KEYBOARD_KEYS)) return false;
if (CORE.Input.Keyboard.currentKeyState[key] == 0) return true;
else return false;
}
Expand Down Expand Up @@ -5226,7 +5231,11 @@ void PollInputEvents(void)
// Keyboard/Mouse input polling (automatically managed by GLFW3 through callback)

// Register previous keys states
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++) CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
{
CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
}

// Register previous mouse states
for (int i = 0; i < MAX_MOUSE_BUTTONS; i++) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
Expand Down Expand Up @@ -5408,7 +5417,11 @@ void PollInputEvents(void)
#if defined(PLATFORM_ANDROID)
// Register previous keys states
// NOTE: Android supports up to 260 keys
for (int i = 0; i < 260; i++) CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
for (int i = 0; i < 260; i++)
{
CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
}

// Android ALooper_pollAll() variables
int pollResult = 0;
Expand Down Expand Up @@ -5607,6 +5620,8 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i
else if(action == GLFW_PRESS) CORE.Input.Keyboard.currentKeyState[key] = 1;
else if(action == GLFW_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1;

if (action == GLFW_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1;
nickyMcDonald marked this conversation as resolved.
Show resolved Hide resolved

#if !defined(PLATFORM_WEB)
// WARNING: Check if CAPS/NUM key modifiers are enabled and force down state for those keys
if (((key == KEY_CAPS_LOCK) && ((mods & GLFW_MOD_CAPS_LOCK) > 0)) ||
Expand Down Expand Up @@ -6075,6 +6090,8 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
}
else CORE.Input.Keyboard.currentKeyState[keycode] = 0; // Key up

if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_MULTIPLE) CORE.Input.Keyboard.keyRepeatInFrame[keycode] = 1;
raysan5 marked this conversation as resolved.
Show resolved Hide resolved

if (keycode == AKEYCODE_POWER)
{
// Let the OS handle input to avoid app stuck. Behaviour: CMD_PAUSE -> CMD_SAVE_STATE -> CMD_STOP -> CMD_CONFIG_CHANGED -> CMD_LOST_FOCUS
Expand Down Expand Up @@ -6496,7 +6513,11 @@ static void InitEvdevInput(void)
}

// Reset keyboard key state
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++) CORE.Input.Keyboard.currentKeyState[i] = 0;
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
{
CORE.Input.Keyboard.currentKeyState[i] = 0;
CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
}

// Open the linux directory of "/dev/input"
directory = opendir(DEFAULT_EVDEV_PATH);
Expand Down