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:
  • double_click_threshold (float, optional) – Maximum time between clicks for double-click (default: 0.5 seconds)

  • double_click_distance (int, optional) – Maximum distance between clicks for double-click (default: 2 pixels)

Variables:
  • double_click_threshold (float) – Maximum time between clicks for double-click in seconds

  • double_click_distance (int) – Maximum Manhattan distance between clicks for double-click

__init__(double_click_threshold=0.5, double_click_distance=2)[source]
Parameters:
  • double_click_threshold (float)

  • double_click_distance (int)

Return type:

None

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

NORMAL_PREFIX

SGR_PATTERN

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:

MouseEvent or None

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:

MouseEvent or None

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 KeyPress whose data is 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 (see console_window_origin()).

Parameters:

data (str) – The KeyPress.data payload for a Keys.WindowsMouseEvent.

Returns:

Parsed (and click-synthesized) mouse event, or None if the payload is not a recognizable Windows mouse string.

Return type:

MouseEvent or None

Notes

Windows reports no button identity on release (MOUSE_UP carries button NONE); 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.

get_sgr_match_length(data)[source]

Get the length of an SGR mouse sequence if present.

This is useful for knowing how many bytes to consume from an input buffer after parsing.

Parameters:

data (bytes) – Data buffer that might contain SGR sequence

Returns:

Length of SGR sequence in bytes, or None if not found

Return type:

int or None