2

I am trying to get user keyboard input using pygame. However, the problem is that after I run my code on IDLE, the keyboard input is never read by the program, and whatever I type is shown on the shell. Same issue if I run my code on PyCharm. Any idea? Below is my code:

pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
     for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == KEYDOWN and event.key == pygame.K_w:
            print("Yup!")
     pygame.display.flip()
10
  • Why the hack are you running a pygame app in terminal?! pygame is for making games in non-terminal windows! Commented May 3, 2016 at 18:07
  • It did not work even if I ran using IDLE or pycharm... Commented May 3, 2016 at 19:54
  • 1
    Make sure the pygame window has the focus, otherwise your keyboard input will not be recognized! Commented May 3, 2016 at 19:55
  • 1
    It is focused when I run the code, but still same problem..Keyboard input is never read... Commented May 3, 2016 at 20:00
  • 1
    It doesn't work because it can't detect any key pressed on my keyboard. Commented May 3, 2016 at 20:32

2 Answers 2

1

I'm having the exact same issue, also on a Mac using Pycharm and python 3.6. I'm printing the events and only MouseMotion events are recorded, rather than KeyDown.

Edit: apparently it's a known issue: Window does not get focus on OS X with Python 3

This is my code, and it also should work:

while not crashed:
for event in pygame.event.get():
    print(event)
    if event.type == pygame.QUIT:
        crashed = True

    # get current list
    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        print("UP")

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_1:
            print('this DOES work! :)')
        elif event.key == pygame.K_LEFT:
            x_change = -5
        elif event.key == pygame.K_RIGHT:
            x_change = 5
        elif pressed[pygame.K_UP]:
            print("UP")
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            x_change = 0

    pygame.display.flip()

x += x_change

gameDisplay.fill(black)
ship(x, y)

pygame.display.update()
clock.tick(60)
Sign up to request clarification or add additional context in comments.

Comments

0

This code works perfectly for me

import pygame

pygame.init()
windowSize = width,height = 800,600
screen = pygame.display.set_mode(windowSize)


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

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                print("Yup!")



    pygame.display.flip()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.