0

I have some code to check if "q" is pressed and then I make an instance of a class. The problem is that sometimes it recognizes a press of the button and sometimes it doesn't. For example if I press "q" 10 times, sometimes it will recognize the press 3 times, sometimes 6 times, sometimes 7 times. So how can I fix it so it will recognize the press every single time? Some people said that it's probably because I call pygame.event.get() multiple times. Because of that I have tried getting events once in the whole script, but it's still the same result.

Code:

pygame.init()

fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((1920, 1080)

while True:

  #Input
  self.events = self.pygame.event.get()
  for event in self.events:
    if event.type == self.QUIT:
      self.pygame.quit()
      self.sys.exit()

    if event.type == self.pygame.KEYDOWN:
               
      if event.key == self.pygame.K_q:
        #makes an instance
  
  pygame.display.flip()
  fpsClock.tick(60)
  • I call it just here. – Mirko Dolenc Dec 28 '22 at 12:32
  • The problem is not reproducible. Each press of a key creates a new `KEYDOWN` event. Of course the Pygame window must have the Focus – Rabbid76 Dec 28 '22 at 12:38
  • What do you mean by "the Focus"? – Mirko Dolenc Dec 28 '22 at 12:45
  • I have it fullscreen, it is active. – Mirko Dolenc Dec 28 '22 at 12:46
  • I changed the code. Is it now reproducible? – Mirko Dolenc Dec 28 '22 at 12:54
  • No. I think you have misunderstood. There is no problem with your code. Your code is correct. It is a problem with your system, not with your code. – Rabbid76 Dec 28 '22 at 14:00
  • How can I solve it? And how can I find it in the first place? – Mirko Dolenc Dec 28 '22 at 14:21
  • The problem is not reproducible, your code works fine for me, I don't know your system either, how should I know what is causing the problem? That it's not a problem with the code, it's also not an appropriate question for StackOverflow. See [What topics can I ask about here?](https://stackoverflow.com/help/on-topic). You may find help in the Discord Pygame community. – Rabbid76 Dec 28 '22 at 14:30
  • This does not appear to be the complete code. Please read [`How to create a Minimal, Reproducible Example`](https://stackoverflow.com/help/minimal-reproducible-example). Do you call `pygame.event.get()` twice or call `pygame.event.pump()` somewhere? Do you use `wait`, `sleep` or `delay`? Do you have a loop in the application loop that animates something? Do you have a loop in the application loop that animates something? Are you loading images in the application loop (e.g. in case of "makes an instance")? – Rabbid76 Dec 28 '22 at 15:03
  • No, just the main pygame loop. – Mirko Dolenc Dec 28 '22 at 15:48

0 Answers0