Python Forum

Full Version: PyGame: detecting key press, but not key
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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')
thanks