我正在尝试让退格键在我拥有的 python 脚本中工作。基本上,脚本中的代码类似于:
print("Please type the character 'h':")
choice = raw_input()
if choice == 'h':
print("Thanks.")
else:
print("You input the wrong character.")
现在,显然,这是一个玩具脚本,但我很难raw_input()
做我想做的事。例如,如果我运行脚本,输入a
,然后意识到我输入了错误的字符,所以按退格键,我得到:
Please type the character 'h':
a^H
但是,如果我键入^?
,它会转换为正确的命令:
Please type the character 'h':
<<< CURSOR HERE
所以,我的问题是:我认为这与我的 linux 环境有关 - 即我认为我需要加载一个键盘映射,告诉 linux 我希望它在^?
按下退格键时输出,但我不知道为什么它似乎适用于所有其他程序(甚至是 python 解释器,令人惊讶的是 - 如果我手动将该代码放入 python 解释器,它会按预期工作!)