首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在curses中启用鼠标移动事件

如何在curses中启用鼠标移动事件
EN

Stack Overflow用户
提问于 2019-05-25 06:28:31
回答 2查看 459关注 0票数 1

如何在curses中启用鼠标移动事件?

我找到了这个Mouse movement events in NCursesXterm Control Sequencesncurses_mouse_movement,但我不明白如何在python-curses中启用鼠标移动事件。我认为它与TERM=xterm-1003有关,但我不知道如何在python-curses中设置它。

我这样做是为了启用任何鼠标事件:

代码语言:javascript
复制
curses.mousemask(curses.REPORT_MOUSE_POSITION | curses.ALL_MOUSE_EVENTS)
EN

回答 2

Stack Overflow用户

发布于 2019-11-16 12:03:20

我终于让它起作用了。在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返回转义键代码)。

代码语言:javascript
复制
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()
票数 1
EN

Stack Overflow用户

发布于 2020-11-13 02:29:20

我知道这是一个相当老的问题,OP可能不再需要它了,但我把它留给那些在几个小时的谷歌搜索和挠头之后偶然发现这个问题的人:

代码语言:javascript
复制
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)

这里最重要的一行是print('\033[?1003h'),它允许向程序报告鼠标位置,而mousemask允许curses解释来自终端的输入。请注意,print必须在调用mousemask()之后出现。

在带有iTerm2的macOS 10.14.6上进行了测试。没有对terminfo进行调整。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56300134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档