获取 Pygame 键盘输入并检查它是否是数字 [英] Getting Pygame keyboard input and check if it's a number
问题描述
我正在尝试使用 Pygame 使用以下命令获取键盘输入:
I am trying to get keyboard input using Pygame using this command:
if event.type == pg.KEYDOWN:
# ADD KEYBOARD EVENTS
keys = pg.key.get_pressed()
我想检查按下的按钮是否代表数字,我已经知道如何使用 try/except
命令检查字符串是否代表数字,但是,在我的代码中 keys
不是字符串,它是一个巨大的元组 - 我不知道如何以有效的方式获取它,因为每次我在互联网上查看如何获取键盘输入时,他们都需要等同于 键
到诸如 pygame.pygame.K_LEFT
之类的东西,我不想为每个数字以及数字键盘(右侧)中的每个数字都这样做.
I want to check if the button pressed represents a number, I already know how to check if a string represent a number using try/except
command, but, in my code keys
is not a string, it is a huge tuple - and I don't know how to get it in an efficient way because every time I look in the internet on how to get keyboard input, they need to equate keys
to something like pygame.pygame.K_LEFT
and I don't want to do this for each number and furthermore every number in the number-pad (right side).
有没有一种有效的方法来确定用户是否点击了一个数字?谢谢!
Is there an efficient way to determine if a user clicked on a number? Thanks!
推荐答案
pygame.key.get_pressed()
返回一个包含所有键盘按钮状态的列表.这不是为了获取键盘事件的键.按下的键可以从 key 属性中获取rel="nofollow noreferrer">pygame.event.Event
对象:
pygame.key.get_pressed()
returns a list with the state of all keyboard buttons. This is not intended to get the key of a keyboard event. The key that was pressed can be obtained from the key
attribute of the pygame.event.Event
object:
if event.type == pg.KEYDOWN:
if event.key == pg.K_a:
# [...]
unicode
包含一个作为完全翻译字符的字符串:
unicode
contains a single character string that is the fully translated character:
if event.type == pg.KEYDOWN:
if event.unicode == 'a':
# [...]
另见 pygame.key.
这篇关于获取 Pygame 键盘输入并检查它是否是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!