Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python诅咒中启用鼠标移动事件_Python_Python 3.x_Python Curses - Fatal编程技术网

如何在python诅咒中启用鼠标移动事件

如何在python诅咒中启用鼠标移动事件,python,python-3.x,python-curses,Python,Python 3.x,Python Curses,我想用python诅咒检测鼠标移动事件。我不知道如何启用这些事件。我尝试按如下方式启用所有鼠标事件: 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 = curse

我想用python诅咒检测鼠标移动事件。我不知道如何启用这些事件。我尝试按如下方式启用所有鼠标事件:

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()

我只在点击、按下鼠标按钮等时获得鼠标事件,但没有鼠标移动事件。如何启用这些事件?

我通过更改$TERM env var/terminfo使其工作。在Ubuntu上,它只需设置
TERM=screen-256color
,但在OSX上,我必须使用以下说明编辑一个terminfo文件:

但对我来说,格式不同,所以我添加了一行:

XM=\E[?1003%?%p1%{1}%=%th%el%;,

为了测试它,我使用了这个Python代码(注意
屏幕。键盘(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()