如何获得是否按下了一个键pygame [英] How to get if a key is pressed pygame

查看:37
本文介绍了如何获得是否按下了一个键pygame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查是否在 pygame 中按下了某个键.我的意思不是使用 KEYDOWNKEYUP 方法.问题是,如果我只检查这些方法,我只会得到反馈(我不知道任何其他表达方式)AS 按键被按下.我在按键和释放之间没有得到反馈.我想知道当前密钥是否已关闭.

I want to check if a key is pressed in pygame. I do not mean by using the KEYDOWN or KEYUP methods. The thing is, if I only check for those methods, I only get feedback (I don't know any other way to phrase it) AS the key is pressed. I do not get feedback in between key press and release. I want to get if the key is currently down.

推荐答案

key 的当前状态可以通过 pygame.key.get_pressed().

The current state of the keys can be get by pygame.key.get_pressed().

获取键的状态 keys = pygame.key.get_pressed() 并评估 UPDOWN 是否被 if keys[pygame.K_UP]: 分别if keys[pygame.K_DOWN]:,在主应用循环中不断:

Get the sate of the keys keys = pygame.key.get_pressed() and evaluate if the UP or DOWN is pressed by if keys[pygame.K_UP]: respectively if keys[pygame.K_DOWN]:, continuously in the main application loop:

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        # [...]
    if keys[pygame.K_DOWN]:
        # [...]

注意,pygame.key.get_pressed() 返回的状态会在 pygame.event.pump()pygame.event.get().

Note, the states which are returned by pygame.key.get_pressed() are updated when the events are handled by pygame.event.pump() or pygame.event.get().

这篇关于如何获得是否按下了一个键pygame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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