键盘事件及类型
键盘按下后会产生三个属性
unicode 与操作系统平台有关,一般不使用
键盘常量名称如下
鼠标事件及属性
事件打印
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame事件处理")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.unicode == "":
print("[KEYDOWN]:", "#", event.key, event.mod)
else:
print("[KEYDOWN]:", event.unicode, event.key, event.mod)
elif event.type == pygame.MOUSEMOTION:
print("[MOUSEMOTION]:", event.pos, event.rel, event.buttons)
elif event.type == pygame.MOUSEBUTTONUP:
print("[MOUSEBUTTONUP]:", event.pos, event.button)
elif event.type == pygame.MOUSEBUTTONDOWN:
print("[MOUSEBUTTONDOWN]:", event.pos, event.button)
pygame.display.update()
事件处理函数
事件队列只能存128个事件,满了多的丢弃,
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame事件处理")
fps = 1
fclock = pygame.time.Clock()
num = 1
while True:
uevent = pygame.event.Event(pygame.KEYDOWN, {"unicode": 123, "key": pygame.K_SPACE, "mod": pygame.KMOD_ALT})
pygame.event.post(uevent)
num = num + 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.unicode == "":
print("[KEYDOWN {}]:".format(num), "#", event.key, event.mod)
else:
print("[KEYDOWN {}]:".format(num), event.unicode, event.key, event.mod)
pygame.display.update()
fclock.tick(fps)