Answers for "pygame get key pressed"

7

check key pressed pygame

import pygame
events = pygame.event.get()
for event in events:
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            location -= 1
        if event.key == pygame.K_RIGHT:
            location += 1
Posted by: Guest on November-26-2020
2

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
Posted by: Guest on November-13-2020
2

pygame.key.get_pressed()

if pygame.mouse.get_pressed()[0]:
	# do something when the left mouse button is pressed
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
	# do something when the a key is pressed
Posted by: Guest on January-16-2021
1

key pressed pygame

letters = {x: pygame.key.key_code(x) for x in "abcdefghijklmnopqrstuvwxyz"}

touche = pygame.key.get-pressed()

for (l, value) in letters.items() :
  if touche[value] :
    print(f"The letter {l} has been pressed ;)")
Posted by: Guest on March-28-2021
0

how to detect when a key is pressed in pygame

import pygame

keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
    x -= 1
if keys[pygame.K_RIGHT]:
	x += 1
Posted by: Guest on June-12-2021

Python Answers by Framework

Browse Popular Code Answers by Language