pygame检测按下了哪个键

时间:2016-07-08 14:17:14

标签: string pygame

我正在创建我正在制作的游戏中的记分牌,我知道如何检查是否按下了某个键,但有没有办法检查按下了哪个键,创建一个变量键,然后将其添加到一个字符串? 编辑:好的,我不清楚:我知道我可以检查是否可以按下像h或t这样的按钮,但我想要一种方法只需立即按一个按钮并将其添加到字符串中,我不想为字母表中的每个字母构建一个事件函数。

5 个答案:

答案 0 :(得分:1)

if event.type == pygame.KEYDOWN :
    print(event.key)

您可以使用以下命令将给定的数字转换为 ascii:

pygame.key.name(event.key)

答案 1 :(得分:0)

如果您已注册回调以检测何时按下某个键,则UNQUESTIONABLY键的标识将包含在传递给回调服务例程的结构中。

是否提供键盘代码"以及我无法告诉你的关键的普通简ASCII值。

答案 2 :(得分:0)

如果您想要简单的textinput(例如,如果用户应输入他们的高分榜名),请查看EzText库。

一般来说,您可以通过两种方式检查按下了哪个键。

如果您使用事件队列,则可以根据pygame密钥代码检查event.key:

Thread1 - Current Counter = 0
Thread1 - Waiting to get notified after squared
Thread2 - 0 squared =>0
Thread2 - Sqared done, send notify

Thread1 - Waiter thread got notified, let's raise!
+1 => counter= 1
Thread1 - Current Counter = 1
Thread1 - Waiting to get notified after squared
Thread2 - 1 squared =>1
Thread2 - Sqared done, send notify

Thread1 - Waiter thread got notified, let's raise!
+1 => counter= 2
Thread1 - Current Counter = 2
Thread1 - Waiting to get notified after squared
Thread2 - 2 squared =>4
Thread2 - Sqared done, send notify

Thread1 - Waiter thread got notified, let's raise!
+1 => counter= 3
Thread1 - Current Counter = 3
Thread1 - Waiting to get notified after squared
Thread2 - 3 squared =>9
Thread2 - Sqared done, send notify

Thread1 - Waiter thread got notified, let's raise!
+1 => counter= 4
Thread1 - Current Counter = 4
Thread1 - Waiting to get notified after squared
Thread2 - 4 squared =>16
Thread2 - Sqared done, send notify

Thread1 - Waiter thread got notified, let's raise!
+1 => counter= 5
Thread1 end___
Thread2 end___

所有密钥代码都列在the documentation中。无论按下单个按键多长时间,都会为每个按键触发一个事件。

或者,您可以使用状态检查:

import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))
done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                print("spacebar was pressed")
            if event.key == pygame.K_q:
                done = True

pygame.quit()

请注意,如果按键被按下的时间更长,这将触发许多事件。如果在程序忙于执行除检查按键操作之外的其他操作时按下并释放按键,它也可能会错过按键。

答案 3 :(得分:0)

有点晚了,但是我想就是了

import pygame
pygame.init()

# set screen size
screen = pygame.display.set_mode((100, 100))

# set loop flag
done = False

while not done:
    # check events
    for event in pygame.event.get():
        # end if X was pressed
        if event.type == pygame.QUIT:
            done = True
        # check if event was a key being pressed down
        if event.type == pygame.KEYDOWN:
            # end if Esc was pressed
            if event.key == pygame.K_ESCAPE:
                done = True
            # unicode returns the char
            print(event.scancode, event.key, event.unicode)

# be IDLE friendly
pygame.quit()

答案 4 :(得分:0)

你需要的是:

if event.type == pygame.KEYDOWN:
    print(event.key, event.unicode)

event.key 显示被按下的字符的代码 event.unicode 显示预置的字符

结果是这样的:

event.key: 97, event.unicode: a
event.key: 98, event.unicode: b
event.key: 99, event.unicode: c
event.key: 100, event.unicode: d
event.key: 101, event.unicode: e
event.key: 102, event.unicode: f