import curses
screen = curses.initscr()
#curses.noecho()
curses.curs_set(0)
screen.keypad(1)
curses.mousemask(1)
screen.addstr("This is a Sample Curses Script\n\n")
while True:
event = screen.getch()
if event == ord("q"): break
if event == curses.KEY_MOUSE:
_, mx, my, _, _ = curses.getmouse()
y, x = screen.getyx()
screen.addstr(y, x, screen.instr(my, mx, 5))
curses.endwin()
You should read the docs more carefully, it's all in there :-)
Answer from Irfy on Stack OverflowVideos
It seems that I only get the mouse location when the mouse is clicked and unclicked. I want to implement a sort of drag and drop, and I need to know where the mouse is during the drag. I tried the following (where I get the mouse positoin regardless if I got a KEY_MOUSE or not, but getmouse() reports an ERR (without much explanation)
curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
while(True):
event = screen.getch()
ch = 'N'
if event == ord('q'): break
elif event == curses.KEY_MOUSE:
ch = 'Y'
_, mx, my, _, _ = curses.getmouse()
screen.addstr(my, mx, ch)I did find this asked on stack overflow 5 months ago, but nobody answered. I'm starting to fear that python/curses doesn't support this.
I know this is a pretty old question and the OP might not need it anymore, but I'm leaving it here for anyone who stumbles upon this question after hours of googling and head scratching:
import curses
def main(win:curses.window):
win.clear()
win.nodelay(True)
curses.mousemask(curses.REPORT_MOUSE_POSITION)
print('\033[?1003h') # enable mouse tracking with the XTERM API
# https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
while True:
ch=win.getch()
if ch==curses.KEY_MOUSE:
win.clear()
win.addstr(0,0,str(curses.getmouse()[1:3]))
win.refresh()
curses.wrapper(main)
The most important line here is the print('\033[?1003h'), which enables the mouse position reporting to the program, while the mousemask enables curses to interpret the input from the terminal. Note that the print must appear after the mousemask() is called.
Tested on macOS 10.14.6 with iTerm2. There is no tweak to the terminfo.
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()