读取按键的Python方法? [英] Python method for reading keypress?

查看:159
本文介绍了读取按键的Python方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,我刚刚用Python制作了一个游戏和一个菜单. 问题是,使用(raw_)input()要求我在每次按键后都按下Enter键,我想这样做,以便按下箭头将立即选择下一个菜单项,或者在游戏中向下移动.此刻,它要求我喜欢键入"down",然后按Enter.我也做了很多研究,但我不希望下载大型模块(例如pygame)只是为了实现一个keyDown()方法.那么,有没有更简单的方法,而我只是找不到?

I'm new to Python, and I just made a game and a menu in Python. Question is, that using (raw_)input() requires me to press enter after every keypress, I'd like to make it so that pressing down-arrow will instantly select the next menu item, or move down in the game. At the moment, it requires me to like type "down" and then hit enter. I also did quite a lot of research, but I would prefer not to download huge modules (e.g. pygame) just to achieve a single keyDown() method. So are there any easier ways, which I just couldn't find?

修改: 刚发现msvcrt.getch()可以解决问题.它不是keyDown(),但是可以工作.但是,我也不确定如何使用它,这似乎很奇怪,在这里有什么帮助吗?这是我目前所得到的:

Just found out that msvcrt.getch() would do the trick. It's not keyDown(), but it works. However, I'm not sure how to use it either, it seems quite weird, any help here? This is what I got at the moment:

from msvcrt import getch
while True:
    key = getch()
    print(key)

但是,它一直给我所有这些无用的字节,例如,向下箭头是这样:

However, it keeps giving me all these nonsense bytes, for example, down-arrow is this:

b'\xe0'
b'P'

我不知道如何使用它们,我试图与chr()进行比较,甚至使用ord(),但实际上无法进行任何比较.我想做的基本上是这样的:

And I have no idea how to use them, I've tried to compare with chr() and even use ord() but can't really do any comparisons. What I'm trying to do is basically this:

from msvcrt import getch
while True:
    key = getch()
    if key == escape:
        break
    elif key == downarrow:
        movedown()
    elif key == 'a':
        ...

等等...有什么帮助吗?

And so on... Any help?

推荐答案

我自己测试了所有内容,从而弄清楚了这一点. 找不到关于它的任何主题,因此我将解决方案留在这里.这可能不是唯一的,甚至不是最好的解决方案,但它对我来说是可行的(在getch的限制内),总比没有好.

Figured it out by testing all the stuff by myself. Couldn't find any topics about it tho, so I'll just leave the solution here. This might not be the only or even the best solution, but it works for my purposes (within getch's limits) and is better than nothing.

注意:可以识别所有按键和实际按键的正确keyDown()仍然很重要.

Note: proper keyDown() which would recognize all the keys and actual key presses, is still valued.

解决方案::使用ord()功能首先将getch()转换为整数(我想它们是虚拟键码,但不太确定),然后可以比较结果为代表所需键的实际数字.另外,如果需要,我可以在返回的数字周围添加一个额外的chr(),以便将其转换为字符.但是,我主要使用向下箭头,esc等,因此将它们转换为字符会很愚蠢.这是最终的代码:

Solution: using ord()-function to first turn the getch() into an integer (I guess they're virtual key codes, but not too sure) works fine, and then comparing the result to the actual number representing the wanted key. Also, if I needed to, I could add an extra chr() around the number returned so that it would convert it to a character. However, I'm using mostly down arrow, esc, etc. so converting those to a character would be stupid. Here's the final code:

from msvcrt import getch
while True:
    key = ord(getch())
    if key == 27: #ESC
        break
    elif key == 13: #Enter
        select()
    elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
        key = ord(getch())
        if key == 80: #Down arrow
            moveDown()
        elif key == 72: #Up arrow
            moveUp()

如果其他人需要,您也可以轻松地从google中找到键码,或者使用python并只需按以下键即可:

Also if someone else needs to, you can easily find out the keycodes from google, or by using python and just pressing the key:

from msvcrt import getch
while True:
    print(ord(getch()))

这篇关于读取按键的Python方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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