wijjit.terminal.mouse.MouseEventParser
- class wijjit.terminal.mouse.MouseEventParser(double_click_threshold=0.5, double_click_distance=2)[source]
Parser for ANSI mouse event sequences.
This class parses both SGR (modern) and normal (legacy) ANSI mouse event sequences and synthesizes high-level events like clicks and double-clicks.
- Parameters:
- Variables:
Methods
__init__([double_click_threshold, ...])get_sgr_match_length(data)Get the length of an SGR mouse sequence if present.
parse_normal(data)Parse normal (legacy) format mouse event sequence.
parse_sgr(data)Parse SGR format mouse event sequence.
parse_windows(data)Parse a Windows console mouse event string.
reset()Reset click synthesis state.
Attributes
- SGR_PATTERN = re.compile(b'\\x1b\\[<(\\d+);(\\d+);(\\d+)([Mm])')
- NORMAL_PREFIX = b'\x1b[M'
- reset()[source]
Reset click synthesis state.
This method clears all internal state used for click and double-click detection. Call this when starting a new session or when the mouse state should be cleared (e.g., window focus lost, view change).
Notes
This prevents stale click state from carrying over between sessions, which could cause incorrect double-click detection if a click from a previous session is still recorded.
- Return type:
None
- parse_sgr(data)[source]
Parse SGR format mouse event sequence.
SGR format is the modern, preferred format that supports extended coordinates and clear press/release distinction.
Format: ESC [ < button ; x ; y M/m Where M = press, m = release
- Parameters:
data (
bytes) – Raw byte sequence to parse- Returns:
Parsed mouse event, or None if not a valid SGR sequence
- Return type:
MouseEventorNone
- parse_normal(data)[source]
Parse normal (legacy) format mouse event sequence.
Normal format uses printable ASCII encoding and has coordinate limitations (max 223 columns/rows).
Format: ESC [ M bxy Where b, x, y are encoded as (value + 32)
- Parameters:
data (
bytes) – Raw byte sequence to parse- Returns:
Parsed mouse event, or None if not a valid normal sequence
- Return type:
MouseEventorNone
- parse_windows(data)[source]
Parse a Windows console mouse event string.
On Windows, prompt_toolkit’s Win32 input does not emit vt100 escape sequences. Instead it delivers a single
KeyPresswhosedatais a";"-delimited string of the form"<button>;<event>;<x>;<y>"(for example"LEFT;MOUSE_DOWN;13;6"). Coordinates are already 0-based, but they are relative to the console screen buffer, so the window’s origin within that buffer is subtracted here to get the window-relative cell Wijjit actually drew (seeconsole_window_origin()).- Parameters:
data (
str) – TheKeyPress.datapayload for aKeys.WindowsMouseEvent.- Returns:
Parsed (and click-synthesized) mouse event, or None if the payload is not a recognizable Windows mouse string.
- Return type:
MouseEventorNone
Notes
Windows reports no button identity on release (
MOUSE_UPcarries buttonNONE); click synthesis in_synthesize_clicks()adopts the recorded press button so single and double clicks still work. Modifier keys are not encoded in this format and are always reported as not pressed.