📜  如何在 PyGame 中获得键盘输入?

📅  最后修改于: 2022-05-13 01:54:58.783000             🧑  作者: Mango

如何在 PyGame 中获得键盘输入?

在使用Python 的pygame 模块时,我们有时需要使用键盘输入进行各种操作,例如向某个方向移动字符。为了实现这一点,我们必须看到所有发生的事件。 Pygame 会跟踪发生的事件,我们可以通过 events.get()函数查看这些事件。在本文中,我们将讨论如何在 pygame 中获取和使用各种键盘输入。

检测是否按下了某个键:

每当按下或释放一个键时, pygame.event() 队列方法 pygame.KEYDOWN 和 pygame.KEYUP 事件分别。

例如,如果我们想检测一个键是否被按下,我们将跟踪 pygame.KEYDOWN 的任何事件是否发生,因此,我们将知道是否有任何键被按下。检测是否有按键按下的代码可以写成:

Python
# importing pygame module
import pygame
 
# importing sys module
import sys
 
# initialising pygame
pygame.init()
 
# creating display
display = pygame.display.set_mode((300, 300))
 
# creating a running loop
while True:
       
    # creating a loop to check events that
    # are occuring
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
         
        # checking if keydown event happened or not
        if event.type == pygame.KEYDOWN:
           
            # if keydown event happened
            # than printing a string to output
            print("A key has been pressed")


Python
# importing pygame module
import pygame
 
# importing sys module
import sys
 
# initialising pygame
pygame.init()
 
# creating display
display = pygame.display.set_mode((300, 300))
 
# creating a running loop
while True:
       
    # creating a loop to check events that
    # are occuring
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
         
        # checking if keydown event happened or not
        if event.type == pygame.KEYDOWN:
               
            # checking if key "A" was pressed
            if event.key == pygame.K_a:
                print("Key A has been pressed")
               
            # checking if key "J" was pressed
            if event.key == pygame.K_j:
                print("Key J has been pressed")
               
            # checking if key "P" was pressed
            if event.key == pygame.K_p:
                print("Key P has been pressed")
             
            # checking if key "M" was pressed
            if event.key == pygame.K_m:
                print("Key M has been pressed")





输出:

运行此代码后,可以看到每当按下一个键时,终端上都会打印一个字符串“A key has been press”

检测按下了哪个键:

要知道按下了哪个键,我们必须检查 event.key 变量对应于哪些 pygame 键。例如,字母“A”的 pygame 键是“K_a”,那么我们将 event.Key 与 K a 进行比较,如果相同,则表示按下了键“A”。

各种键盘键和相应的 pygame 键是:

pygamekeyDescription
K_BACKSPACEbackspace
K_TABtab
K_CLEARclear
K_RETURNreturn
K_PAUSEpause
K_ESCAPEescape
K_SPACEspace
K_EXCLAIMexclaim
K_HASHhash
K_QUOTEDBLquotedbl
K_DOLLARdollar
K_AMPERSANDampersand
K_QUOTEquote
K_LEFTPARENleft parenthesis
K_RIGHTPARENright parenthesis
K_ASTERISKasterisk
K_PLUSplus sign
K_COMMAcomma
K_MINUS minus sign
K_PERIOD period
K_SLASHforward slash
K_0 0
K_11
K_22
K_33
K_44
K_55
K_66
K_77
K_88
K_99
K_COLONcolon
K_SEMICOLONsemicolon
K_LESSless-than sign
K_EQUALSequals sign
K_GREATERgreater-than sign
K_QUESTION question mark
K_ATat
K_LEFTBRACKETleft bracket
K_BACKSLASH backslash
K_RIGHTBRACKET  right bracket
K_CARETcaret
K_UNDERSCOREunderscore
K_BACKQUOTEgrave
K_a,b,c…….zA to Z Alphabet
K_DELETEdelete
K_KP0, K_KP1, K_KP2….K_KP9keypad 0 to 9
K_KP_PERIODkeypad period
K_KP_DIVIDEkeypad divide
K_KP_MULTIPLYkeypad multiply
K_KP_MINUSkeypad minus
K_KP_PLUS  keypad plus
K_KP_ENTERkeypad enter
K_KP_EQUALSkeypad equals
K_UPup arrow
K_DOWNdown arrow
K_RIGHT right arrow
K_LEFT  Left arrow
K_INSERTInsert
K_HOMEHome
K_ENDEnd
K_PAGEUP Page Up
K_PAGEDOWN  Page Down
K_F1, K_F2, K_F3……K_F15F1 to F15
K_NUMLOCKNumlock
K_CAPSLOCKCapsloack
K_SCROLLOCKScrollock
K_RSHIFTRight shift
K_LSHIFTLeft shift
K_RCTRLright control
K_LCTRLLeft control
K_RALT Right alt
K_LALT Left alt
K_RMETAright meta
K_LMETA left meta
K_LSUPERleft Windows key
K_RSUPER right Windows key
K_MODEmode shift
K_HELPHelp
K_PRINTPrint Screen
K_SYSREQsysrq
K_BREAKBreak
K_MENUMenu
K_POWERPower
K_EUROEuro



例如,让我们创建一个代码来检查是否按下了键“A”或“J”或“P”或“M”。检查代码将是:

Python

# importing pygame module
import pygame
 
# importing sys module
import sys
 
# initialising pygame
pygame.init()
 
# creating display
display = pygame.display.set_mode((300, 300))
 
# creating a running loop
while True:
       
    # creating a loop to check events that
    # are occuring
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
         
        # checking if keydown event happened or not
        if event.type == pygame.KEYDOWN:
               
            # checking if key "A" was pressed
            if event.key == pygame.K_a:
                print("Key A has been pressed")
               
            # checking if key "J" was pressed
            if event.key == pygame.K_j:
                print("Key J has been pressed")
               
            # checking if key "P" was pressed
            if event.key == pygame.K_p:
                print("Key P has been pressed")
             
            # checking if key "M" was pressed
            if event.key == pygame.K_m:
                print("Key M has been pressed")

输出:

当我们运行这段代码并按下给定的键时,相应的字符串将打印在终端上。