How do I check if left ctrl key is released in pygame? I saw the documentation, but following code didn't work for me:
if event.type == pygame.KEYUP:
if pygame.key.get_mods() & pygame.KMOD_CTRL: # this is not working!
print("Left control is released")
What am I doing wrong? Or what is the proper way of checking it?
The constant for the left control key is K_LCTRL. Therefor you have to check if the key attribute of the event object is K_LCTRL:
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYUP:
if event.key == pygame.K_LCTRL:
print(f'release {pygame.key.name(event.key)}')
I think you need to match the event key with the left control key value.
And so your final code look like:
if event.type == pygame.KEYUP:
if event.key == pygame.K_LCTRL:
print("Left control is released")
Related
I'm trying to check if keyUp is pressed by event.loop in pygame, but when i click any other button on keyboard i get the same output with print(event.type) that every key has same id as pygame.KEYUP
Here is the code:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYUP: print("key up")
KEYUP down does not address the "up" key. It is the event that occurs when a key is released. You must check that the event type is KEYDOWN (key pressed) and the key is K_UP:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
print("key up")
Also see How to get keyboard input in pygame?.
Any key that gets pressed on the keyboard generates a keydown event. Any key that gets released generates a keyup event.
If you want to know whether the 'up arrow' key got pressed, check for keydown on the K_UP key.
If you want to know whether the 'up arrow' key got released, check for keyup on the K_UP key.
If you want to know whether the 'down arrow' key got pressed, check for keydown on the K_DOWN key.
If you want to know whether the 'down arrow' key got released, check for keyup on the K_DOWN key.
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]:
# [...]
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:
keep in mind there is more code before this, and other imports linked to here, but that does not have to do with my problem.
I would like to know why my event.type==pygame.K_RIGHT command is not working.
When I run the code, it says no errors but the player can not move.
pygame.mouse.set_cursor(*pygame.cursors.diamond)
movex, movey = 0,0
while gameLoop:
for event in pygame.event.get():
if (event.type==pygame.QUIT):
gameLoop=False
if (event.type==pygame.KEYDOWN):
if (event.type==pygame.K_d):
movex=5
elif (event.type==pygame.K_a) :
movex=-5
if (event.type==pygame.KEYUP):
if (event.type==pygame.K_RIGHT):
movex=0
elif (event.type==pygame.K_LEFT) :
movex=0
window.fill (blue)
player.render(window)
player.x+=movex
clock.tick(60)
pygame.display.flip()
pygame.quit()
Once the KEYDOWN or KEYUP event has been detected, you need to check the event.key attribute rather than event.type to know which key was pressed. Something like this:
for event in pygame.event.get():
# The KEYDOWN event was posted
if event.type == pygame.KEYDOWN:
# The down arrow key was pressed
# Note the key attribute is being checked rather than type
if event.key == pygame.KEY_DOWN:
# do something
# The right arrow key was pressed
elif event.key == pygame.K_RIGHT:
# do something else
if (event.type==pygame.K_RIGHT):
movex=0
You have put movex= 0 hence the player is not moving. You need to change it. Change it to movex = 1.
I made 2 functions, which would check for an event
def get_pygame_events():
pygame_events = pygame.event.get()
return pygame_events
and
def get_keys_pressed(self):
keys_pressed = get_pygame_events() #pygame.event.get(pygame.KEYDOWN)
# print(keys_pressed)
keys_pressed_list = []
for event in keys_pressed:
if event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
keys_pressed_list.append("left")
if event.key == K_RIGHT:
keys_pressed_list.append("right")
if event.key == K_UP:
keys_pressed_list.append("up")
if event.key == K_DOWN:
keys_pressed_list.append("down")
if event.key == K_a:
keys_pressed_list.append("a")
if event.key == K_d:
keys_pressed_list.append("b")
if event.key == K_w:
keys_pressed_list.append("w")
if event.key == K_s:
keys_pressed_list.append("s")
if event.key == K_SPACE:
keys_pressed_list.append("space")
if event.key == K_q:
keys_pressed_list.append("q")
if event.key == K_e:
keys_pressed_list.append("e")
if event.type == pygame.MOUSEBUTTONDOWN:
keys_pressed_list.append("click")
return (keys_pressed_list, event.pos)
return keys_pressed_list
I expected that if I could do something similar to:
while True:
Variable1 = get_pygame_events()
Variable2 = get_keys_pressed()
if Variable2 == ["w"]:
print("w")
(That while loop was just a summary of what I did)
Then if I held down W, "w" would be printed over and over and over again. Instead when I tried, it printed W once, and unless I pressed again, that is all that would happen.
How can I make it so by holding down the W (or any) key, it identifies the event happening, and (in this case) prints "w" every time it goes through the while loop?
Use pygame.KEYDOWN and pygame.KEYUP to detect if a key is physically pressed down or released. You can activate keyboard repeat by using pygame.key.set_repeat to generate multiple pygame.KEYDOWN events when a key is held down, but that's rarely a good idea.
Instead, you can use pygame.key.get_pressed() to check if a key is currently held down:
while True:
...
pressed = pygame.key.get_pressed()
if pressed[pygame.K_w]:
print("w is pressed")
if pressed[pygame.K_s]:
print("s is pressed")
I would advice you to stick with the event-driven approach rather than using a polling mechanism.
You should let the key events alter some internal state to reflect a pressed key imo.
Example: You are controlling a spaceship with your keyboard. You want the propulsion rockets to fire when you press one of 'w', 's', 'a' or 'd' to make the ship accelerate in a certain direction:
On pygame.KEYDOWN event, set an appropriate acceleration vector for the object if event.key in [K_w, K_s, K_a, K_d].
On pygame.KEYUP event, set the acceleration vector to the zero vector if event.key in [K_w, K_s, K_a, K_d].
This will effectively make the object accelerate while a movement key is pressed, and stop accelerating when the key is released.
event.key == chr('a')
event.key returns the ascii of the key
Use pygame.key.set_repeat().
set_repeat(delay, interval) -> None.
When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number of milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds. If no arguments are passed the key repeat is disabled.