Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygame.key.get_pressed() doesn't return the current state of keys #1619

Closed
yannbouteiller opened this issue Apr 4, 2020 · 3 comments
Closed

Comments

@yannbouteiller
Copy link

yannbouteiller commented Apr 4, 2020

Hello. I am trying to use pygame in a project where I must get the current instantaneous state of the keyboard. According to https://devdocs.io/pygame/ref/key#pygame.key.get_pressed , the get_pressed() function should do this, however it does not: it seems to return the first event frame it didn't process instead of the last one.

Showing this is this code snippet:

import pygame
import time

pygame.init()
gameDisplay = pygame.display.set_mode((100, 100))

i = 0
while True:
    events = pygame.event.get()
    keys = pygame.key.get_pressed()
    print(f"iter:{i}")
    # print(f"events:{events}")
    print(f"keys[pygame.K_a]:{keys[pygame.K_a]}")
    time.sleep(5.0)
    i = i + 1

If during, let's say, the sleep() of iteration 5, I press and relase twice the 'a' key (which by the way seems to be 'q' on my French keyboard), the following happens:

iter:5
keys[pygame.K_a]:0
iter:6
keys[pygame.K_a]:0
iter:7
keys[pygame.K_a]:1
iter:8
keys[pygame.K_a]:0
iter:9
keys[pygame.K_a]:1
iter:10
keys[pygame.K_a]:0
iter:11
keys[pygame.K_a]:0

I would expect to see only zeroes here, or just one '1' at iteration 2 if I was still pressing 'a' at the end of sleep().

Am I doing something wrong, please?

(cross-posted on stackoverflow: https://stackoverflow.com/questions/61036687/how-to-retrieve-the-instantaneous-last-state-of-the-keyboard-with-pygame)

EDIT: from stack overflow, apparently this doesn't happen for everyone. Here I am using python 3.7.3 and Ubuntu 18.04.2 LTS

EDIT2: some more info: apparently on my machine pygame.event.get() doesn't work as expected either, but returns events one by one.

With

import pygame
import time

pygame.init()
gameDisplay = pygame.display.set_mode((100, 100))

i = 0
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_a:
            print("KEYDOWN:a")
        elif event.type == pygame.KEYUP and event.key == pygame.K_a:
            print("KEYUP:a")
    keys = pygame.key.get_pressed()
    print(f"iter:{i}")
    print(f"keys[pygame.K_a]:{keys[pygame.K_a]}")
    time.sleep(5.0)
    i = i + 1

I get the following output:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
iter:0
keys[pygame.K_a]:0
iter:1
keys[pygame.K_a]:0
iter:2
keys[pygame.K_a]:0
KEYDOWN:a
iter:3
keys[pygame.K_a]:1
KEYUP:a
iter:4
keys[pygame.K_a]:0
KEYDOWN:a
iter:5
keys[pygame.K_a]:1
KEYUP:a
iter:6
keys[pygame.K_a]:0
iter:7
keys[pygame.K_a]:0
iter:8
keys[pygame.K_a]:0
@yannbouteiller
Copy link
Author

yannbouteiller commented Apr 5, 2020

More info on this issue. With the following script:

import pygame
import time

pygame.init()
gameDisplay = pygame.display.set_mode((100, 100))

i = 0
while True:
    print(f"iter:{i} ---------------------")
    print("first pygame.event.get")
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print("bye")
            exit()
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_a:
            print("KEYDOWN:a")
        elif event.type == pygame.KEYUP and event.key == pygame.K_a:
            print("KEYUP:a")
    print("second pygame.event.get")
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_a:
            print("KEYDOWN:a")
        elif event.type == pygame.KEYUP and event.key == pygame.K_a:
            print("KEYUP:a")
    print("key.get_pressed")
    keys = pygame.key.get_pressed()
    print(f"keys[pygame.K_a]:{keys[pygame.K_a]}")
    print("sleep...")
    time.sleep(5.0)
    i = i + 1

If I press 3 times 'a' during the first sleep() (iter0) and press the exit button of the window during the sleep() of iter 6 here is what happens:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
iter:0 ---------------------
first pygame.event.get
second pygame.event.get
key.get_pressed
keys[pygame.K_a]:0
sleep...
iter:1 ---------------------
first pygame.event.get
second pygame.event.get
key.get_pressed
keys[pygame.K_a]:0
sleep...
iter:2 ---------------------
first pygame.event.get
KEYDOWN:a
second pygame.event.get
key.get_pressed
keys[pygame.K_a]:1
sleep...
iter:3 ---------------------
first pygame.event.get
KEYUP:a
second pygame.event.get
key.get_pressed
keys[pygame.K_a]:0
sleep...
iter:4 ---------------------
first pygame.event.get
KEYDOWN:a
second pygame.event.get
key.get_pressed
keys[pygame.K_a]:1
sleep...
iter:5 ---------------------
first pygame.event.get
KEYUP:a
second pygame.event.get
key.get_pressed
keys[pygame.K_a]:0
sleep...
iter:6 ---------------------
first pygame.event.get
KEYDOWN:a
second pygame.event.get
key.get_pressed
keys[pygame.K_a]:1
sleep...
iter:7 ---------------------
first pygame.event.get
KEYUP:a
bye

So it seems that not only the events from my keyboard are detected one by one, but also after event.get is called once, there is a delay before a new event can be detected. However this seems to happen only with the keyboard (not the exit button)

Could that be my keyboard that buffers strokes and only sends them one by one to a 1-event-long buffer or something like that? I didn't find any similar issue with pygame on the internet.

@robertpfeiffer
Copy link
Contributor

This seems to be working correctly on the current master branch. I'll see if I can reproduce the bug with 1.9.6, and then close if I can.

@yannbouteiller
Copy link
Author

@robertpfeiffer I have installed 2.0.0.dev6 from pypy and the issue disappeared, thanks 👍

Closing then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants