有一个简单的程序:
import curses
import time
window = curses.initscr()
curses.cbreak()
window.nodelay(True)
while True:
key = window.getch()
if key != -1:
print key
time.sleep(0.01)
curses.endwin()
我如何打开不忽略标准输入,退格键和箭头键功能的模式?或唯一的方法是将所有特殊字符添加到elif:
if event == curses.KEY_DOWN:
#key down function
我正在尝试模式curses.raw()
和其他,但没有效果...请添加示例,如果可以。
这里是一个允许您保留退格键的示例(请注意,退格键的ASCII码是127):
import curses
import time
window = curses.initscr()
curses.cbreak()
window.nodelay(True)
# Uncomment if you don't want to see the character you enter
# curses.noecho()
while True:
key = window.getch()
try:
if key not in [-1, 127]:
print key
except KeyboardInterrupt:
curses.echo()
curses.nocbreak() # Reset the program, so the prompt isn't messed up afterwards
curses.endwin()
raise SystemExit
finally:
try:
time.sleep(0.01)
except KeyboardInterrupt:
curses.echo()
curses.nocbreak() # Reset the program, so the prompt isn't messed up afterwards
curses.endwin()
raise SystemExit
finally:
pass
key not in [-1, 127]
忽略打印127(ASCII DEL)或-1(错误)。您可以在其中添加其他项目,以获取其他字符代码。try / except / finally用于处理Ctrl-C。这将重置终端,因此您不会在运行时得到奇怪的提示输出。这是官方Python文档的链接,以供将来参考:https://docs.python.org/2.7/library/curses.html#module-curses希望对您有所帮助。
stackoverflow.com/a/58886107/9028532中的代码可让您轻松使用任何键码!https://stackoverflow.com/a/58886107/9028532