Pygame event.unicode not handling ^ (caret) correctly [duplicate] - python

I want to change the text in the controls screen based on the key which the user presses.
how do I convert pygame.event.get() into a string that shows which key has been pressed?
preferably without many if staments
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
slectedKey = # get key name as a string
print(slectedKey)

The code of the pressed can be get by the event.key attribute.
The unicode representation for the key can be get by the event.unicode attribute.
See pygame.event module.
A unser friendly name of a key can be get by pygame.key.name():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))
Note, if you want to evaluate if a certain key is pressed, the compare event.key to the constants defined in the pygame.key module:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
# [...]
elif event.key == pygame.K_RIGHT:
# [...]
Or store the key in a variable and use it continuously in the application loop:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
slectedKey = event.key
if slectedKey == pygame.K_UP:
# [...]
elif slectedKey == pygame.K_DOWN:
# [...]
If you want to evaluate if a key is is hold down, the use pygame.key.get_pressed():
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
# [...]
elif keys[pygame.K_a]:
# [...]

Related

How do I check for two keyboard keys being pressed at the same time with pygame?

I'm trying to put some features into my pygame games that get executed when two keys (e.g. a + ESC) get pressed at the same time. I tried using
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_a and event.key == pygame.K_ESCAPE:
# do something
But it doesn't recognize when I hit both keys at the same time
Use pygame.key.get_pressed() to get the state of all keyboard buttons.
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and keys[pygame.K_ESCAPE]:
# [...]
Check the state of the keys when the KEYDOWN event occurs on one of the keys (a or ESC):
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_ESCAPE: # <--- or
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and keys[pygame.K_ESCAPE]:
print("a and ESC")
The same combined in a single condition:
event_list = pygame.event.get()
keys = pygame.key.get_pressed()
for event in event_list:
# [...]
if event.type == pygame.KEYDOWN and \
((event.key == pygame.K_a and keys[pygame.K_ESCAPE]) or \
(event.key == pygame.K_ESCAPE and keys[pygame.K_a])):
print("a and ESC")
Note: pygame.event.get() must be called before pygame.key.get_pressed(), since the states of the keys returned by pygame.event.get() are set when the events are evaluated.

pygame not detecting keypresses

I've been trying to get pygame to detect keypresses so later on I can move a character around on the screen. I'm new to coding so I might just be missing something simple, but I can't figure out what's wrong with the code, should I switch to pygame.key.get_pressed() or something instead? thanks in advance.
running = True
while running:
screen.fill((0,0,0))
# set background
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# detecting key presses
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
print("left")
if event.type == pygame.K_RIGHT:
print("right")
if event.type == pygame.K_UP:
print("up")
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
print("stop")
pygame.display.update()
It is a matter of Indentation. You must evaluate the events in the event loop instead of the application loop.
If you want to determine if a certain key is pressed, the you've to verify if the event type is pygame.KEYDOWN (or pygame.KEYUP for button release) and if the .key attribute of the event is equal the key enumerator.
running = True
while running:
screen.fill((0,0,0))
# set background
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# INDENTATION
#-->|
# detecting key presses
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: #<-- "key" instead of "type"
print("left")
if event.key == pygame.K_RIGHT: #<-- "key" instead of "type"
print("right")
if event.key == pygame.K_UP: #<-- "key" instead of "type"
print("up")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: #<-- "key" instead of "type"
print("stop")
#-->|
# INDENTATION
However, I recommend to use pygame.key.get_pressed() instead of the KEYDOWN event.
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement
pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.
clock = pygame.time.Clock()
velocity = 5
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * velocity
playerY += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * velocity
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
You need to indent this part of your code to be in the for loop:
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
print("left")
if event.type == pygame.K_RIGHT:
print("right")
if event.type == pygame.K_UP:
print("up")
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
print("stop")
pygame.display.update()
Otherwise, the event will always only be the last event from the pygame.event.get() list.
So basically, from
running = True
while running:
screen.fill((0,0,0))
# set background
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# detecting key presses
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
print("left")
if event.type == pygame.K_RIGHT:
print("right")
if event.type == pygame.K_UP:
print("up")
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
print("stop")
pygame.display.update()
to:
running = True
while running:
screen.fill((0,0,0))
# set background
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("left")
if event.key == pygame.K_RIGHT:
print("right")
if event.key == pygame.K_UP:
print("up")
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("stop")
pygame.display.update()
(Notice the event.keys I replaced part of your event.types with.)

pygame KEYUP registers only KEYDOWNs

When I press the spacebar "up" and "down" is printed simultaneous, however when I release, nothing is happening.
while True:
pygame.display.set_mode((600,600))
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print('down')
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
print('up')
if event.type == pygame.QUIT:
pygame.quit()
exit()
You're creating a new display every single frame of your game.
You need to create the display only once, on the outside of your game loop. It should also be assigned to a variable, as you'll need it if you want to draw (blit) items to it, for example.
display = pygame.display.set_mode((600,600))
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
# ...

Python PyGame press two buttons at the same time

import pygame
finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
print "A"
if event.key == pygame.K_RIGHT:
print "B"
if event.key == pygame.K_LEFT:
print "C"
Why doesn't this let me press 2 buttons at the same time and how do I make a code that does that?
The keyboard events (e.g. pygame.KEYDOWN) occure only once when a button is pressed.
Use pygame.key.get_pressed() to continuously evaluate the states of the buttons. e.g.:
finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
print "A"
if keys[pygame.K_RIGHT]:
print "B"
if keys[pygame.K_LEFT]:
print "C"
Or if you would rather get a list:
finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
keys = pygame.key.get_pressed()
if any(keys):
kmap = {pygame.K_UP : "A", pygame.K_RIGHT : "B", pygame.K_LEFT : "C"}
sl = [kmap[key] for key in kmap if keys[key]]
print sl
Try this:
import pygame
finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
print "A"
if event.key == pygame.K_RIGHT:
print "B"
if event.key == pygame.K_LEFT:
print "C"
In your version you're iterating over pygame.event.get() and you evaluate only the last event in the for loop (apart from the quit logic), meaning you only evaluate the last keypress. Move the code into the loop and you can evaluate all the events.
If you want to detect multiple keypresses then use pygame.key.get_pressed()
finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
print("C")
if keys[pygame.K_RIGHT]:
print("B")
if keys[pygame.K_UP]:
print("A")

PyGame wont register keydowns

I have installed pygame on my macbook and it seems to be working however it doesnt seem to be registering specific keys
For example
RunGame = True
while RunGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
RunGame = False
if event.type == pygame.K_LEFT:
Direction = 'West'
The quit code runs just fine however the left key is not registered. When i print the event the only event that is printed when i press any key is the key up and key down event so i cant do anything with specific keys. Am i using pygame wrong?
RunGame = True
while RunGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
RunGame = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
Direction = 'West'
You forgot the event.type and remember it is event.key == pygame.K_LEFT:. you had event.type == pygame.K_LEFT:

Categories

Resources