Ivan Kovtun
Ivan Kovtun

Reputation: 703

Ubuntu pygame gets event.key values

The following pygame code stopped working correctly after I upgraded from ubuntu 16.04 to ubuntu 18.04.

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        print "KEY:", event.key
        if event.key == pygame.K_ESCAPE:
            # The code here is executed when "Esc" is pressed.
        elif event.key == pygame.K_f:
            # Never happens even when "f" is pressed.

The code prints "Key: 193" when I press f instead of expected "Key: 102".

Any ideas what can be wrong?

I have several keyboard layouts installed and I am sure that I use "English" when it happens.

Upvotes: 1

Views: 181

Answers (1)

Philip DiSarro
Philip DiSarro

Reputation: 1025

If you are absolutely sure that your keyboard configurations haven't changed since the Ubuntu version change, then you can solve this issue by comparing unicode values:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        print "KEY:", event.key
        if event.key == pygame.K_ESCAPE:
            # The code here is executed when "Esc" is pressed.
        elif event.unicode == 'f':
            ...

Upvotes: 1

Related Questions