擅长:python、mysql、java
<p>当我试着做类似的事情时:</p>
<pre><code>% cat test.py
char = raw_input()
print("\nInput char is [%s]." % char)
% python a.py
^[[A
].
</code></pre>
<p>它将print语句中的“\Input char is[”部分去掉。
raw_input()似乎没有接收转义字符。终端程序正在捕获转义的击键并使用它来操纵屏幕。您必须使用较低级别的程序来捕获这些字符。
查看<a href="https://stackoverflow.com/questions/22397289/finding-the-values-of-the-arrow-keys-in-python-why-are-they-triples">Finding the Values of the Arrow Keys in Python: Why are they triples?</a>是否有关于如何获得这些击键的帮助</p>
<p>从当前的<a href="https://stackoverflow.com/a/22398481/4909923">accepted answer</a>开始:</p>
<blockquote>
<pre><code> if k=='\x1b[A':
print "up"
elif k=='\x1b[B':
print "down"
elif k=='\x1b[C':
print "right"
elif k=='\x1b[D':
print "left"
else:
print "not an arrow key!"
</code></pre>
</blockquote>