0

I'm having a problem with the KEYDOWN event. for some reason it causes repeat inputs when I'm holding down a button. here's my code:

import pygame
 

def main():
     
    
    pygame.init()
    
    pygame.display.set_caption("minimal program")
     
    
    screen = pygame.display.set_mode((240,180))
     
    
    running = True
    
    pygame.key.set_repeat()
    
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                running = False
            if event.type == pygame.KEYDOWN:
                print("pressed")


if __name__=="__main__":
    main()

I tried to use pygame.key.set_repeat() and it didn't work. I also opened a new file to check if something is wrong with my current file and nothing changed. if anyone knows the reason for this please help.

Edit: after a ton of checks and restarting I noticed that if I restart jupyter (it's where I write my code) the first run won't have this problem, but all the ones after that will, untill I restart again. I realized that for some reason the pygame.quit() function ruins the KEYDOWN and makes the error I described above (I deleted this line and everything worked fine).

If anyone has any idea why this happens please explain it to me

Gilad HR
  • 1
  • 2
  • Sorry, but the problem cannot be reproduced. Is the [indentation](https://docs.python.org/3/reference/lexical_analysis.html)correct in your original code? – Rabbid76 Jul 10 '21 at 16:19
  • It works fine for me I copied the code and held down a button and it only printed once until I stopped pressing and pressed again. Your code is all working I think – Pixeled Jul 10 '21 at 19:46
  • if jupyter is one of the issues I would add it as a tag. But there's still a potential for problems because of the keyrepeat issue. If you want only one event to be registered you need to remember if the button was previously pressed, I would use get_pressed() and only responed to the keydown if it was not pressed last time I checked and is pressed now – DCA- Jul 13 '21 at 07:16

0 Answers0