Search code examples
cursespython-curses

Python curses getmouse returns negative number


I'm using Python's curses library to implement some mouse-clickable UI in the Linux terminal. I noticed that when the screen width is greater than 250 columns or so, if I call

_, mouse_x, mouse_y, _, mouse_event_type = curses.getmouse()

mouse_x will be a negative number (e.g., -33)

Any idea why that might be the case?


Solution

  • The prevailing xterm mouse protocol allows only values up to 224 (i.e., 256-32). When you click outside that range, you'll get interesting results.

    Some (but not all) of the terminals which support the xterm mouse protocol have been modified to support a newer xterm protocol (SGR 1006) which allows larger coordinate values. Some (again not all) applications which use the mouse protocol can use the newer protocol.

    If you have the full ncurses terminal database installed, and are using applications (including ncurses) which work with this protocol, you could use TERM=xterm-1006 to get this feature.