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.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
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

pygame key button

import pygame as pg

## Basically there are 3 ways to check keyboard inputs:
## 1. If key its BEING PRESSED:
keys = pg.key.get_pressed()
if keys[pg.K_UP] or keys[ord('w')]:
	pass #do something while UP or 'w' is being pressed
    
## 2. if key was PRESSED ONCE:
while True:
	for event in pg.event.get():
    	if event.type == pg.KEYDOWN:
        	if event.key == pg.K_RIGHT:
            	pass #do something when RIGHT is pressed
                
## 3. if key was RELEASED:
while True:
	for event in pg.event.get():
    	if event.type == pg.KEYUP
        	if event.key == pg.K_w:
            	pass #do something when 'w' is released
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

PREVIOUS NEXT
Code Example
Python :: create list of dictionaries from list of list python 
Python :: python catch int conversion error 
Python :: pandas get higher value of column 
Python :: Python Generators with a Loop 
Python :: eval in python 
Python :: collections.defaultdict(set) 
Python :: how to make a new column with explode pyspark 
Python :: How to make a function repeat itself a specifc amount of times python 
Python :: keras name model 
Python :: flask add_url_rule 
Python :: python any in string 
Python :: Python NumPy insert Function Syntax 
Python :: reshape SAS matrix 
Python :: index start from 1 pandas 
Python :: allow x_frame_options django 
Python :: create hasmap in python 
Python :: wkhtmltopdf pdfkit blocked access to file 
Python :: explicitly free memory in Python code 
Python :: sqlalchemy integrityerror 
Python :: convert word to pdf python 
Python :: time zone 
Python :: Delete All Rows In Table Django 
Python :: python increment 
Python :: matplot image axis 
Python :: how to create dynamic list in python 
Python :: using shebang python 
Python :: dictionary.com 
Python :: python regex true false 
Python :: sanke in python 
Python :: explain the use of return keyword python 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =