当我试图抓住按键时,它们会在终端中打印,但不会被pygame和脚本捕获。脚本执行如下:
>>>import scriptname
>>>scriptname.wa()
scriptname文件: 导入pygame 来自pygame.locals import *
def wa():
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
alive_key = True
while alive_key:
for event in pygame.event.get():
if event.type == QUIT:
alive_key = False
elif event.type == KEYDOWN and event.key == K_q:
print '\nThis is not happening\n'
screen.fill((0, 0, 0))
if pygame.mouse.get_pressed()[0]:
pygame.event.post(
pygame.event.Event(KEYDOWN, key=K_q, mod=0, unicode=u'q'))
pygame.display.update()
pygame.quit()
如果在鼠标按下时创建了事件(如代码中所示),则它们可以正常工作。
我使用的是OS X 10.8.5,python 2.7,pygame2.7 1.9.1。在具有类似配置的Windows 7中,一切都运行良好。
谢谢!
答案 0 :(得分:0)
将您的代码更改为:
elif event.type == KEYDOWN or event.type == pygame.KEYDOWN:
# for testing purpose
print event
if event.key == pygame.K_q:
print '\nThis is not happening\n
我使用OS X 10.9.5,python 2.7.7,pygame-1.9.2pre-py2.7-macosx10.7。有类似的问题,比你早些时候。我在这里找到了这个解决方案:http://content.gpwiki.org/index.php/Python:Pygame_keyboard_input
我认为这可能不是mac的错。