I finally got it to work. On Ubuntu it worked by simply setting TERM=screen-256color
, but on OSX I had to edit a terminfo file, using the instructions here:
Which $TERM to use to have both 256 colors and mouse move events in python curses?
but on my system the format was different so I added the line:
XM=\E[?1003%?%p1%{1}%=%th%el%;,
to my terminfo. To test it, I used this Python code (note screen.keypad(1)
is very necessary, otherwise mouse events cause getch
to return escape key codes).
import curses
screen = curses.initscr()
screen.keypad(1)
curses.curs_set(0)
curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
curses.flushinp()
curses.noecho()
screen.clear()
while True:
key = screen.getch()
screen.clear()
screen.addstr(0, 0, 'key: {}'.format(key))
if key == curses.KEY_MOUSE:
_, x, y, _, button = curses.getmouse()
screen.addstr(1, 0, 'x, y, button = {}, {}, {}'.format(x, y, button))
elif key == 27:
break
curses.endwin()
curses.flushinp()