gpt4 book ai didi

python - python curses中的鼠标滚轮

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:03 25 4
gpt4 key购买 nike

有谁知道是否可以在 curses 中处理鼠标滚轮移动?我想它们应该与箭头键相同。一个例子将不胜感激。

谢谢。

最佳答案

我猜它与系统和终端有关。我写了以下内容——它只是捕获鼠标状态并将它们打印到屏幕上。我能够使用 xterm 在 Mac OSX 中接收鼠标滚动事件。 (对我来说,向上滚动注册为按钮 4;向下滚动注册为按钮 2——但它在使用 native 终端应用程序时不起作用。)

如果您查看这段代码就会清楚,您必须首先使用 scr.getch(),然后测试它是否返回 curses.KEY_MOUSE。如果是这样,您可以调用 curses.getmouse() ,它返回一个包含设备 ID、坐标和状态标志的元组。

import curses
from contextlib import contextmanager

@contextmanager
def open_screen():
scr = curses.initscr()
curses.noecho()
curses.cbreak()
scr.keypad(1)
try:
yield scr
finally:
curses.nocbreak()
scr.keypad(0)
curses.echo()
curses.endwin()

curses_mouse_states = {
curses.BUTTON1_PRESSED: 'Button 1 Pressed',
curses.BUTTON1_RELEASED: 'Button 1 Released',
curses.BUTTON1_CLICKED: 'Button 1 Clicked',
curses.BUTTON1_DOUBLE_CLICKED: 'Button 1 Double-Clicked',
curses.BUTTON1_TRIPLE_CLICKED: 'Button 1 Triple-Clicked',

curses.BUTTON2_PRESSED: 'Button 2 Pressed',
curses.BUTTON2_RELEASED: 'Button 2 Released',
curses.BUTTON2_CLICKED: 'Button 2 Clicked',
curses.BUTTON2_DOUBLE_CLICKED: 'Button 2 Double-Clicked',
curses.BUTTON2_TRIPLE_CLICKED: 'Button 2 Triple-Clicked',

curses.BUTTON3_PRESSED: 'Button 3 Pressed',
curses.BUTTON3_RELEASED: 'Button 3 Released',
curses.BUTTON3_CLICKED: 'Button 3 Clicked',
curses.BUTTON3_DOUBLE_CLICKED: 'Button 3 Double-Clicked',
curses.BUTTON3_TRIPLE_CLICKED: 'Button 3 Triple-Clicked',

curses.BUTTON4_PRESSED: 'Button 4 Pressed',
curses.BUTTON4_RELEASED: 'Button 4 Released',
curses.BUTTON4_CLICKED: 'Button 4 Clicked',
curses.BUTTON4_DOUBLE_CLICKED: 'Button 4 Double-Clicked',
curses.BUTTON4_TRIPLE_CLICKED: 'Button 4 Triple-Clicked',

curses.BUTTON_SHIFT: 'Button Shift',
curses.BUTTON_CTRL: 'Button Ctrl',
curses.BUTTON_ALT: 'Button Alt'
}

with open_screen() as scr:
curses.mousemask(-1)
while True:
c = scr.getch()
if c == curses.KEY_MOUSE:
mouse_state = curses.getmouse()[4]
states = '; '.join(state_string for state, state_string
in curses_mouse_states.viewitems()
if mouse_state & state)
scr.addstr(0, 0, states)
scr.clrtoeol()
scr.refresh()
elif c == ord('q'):
break

关于python - python curses中的鼠标滚轮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11893034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com