Skip to content

[rcore] IsMouseButton*(), random key codes return unexpected results#5516

Merged
raysan5 merged 18 commits into
raysan5:masterfrom
jasoncnm:master
Jan 28, 2026
Merged

[rcore] IsMouseButton*(), random key codes return unexpected results#5516
raysan5 merged 18 commits into
raysan5:masterfrom
jasoncnm:master

Conversation

@jasoncnm
Copy link
Copy Markdown
Contributor

Hi, today I've encountered a weird behaviour when I accidentally wrote

IsMouseButtonDown(KEY_S);

In my raylib project, it will sometimes return true when I'm moving my cursor. So I've written a simple test code as such

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 1000;
    const int screenHeight = 1000;

    InitWindow(screenWidth, screenHeight, "raylib [shapes] example - starfield effect");

    Color bgColor = ColorLerp(DARKBLUE, BLACK, 0.69f);

    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------
    
     unsigned int press_count = 0, down_count = 0, release_count = 0, up_count = 0;
    
    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        
        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();
        
        ClearBackground(bgColor);
        
        if (IsMouseButtonPressed(KEY_S))
        {
            press_count++;
        }
        
        if (IsMouseButtonDown(KEY_S))
        {
            down_count++;
        }
        
        if (IsMouseButtonReleased(KEY_S))
        {
            release_count++;
        }
        
        DrawCircleV(GetMousePosition(), 5, RED);
        
        DrawText(TextFormat("KeyCode: %d\nPressed %d times\nDowned %d times\nReleased %d times", 
                            KEY_S, press_count, down_count, release_count), GetScreenWidth() / 2, GetScreenHeight() / 2, 20, RAYWHITE);
        
            DrawFPS(10, 10);
        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();          // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

And I got the same result, when I move around the cursor on the screen, it will increase down_count and press_count.
Here is the video of it.

2026-01-27.21-27-34.mp4

What I did to fix this was return false when the keycodes are not valid. i.e
if ((button < 0) || (button > MOUSE_BUTTON_BACK)) return false;

@raysan5
Copy link
Copy Markdown
Owner

raysan5 commented Jan 28, 2026

@jasoncnm oh, interesting issue... thanks for the review. In raylib I try to avoid/minimiz the early returns, can you refactor it to be code to be:

if ((button < 0) || (button > MOUSE_BUTTON_BACK))
{
    if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) pressed = true;
}

It will be consistent with IsKey*().

@raysan5 raysan5 changed the title Radom keycode cause IsMouseButton[Pressed/Down] as true [rcore] IsMouseButton*(), random key codes return unexpected results Jan 28, 2026
@jasoncnm
Copy link
Copy Markdown
Contributor Author

@raysan5 I think

if ((button >= 0) && (button <= MOUSE_BUTTON_BACK))
{
    if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) pressed = true;
}

Is the correct change, and that's updated. Thanks for the feedback!

@raysan5 raysan5 merged commit d0a6892 into raysan5:master Jan 28, 2026
@raysan5
Copy link
Copy Markdown
Owner

raysan5 commented Jan 28, 2026

@jasoncnm Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants