Skip to content

Commit 53af952

Browse files
committed
Fix erronious ButtonDown mouse event reporting.
After v0.38.0, I was seeing Input fields lose focus whenever the mouse left the input field area (but without having clicked anywhere). This was being caused because since the release Mouse ButtonDown events trigger loss of focus. But I wasn't pressing any mouse buttons. The erronious ButtonDown events were actually MouseMove events with no keys held down. According to the docs for xterm escape sequences, wher SGR (1006) mode is enabled, the encoded button value isn't always incremented by 32 for move movements events. [1] I believe the correct fix to this issue is to detect mouse movement events with no button down by checking if the "pressed button" is 0. Which appears to indicate that no mouse button is pressed (in this specific case). Whereas, the button value will be set to 1 when left click is pressed for example. I'm not sure if this change is fully correct for terminals which don't support SGR mode. [1] https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates
1 parent 3fdea3d commit 53af952

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
252252
- Fixed CSS watcher crash if file becomes unreadable (even temporarily) https://github.com/Textualize/textual/pull/4079
253253
- Fixed display of keys when used in conjunction with other keys https://github.com/Textualize/textual/pull/3050
254254
- Fixed double detection of <kbd>Escape</kbd> on Windows https://github.com/Textualize/textual/issues/4038
255+
- Fixed erroneous mouse 'ButtonDown' reporting for mouse movement when any-event mode is enabled in xterm. https://github.com/Textualize/textual/pull/3647
256+
255257

256258
## [0.47.1] - 2024-01-05
257259

src/textual/_xterm_parser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,14 @@ def parse_mouse_code(self, code: str) -> events.Event | None:
7373
)
7474
button = 0
7575
else:
76-
if buttons & 32:
76+
button = (buttons + 1) & 3
77+
# XTerm events for mouse movement can look like mouse button down events. But if there is no key pressed,
78+
# it's a mouse move event.
79+
if buttons & 32 or button == 0:
7780
event_class = events.MouseMove
7881
else:
7982
event_class = events.MouseDown if state == "M" else events.MouseUp
8083

81-
button = (buttons + 1) & 3
82-
8384
event = event_class(
8485
x,
8586
y,

tests/test_xterm_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ def test_mouse_click(parser, sequence, event_type, shift, meta):
218218
("\x1b[<35;15;38M", False, False, 0), # Basic cursor movement
219219
("\x1b[<39;15;38M", True, False, 0), # Shift held down
220220
("\x1b[<43;15;38M", False, True, 0), # Meta held down
221+
("\x1b[<3;15;38M", False, False, 0),
221222
],
222223
)
223224
def test_mouse_move(parser, sequence, shift, meta, button):

0 commit comments

Comments
 (0)