I developed a python script on Windows using the pygame library but am getting different results on my Ubuntu system. I am calling the pygame.event.get() function to get keyboard events and it works as expected on Windows and a Raspberry Pi-400, but I get different results when run on my Linux box, see code below. I have reduced the code and added some debugging to get the events list, event type and key. The versions of python and pygame are shown, together with the debug results in the comments below the code. Can anyone explain the differences and/or how to fix?
# keyboard_test.py
import pygame
pygame.init()
screen_width = 1200
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
caption = 'Keyboard test'
pygame.display.set_caption(caption)
clock = pygame.time.Clock()
FPS = 60
# variables
last_events_len = 0
# main loop
keep_running = True
while keep_running:
# check for events
events = pygame.event.get()
if len(events) > 0:
print('len(events) =', len(events))
for n in range(len(events)):
print('events[' + str(n) + '].type =', events[n].type)
if events[n].type == pygame.QUIT:
keep_running = False
break
if events[n].type == pygame.KEYDOWN:
print('events[' + str(n) + '].key =', events[n].key)
last_events_len = len(events)
else:
if last_events_len != 0:
print()
last_events_len = 0
# repaint screen
pygame.display.flip()
clock.tick(FPS)
# quitting
pygame.quit()
on_windows=''' pygame 1.9.3 (python 3.6.2)
when I press the 'w' key, I get:
len(events) = 1
events[0].type = 2
events[0].key = 119
when I release the key, I get:
len(events) = 1
events[0].type = 3
'''
on_raspberry_pi400=''' pygame 1.9.6 (python 3.9.2)
when I press the 'w' key, I get:
len(events) = 1
events[0].type = 2
events[0].key = 119
when I release the key, I get:
len(events) = 1
events[0].type = 3
'''
on_ubuntu=''' pygame 2.1.2 (SDL 2.0.16, Python 3.8.10)
when I press the 'w' key, I get:
len(events) = 2
events[0].type = 768
events[0].key = 119
events[1].type = 771
when I release the key, I get:
len(events) = 1
events[0].type = 769
'''