Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

pygame keys pressed

keys = pygame.key.get_pressed()
if keys[K_LEFT]:
    print("left")
Comment

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
Comment

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
Comment

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
            if keys[K_DOWN]:
                print "DOWN"
Comment

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 ;)")
Comment

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()
Comment

PREVIOUS NEXT
Code Example
Python :: list tuples and dictionary in python 
Python :: df.iterrows() 
Python :: How to check for palindromes in python 
Python :: append item to array python 
Python :: sort a stack using recursion 
Python :: python swap two values in list 
Python :: web crawler using python 
Python :: python test if string begins with python 
Python :: tqdm progress bar python 
Python :: how to export DataFrame to CSV file 
Python :: horizontal bar plot matplotlib 
Python :: python square all numbers in list 
Python :: tkinter widget span multiple colums 
Python :: python font 
Python :: python split string every character 
Python :: sort series in ascending order 
Python :: how to press enter in selenium python 
Python :: dropna in specific column pandas 
Python :: drop all characters after a character in python 
Python :: python send image in post request with json data 
Python :: pandas plot several columns 
Python :: python request response json format 
Python :: error handling in python using flask 
Python :: how to use enumerate in python 
Python :: find max length in string in pandas dataframe 
Python :: remove duplicate columns python dataframe 
Python :: batchnormalization keras 
Python :: esp8266 micropython ds18b20 
Python :: django boilerplate command 
Python :: python print variables and string 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =