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

keyboard input won't work #4164

Open
Chole01 opened this issue Feb 27, 2024 · 1 comment
Open

keyboard input won't work #4164

Chole01 opened this issue Feb 27, 2024 · 1 comment
Labels

Comments

@Chole01
Copy link

Chole01 commented Feb 27, 2024

Environment:

i want to control my role's movement by press "a", "d", and i used "pygame.key.get_pressed()[pygame.K_d]:", but it won't work.

  • Operating system (Windows 11)):
  • Python version (3.12.2) :
  • SDL version (2.28.3):
  • PyGame version (2.5.2):

Current behavior:

it seems only receive arrow-key signal like :

"pygame.key.get_pressed()[pygame.K_RIGHT]"
"pygame.key.get_pressed()[pygame.K_LEFT]"

when i use those (K_RIGHT , K_LEFT), it dose work, but dysfunctional in "K_d" or other letters.

even if i write this:

def input():
    if pygame.key.get_pressed()[pygame.K_RIGHT]:
        print("right")
    elif pygame.key.get_pressed()[pygame.K_d]:
        print("d")

when i run this code, if i press right arrow key, it will give me "right", but when i press "d", i got nothing. even worse, after i pressed key_d, the whole input function turn into dysfunctional, which means now i press right arrow key, i also get nothing.

no error hint.

Expected behavior:

is there anyway can fix this problem?

Test code

If possible add a simple test program that shows the problem described in this report.

  import pygame
  import sys
  
  pygame.init()
  
  GameWindow = pygame.display.set_mode((1280,720))
  pygame.display.set_caption("test")
  
  def input():
      if pygame.key.get_pressed()[pygame.K_RIGHT]:
          print("right")
      elif pygame.key.get_pressed()[pygame.K_d]:
          print("d")
  while True:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              pygame.quit()
              sys.exit()
          input()
@Chole01 Chole01 added the bug label Feb 27, 2024
@arteagajosepaul
Copy link

Instead of using pygame.K_d, you should use pygame.K_d + 32 to get the equivalent ASCII value.

You can try:

import pygame
import sys

pygame.init()

GameWindow = pygame.display.set_mode((1280, 720))
pygame.display.set_caption("test")

def input():
keys = pygame.key.get_pressed()

if keys[pygame.K_RIGHT]:
    print("right")
elif keys[pygame.K_d] or keys[pygame.K_RIGHT + 32]:
    print("d")

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

input()

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

No branches or pull requests

2 participants