pygame keyboard events pressing down code example

Example 1: pygame key pressed once

# Outside loop
pressed = False

# Inside loop
if event.type == pygame.KEYDOWN:
	if event.key == pygame.K_a and not pressed: #K_a can be replaced by any key
		# Do something
		pressed = True
	elif event.key != pygame.K_a:
    	pressed = False

Example 2: pygame keys keep pressing

while not done:
    for e in event.get():
        if e.type == KEYDOWN:
            keys = key.get_pressed()
            if e.type == QUIT or keys[K_ESCAPE]:
                done = True
            while keys[K_DOWN]:
                print "DOWN"
                event.get()
                keys = key.get_pressed()

Example 3: keyboard events pygame

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_q:
            exit(0)
        elif event.key == pygame.K_LEFT:
            print("You pressed LEFT")
        elif event.key == pygame.K_RIGHT:
            print("You pressed RIGHT")