-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
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. |
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. |
@robertpfeiffer I have installed 2.0.0.dev6 from pypy and the issue disappeared, thanks 👍 Closing then. |
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:
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:
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
I get the following output:
The text was updated successfully, but these errors were encountered: