解释 stdscr 中的 "ENTER" 按键(Python 中的 curses 模块)
Interpreting "ENTER" keypress in stdscr (curses module in Python)
我正在使用 Python 的 curses 模块。
在 stdscr 中,每当我按下回车键时,诅咒就会移动到同一行的第一列。我有几个问题。
这是什么原因?
有没有办法把诅咒移到下一行?
如果我想在按下回车键时做某些事情(执行某些功能或其他事情),那么在 'if' 条件下会出现什么?例如
if (condition which will determine if ENTER was pressed or not)
# somecode
- What is the reason for that?
您需要调用 curses.noecho()
作为初始化的一部分。
- Is there a way to move the curse to the next line?
screen.move(y,x)
将移动到绝对位置。 screen.getyx()
会告诉您当前位置。
- If I want to do certain things (execute some function or something) on enter key press, then what will come in 'if' condition? e.g.
您认为可以调用 getch()
并将结果与 KEY_ENTER
进行比较。实际上,您需要检查更多的值。根据您的终端设置、您使用的库和月相,您可能需要检查换行符(又名 \n
、^J、ASCII 10)或回车 return(\r
, ^M, ASCII 13).
c = screen.getch()
if c == curses.KEY_ENTER or c == 10 or c == 13:
# I hit ENTER
示例程序:
import curses
# Thanks, http://www.ipsum-generator.com
ipsum = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla
quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent
mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum
lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent
per conubia nostra, per inceptos himenaeos.'''
try:
# Standard startup. Probably don't need to change this
screen = curses.initscr()
curses.cbreak()
curses.noecho()
screen.keypad(True)
# Silly program to write to the screen,
# wait for either <ENTER> or <Q>.
# On <ENTER>, mess with the screen.
# On <Q>, exit.
screen.addstr(0, 0, ipsum)
screen.move(0, 0)
screen.refresh()
i = 0
j = 0
while True:
c = screen.getch()
if c == ord('q'):
exit(0)
if c == curses.KEY_ENTER or c == 10 or c == 13:
i += 1
if i % 3 == 0:
screen.addstr(0, 0, ipsum.lower())
if i % 3 == 1:
screen.addstr(0, 0, ipsum.upper())
if i % 3 == 2:
screen.addstr(0, 0, ipsum)
screen.move(0, 0)
if c == curses.KEY_DOWN:
y, x = screen.getyx()
maxy, maxx = screen.getmaxyx()
screen.move((y+1) % maxy, x)
screen.refresh()
finally:
# Standard shutdown. Probably don't need to change this.
curses.nocbreak()
screen.keypad(0)
curses.echo()
curses.endwin()
参考:
- http://pubs.opengroup.org/onlinepubs/007908799/cursesix.html
- https://docs.python.org/2/howto/curses.html
- ncurses- KEY_ENTER is fail
我正在使用 Python 的 curses 模块。 在 stdscr 中,每当我按下回车键时,诅咒就会移动到同一行的第一列。我有几个问题。
这是什么原因?
有没有办法把诅咒移到下一行?
如果我想在按下回车键时做某些事情(执行某些功能或其他事情),那么在 'if' 条件下会出现什么?例如
if (condition which will determine if ENTER was pressed or not) # somecode
- What is the reason for that?
您需要调用 curses.noecho()
作为初始化的一部分。
- Is there a way to move the curse to the next line?
screen.move(y,x)
将移动到绝对位置。 screen.getyx()
会告诉您当前位置。
- If I want to do certain things (execute some function or something) on enter key press, then what will come in 'if' condition? e.g.
您认为可以调用 getch()
并将结果与 KEY_ENTER
进行比较。实际上,您需要检查更多的值。根据您的终端设置、您使用的库和月相,您可能需要检查换行符(又名 \n
、^J、ASCII 10)或回车 return(\r
, ^M, ASCII 13).
c = screen.getch()
if c == curses.KEY_ENTER or c == 10 or c == 13:
# I hit ENTER
示例程序:
import curses
# Thanks, http://www.ipsum-generator.com
ipsum = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla
quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent
mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum
lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent
per conubia nostra, per inceptos himenaeos.'''
try:
# Standard startup. Probably don't need to change this
screen = curses.initscr()
curses.cbreak()
curses.noecho()
screen.keypad(True)
# Silly program to write to the screen,
# wait for either <ENTER> or <Q>.
# On <ENTER>, mess with the screen.
# On <Q>, exit.
screen.addstr(0, 0, ipsum)
screen.move(0, 0)
screen.refresh()
i = 0
j = 0
while True:
c = screen.getch()
if c == ord('q'):
exit(0)
if c == curses.KEY_ENTER or c == 10 or c == 13:
i += 1
if i % 3 == 0:
screen.addstr(0, 0, ipsum.lower())
if i % 3 == 1:
screen.addstr(0, 0, ipsum.upper())
if i % 3 == 2:
screen.addstr(0, 0, ipsum)
screen.move(0, 0)
if c == curses.KEY_DOWN:
y, x = screen.getyx()
maxy, maxx = screen.getmaxyx()
screen.move((y+1) % maxy, x)
screen.refresh()
finally:
# Standard shutdown. Probably don't need to change this.
curses.nocbreak()
screen.keypad(0)
curses.echo()
curses.endwin()
参考:
- http://pubs.opengroup.org/onlinepubs/007908799/cursesix.html
- https://docs.python.org/2/howto/curses.html
- ncurses- KEY_ENTER is fail