Python Forum
PyGame: detecting key press, but not key - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: PyGame: detecting key press, but not key (/thread-13122.html)



PyGame: detecting key press, but not key - qrani - Sep-28-2018

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


RE: PyGame: detecting key press, but not key - Mekire - Sep-28-2018

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')



RE: PyGame: detecting key press, but not key - qrani - Sep-28-2018

thanks