python三个箭头什么意思_在Python中查找箭头键的值:为什么它们三重?

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:

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:

$ 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:

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?

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.

解决方案

I think I figured it out.

I learned from here that 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 cat and 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 Escape can be in hex(beginning with an x), octal (beginning with a 0), or decimal(no lead in number).

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 chr on 65,66,67,68 returned A,B,C,D respectively. Then it was clear: \x1b was the escape-code!

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:

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值