OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pygame key detection does not detect keys pressed [duplicate]

  • Thread starter Thread starter Connor McBride
  • Start date Start date
C

Connor McBride

Guest
I have a function called detectletters() for my Pygame text-editor test. That function was put into the main gameloop:

Code:
while running:
  detectletters()
  detectdelete()
  pygame.display.flip()
  updateletterlocations()

Inside the detectletters() function, some global variables are defined but then the program starts checking to see if each key was pressed, in if statements like this:

Code:
keys = pygame.key.get_pressed()
  if keys[pygame.K_a]:
    textbox = text.render("a", False, (colors["blue"]))
    screen.blit(textbox, (letterx, lettery))
    lastkerning.append(2)
    kerning += 2
  if keys[pygame.K_b]:
    textbox = text.render("b", False, (colors["blue"]))
    screen.blit(textbox, (letterx, lettery))
    lastkerning.append(2)
    kerning += 2

Nothing is happening when I press the a, b, etc. keys. It has nothing to do with the code inside the if statements because if I write a print statement there it doesn't work either. Print statements do work however outside the if statements in the main detectletters() function.

I tried making sure that I had the indentations, format, and spelling right of every item but as far as I can tell it is correct.

I, like stated above, tested where the issue was happening and discovered it WAS the if statements themselves. I don't know what I'm doing wrong.

The funniest part of all of this is a very similar code works perfectly fine in another project.
<p>I have a function called detectletters() for my Pygame text-editor test. That function was put into the main gameloop:</p>
<pre><code>while running:
detectletters()
detectdelete()
pygame.display.flip()
updateletterlocations()
</code></pre>
<p>Inside the detectletters() function, some global variables are defined but then the program starts checking to see if each key was pressed, in if statements like this:</p>
<pre><code>keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
textbox = text.render("a", False, (colors["blue"]))
screen.blit(textbox, (letterx, lettery))
lastkerning.append(2)
kerning += 2
if keys[pygame.K_b]:
textbox = text.render("b", False, (colors["blue"]))
screen.blit(textbox, (letterx, lettery))
lastkerning.append(2)
kerning += 2
</code></pre>
<p>Nothing is happening when I press the a, b, etc. keys. It has nothing to do with the code inside the if statements because if I write a print statement there it doesn't work either. Print statements do work however outside the if statements in the main detectletters() function.</p>
<p>I tried making sure that I had the indentations, format, and spelling right of every item but as far as I can tell it is correct.</p>
<p>I, like stated above, tested where the issue was happening and discovered it WAS the if statements themselves. I don't know what I'm doing wrong.</p>
<p>The funniest part of all of this is a very similar code works perfectly fine in another project.</p>
 

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top