如何在python-curses中启用鼠标移动事件 [英] how to enable mouse movement events in python-curses

查看:296
本文介绍了如何在python-curses中启用鼠标移动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用python-curses检测鼠标移动事件.我不知道如何启用这些事件.我尝试启用所有鼠标事件,如下所示:

I want to detect mouse movement events with python-curses. I don't know how to enable these events. I tried to enable all mouse-events as follows:

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

我仅在单击,按下鼠标按钮等情况下获得鼠标事件,但没有鼠标移动事件.如何启用这些事件?

I only get mouse-events when a mouse-button is clicked, pushed-down, etc but no mouse move events. How do I enable these events?

推荐答案

我通过更改$ TERM env var/terminfo使它起作用.在Ubuntu上,只需设置TERM=screen-256color即可工作,但是在OSX上,我必须按照此处的说明编辑terminfo文件:

I got it to work by changing my $TERM env var / terminfo. On Ubuntu it worked by simply setting TERM=screen-256color, but on OSX I had to edit a terminfo file, using the instructions here:

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

but for me the format was different so I added the line:

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

为了测试它,我使用了此Python代码(请注意screen.keypad(1)是非常必要的,否则鼠标事件会导致getch返回转义键代码).

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

这篇关于如何在python-curses中启用鼠标移动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆