Python Forum
PyGame: detecting key press, but not key
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyGame: detecting key press, but not key
#1
I can't get it to detect what key is being pressed.
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 400))
while True:
    for event in pygame.event.get():
        print('e')
        if event.type == pygame.KEYDOWN:
            print('q')
            if event.type == K_w:
                print('w')
            if event.type == K_s:
                print('s')
            if event.type == K_a:
                print('a')
            if event.type == K_d:
                print('d')
it will print 'e', and if i'm pressing a key it will print 'q', but
if i'm pressing w, s, a, or d it won't print 'w', 's', 'a' or 'd'
I don't know what i'm doing wrong here
Reply
#2
Not this:
        if event.type == pygame.KEYDOWN:
            print('q')
            if event.type == K_w:
                print('w')
            if event.type == K_s:
                print('s')
            if event.type == K_a:
                print('a')
            if event.type == K_d:
                print('d')
This:
        if event.type == pygame.KEYDOWN:
            print('q')
            if event.key == K_w:
                print('w')
            if event.key  == K_s:
                print('s')
            if event.key  == K_a:
                print('a')
            if event.key  == K_d:
                print('d')
Reply
#3
thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Detecting Solar Radio Burst Using Python hermelindascott 2 303 Mar-21-2024, 07:53 PM
Last Post: deanhystad
  [PyGame] PyGame does not close when I press the close button Midnight_Sparkle3344 9 25,043 Dec-16-2022, 03:55 PM
Last Post: d3sh19th

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020