什么是其他“代码"?在Python中的左右键? [英] What are the other "codes" for the left and right keys in python?

查看:95
本文介绍了什么是其他“代码"?在Python中的左右键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

while True:
    key = ord(getch())  

    if key == 72:
        print "up"
        time.sleep(1)

    elif key == 80:
        print "down."
        time.sleep(1)

我正在搜索相同类型的箭头键代码.

I am searching for same kind of codes for arrow keys.

LEFT和RIGHT的代码是什么?

What are the codes for LEFT and RIGHT?

推荐答案

似乎您正在呼叫 _ getch 函数,该函数在Windows平台上的msvcrt模块中提供.

It seems like you are calling the _getch function which is provided in the msvcrt module on Windows platforms.

请注意,箭头键是作为两个值提供的,也就是说,您的UP和DOWN值是错误的(仅72个是"H",而80个是"P").

Note that the arrow keys are delivered as two values, that is, your values for UP and DOWN are wrong (alone, 72 is 'H' and 80 is 'P').

通过对上述文档进行实验性补充,看来答案可能是:

By complementing the documentation above with experimentation it seems like the answer may be:

  • UP为0或224,后跟72.
  • DOWN为0或224,后跟80.
  • LEFT为0或224,后跟75.
  • RIGHT为0或224,后跟77.

第一次调用_getch,如果它是0或224,则再次调用它以获得箭头键的实际键代码.

First call _getch once, and if it's either 0 or 224, call it again to get the actual key codes for the arrow keys.

这篇关于什么是其他“代码"?在Python中的左右键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆