我在使用Curses模块检测Backspace键时遇到了困难。每当我按下Backspace键,返回的字符/字符串是'^?',但我无法通过以下代码进行检测:
if str(key) == '^?':
以下代码已经准备就绪。
import curses
def main(win):
win.nodelay(True)
key = ''
record = ''
win.clear()
win.addstr("Key:")
win.addstr('\n\nRecord:')
win.addstr(record)
while True:
try:
key = win.getkey()
if str(key) == '^?':
# Attempt at detecting the Backspace key
record = record[:-1]
elif str(key) == '\n':
# Attempt at detecting the Enter Key
record = ''
else:
record += str(key)
win.clear()
win.addstr("Key:")
win.addstr(str(key))
win.addstr('\n\nRecord:')
win.addstr(record)
if key == os.linesep:
break
except Exception as e:
# No input
pass
curses.wrapper(main)
# CTRL+C to close the program
type(key)
和repr(key)
会输出什么? - ssokoloword(key)
返回 127,我用它来检测退格键。 - Neil Graham