在 Python 中查找箭头键的值:为什么它们是三元组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22397289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the Values of the Arrow Keys in Python: Why are they triples?
提问by Newb
I am trying to find the values that my local system assigns to the arrow keys, specifically in Python. I am using the following script to do this:
我试图找到我的本地系统分配给箭头键的值,特别是在 Python 中。我正在使用以下脚本来执行此操作:
import sys,tty,termios
class _Getch:
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def get():
inkey = _Getch()
while(1):
k=inkey()
if k!='':break
print 'you pressed', ord(k)
def main():
for i in range(0,25):
get()
if __name__=='__main__':
main()
Then I ran the script, and hit UP DOWN RIGHT LEFT, which gave me this output:
然后我运行脚本,并点击 UP DOWN RIGHT LEFT,这给了我这个输出:
$ python getchar.py
you pressed 27
you pressed 91
you pressed 65
you pressed 27
you pressed 91
you pressed 66
you pressed 27
you pressed 91
you pressed 67
you pressed 27
you pressed 91
you pressed 68
This is anomalous because it suggests that the arrow keys are registered as some form of triple (27-91-6x) on my system, as each press of an arrow key takes up three instances of get(). By comparison, pressing a,b,c and CTRL-C gives:
这是异常的,因为它表明箭头键在我的系统上注册为某种形式的三元组 (27-91-6x),因为每次按下箭头键都会占用三个 get() 实例。相比之下,按 a,b,c 和 CTRL-C 给出:
you pressed 97
you pressed 98
you pressed 99
you pressed 3
Can anyone explain to me why the values of my arrow-keys seem to be stored as triples? Why is this is so? Is this the same across all platforms? (I'm using Debian Linux.) If not, how should I go about storing the values of the arrow-keys?
谁能向我解释为什么我的箭头键的值似乎存储为三元组?为什么会这样?这在所有平台上都一样吗?(我使用的是 Debian Linux。)如果不是,我应该如何存储箭头键的值?
The end goal here is in that I'm trying to write a program which needs to correctly recognize arrow-keys and perform a function depending on which arrow-key was pressed.
这里的最终目标是我正在尝试编写一个程序,该程序需要正确识别箭头键并根据按下的箭头键执行功能。
采纳答案by Newb
I think I figured it out.
我想我想通了。
I learned from herethat each arrow key is represented by a unique ANSI escape code. Then I learned that the ANSI escape codes vary by system and application: in my terminal, hitting catand pressing the up-arrow gives ^[[A, in C it seems to be \033[A, etc. The latter part, the [A, remains the same, but the code for the preceding Escapecan be in hex(beginning with an x), octal (beginning with a 0), or decimal(no lead in number).
我从这里了解到每个箭头键都由一个唯一的 ANSI 转义码表示。然后我了解到 ANSI 转义码因系统和应用程序而异:在我的终端中,点击cat并按下向上箭头给出^[[A,在 C 中它似乎是\033[A等等。后一部分,[A,保持不变,但代码前面的Escape可以是十六进制(以 x 开头)、八进制(以 0 开头)或十进制(数字无前导)。
Then I opened the python console, and plugged in the triples I had previously received, trying to find their character values. As it turned out, chr(27)gave \x1b, chr(91)gave [, and calling chron 65,66,67,68returned A,B,C,Drespectively. Then it was clear: \x1bwas the escape-code!
然后我打开python控制台,插入我之前收到的三元组,试图找到它们的字符值。结果是,chr(27)give \x1b、chr(91)give[和 call chron分别65,66,67,68返回了A,B,C,D。然后很明显:\x1b是转义码!
Then I noted that an arrow key, in ANSI represented as a triple, is of course represented as three characters, so I needed to amend my code so as to read in three characters at a time. Here is the result:
然后我注意到一个箭头键,在 ANSI 中表示为三元组,当然表示为三个字符,所以我需要修改我的代码以便一次读取三个字符。结果如下:
import sys,tty,termios
class _Getch:
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(3)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def get():
inkey = _Getch()
while(1):
k=inkey()
if k!='':break
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!"
def main():
for i in range(0,20):
get()
if __name__=='__main__':
main()
回答by Mauricio Alo
I am using Mac and I used the following code and it worked well: I got the values for my arrow keys as 0,1,2,3 (Up, Down, Left, Right): Always good to remember code 27 for ESC key too. Best regards!
我正在使用 Mac 并且我使用了以下代码并且它运行良好:我将箭头键的值设置为 0,1,2,3(向上、向下、向左、向右):记住 ESC 键的代码 27 总是好的也。此致!
while True:
key = cv2.waitKey(1) & 0xFF
# if the 'ESC' key is pressed, Quit
if key == 27:
quit()
if key == 0:
print "up"
elif key == 1:
print "down"
elif key == 2:
print "left"
elif key == 3:
print "right"
# 255 is what the console returns when there is no key press...
elif key != 255:
print(key)

