Skip to content

Take mouse button codes from curses, not hardcoded integers - #2

Merged
leancode merged 1 commit into
masterfrom
mouse-button-constants
Sep 3, 2026
Merged

Take mouse button codes from curses, not hardcoded integers#2
leancode merged 1 commit into
masterfrom
mouse-button-constants

Conversation

@leancode

@leancode leancode commented Sep 3, 2026

Copy link
Copy Markdown
Owner

Mouse mode (F8) only responded to left click. Right click and both scroll directions did nothing.

Cause

handle_mouse compared event.mouse_code against hardcoded integers:

if event.mouse_code == 1:            # left release
elif event.mouse_code == 4096:       # right release
elif event.mouse_code == 524288:     # wheel up
elif event.mouse_code == 134217728:  # wheel down

Those are NCURSES_MOUSE_VERSION 1 values, which allocated six bits per button. Version 2 packs them into five to make room for button 5, so every constant moved:

action hardcoded actual (ncurses 6.5)
left release 1 BUTTON1_RELEASED = 1 worked
right release 4096 BUTTON3_RELEASED = 1024 dead
wheel up 524288 BUTTON4_PRESSED = 65536 dead
wheel down 134217728 BUTTON5_PRESSED = 2097152 dead

Left click worked by coincidence — BUTTON1_RELEASED is 1 under both versions.

Fix

Match against the curses constants. BUTTON5 is only defined when ncurses is built with mouse version 2, so it falls back to 0 and wheel-down is simply unavailable there rather than mismatched.

Verification

Driving the real handle_mouse with the constants this ncurses reports:

action before after
left click set_single_cursor set_single_cursor
right click unhandled add_cursor
wheel up unhandled jump_up
wheel down unhandled jump_down

An unrecognised code still returns False. test.sh passes, flake8 clean.

Same class of bug as the hardcoded 5xx keycodes fixed in 0.3.0.

handle_mouse compared against integers from NCURSES_MOUSE_VERSION 1,
which allocated six bits per button. Version 2 packs them into five to
make room for button 5, so every constant except BUTTON1_RELEASED
moved: right click is 1024 rather than 4096, wheel up 65536 rather than
524288, wheel down 2097152 rather than 134217728.

Only left click still worked, and by coincidence, because
BUTTON1_RELEASED is 1 under both versions.

BUTTON5 exists only under mouse version 2, so it degrades to no
wheel-down event rather than a mismatched one.
@leancode
leancode merged commit 7d3fe75 into master Sep 3, 2026
4 checks passed
@leancode
leancode deleted the mouse-button-constants branch September 3, 2026 22:02
@leancode leancode mentioned this pull request Sep 3, 2026
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.

1 participant