诅咒模块中的标准键功能 [英] Standard keys functions in curses module
问题描述
有一个简单的程序:
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中:
How can i turn on mode that doesn't ignore standart Enter, Backspace and Arrows keys functions? or only the way is to add all special characters to elif:
if event == curses.KEY_DOWN:
#key down function
我正在尝试模式 curses.raw()
等,但没有效果...请添加示例,如果可以的话。
I'm trying modes curses.raw()
and other, but there no effect... Please add example if you can.
推荐答案
这里是一个允许你保留退格的例子(记住退格的ASCII代码是127):
Here is an example that allows you to keep backspace (keep in mind ASCII code for backspace is 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
不在[-1,127] 中的键忽略打印127(ASCII DEL)或-1(错误)。您可以为其他字符代码添加其他项目。
try / except / finally用于处理Ctrl-C。这将重置终端,以便您不会出现奇怪的提示输出ater运行。
这是一个指向官方Python文档的链接,以备将来参考:
https://docs.python.org/2.7/library/curses.html#module-curses
我希望这有帮助。
The key not in [-1, 127]
ignores printing 127 (ASCII DEL), or -1 (error). You can add other items into this, for other character codes.
The try/except/finally is for handling Ctrl-C. This resets the terminal so you don't get weird prompt output ater running.
Here is a link to the official Python docs, for future reference:
https://docs.python.org/2.7/library/curses.html#module-curses
I hope this helps.
这篇关于诅咒模块中的标准键功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!