我想用python-curses检测鼠标移动事件。我不知道如何启用这些事件。我尝试启用所有鼠标事件,如下所示:
stdscr = curses.initscr()
curses.mousemask(curses.REPORT_MOUSE_POSITION | curses.ALL_MOUSE_EVENTS)
while True:
c = stdscr.getch()
if c == curses.KEY_MOUSE:
id, x, y, z, bstate = curses.getmouse()
stdscr.addstr(curses.LINES-2, 0, "x: " + str(x))
stdscr.addstr(curses.LINES-1, 0, "y: " + str(y))
stdscr.refresh()
if c == ord('q'):
break
curses.endwin()我只得到鼠标事件,当鼠标按键被点击,按下等,但没有鼠标移动事件。如何启用这些事件?
发布于 2019-11-16 14:50:11
我通过更改我的$TERM环境var / terminfo使其正常工作。在Ubuntu上,只需设置TERM=screen-256color即可,但在OSX上,我必须编辑一个terminfo文件,使用下面的说明:
Which $TERM to use to have both 256 colors and mouse move events in python curses?
但对我来说,格式是不同的,所以我添加了一行:
XM=\E[?1003%?%p1%{1}%=%th%el%;,
为了测试它,我使用了这段Python代码(注意screen.keypad(1)是非常必要的,否则鼠标事件会导致getch返回转义键代码)。
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()https://stackoverflow.com/questions/56303971
复制相似问题