I have some python code with keydown events happening, I am basically wondering if it is possible to have two keys pressed at a time, like ctrl+a or something like that. Is this possible, or will I have to find a workaround?
Use pygame.key.get_mods() to get state of special keys like Control or Shift.
get_mods()
gives one integer and you have to use bitwise operators to compare it with constants like KMOD_SHIFT
See documentation: pygame.key
EDIT: example
import pygame
import pygame.locals
pygame.init()
screen = pygame.display.set_mode((300,200))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_a and pygame.key.get_mods() & pygame.KMOD_SHIFT:
print "pressed: SHIFT + A"
pygame.quit()
BTW: you can use KMOD_LSHIFT
or KMOD_RSHIFT
to test only left shift or only right shift.
EDIT:
BTW: example how to use get_pressed()
K_LSHIFT
and K_LSHIFT
to check both shifts..
import pygame
import pygame.locals
pygame.init()
screen = pygame.display.set_mode((300,200))
running = True
while running:
#
# events
#
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
#
# others
#
all_keys = pygame.key.get_pressed()
#print 'shift:', all_keys[pygame.K_LSHIFT], all_keys[pygame.K_RSHIFT]
if all_keys[pygame.K_a] and (all_keys[pygame.K_LSHIFT] or all_keys[pygame.K_RSHIFT]):
print "pressed: SHIFT + A"
pygame.quit()
BTW: get_pressed()
and get_mods()
give actual information only if pygame.event.get()
was use before.
EDIT:
How to recognize A, CTRL+A, SHIFT+A, ALT+A, CTRL+SHIFT+A, CTRL+ALT+A, SHIFT+ALT+A, , CTRL+SHIFT+ALT+A
import pygame
import pygame.locals
pygame.init()
screen = pygame.display.set_mode((300,200))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_a:
mods = pygame.key.get_mods()
if mods & pygame.KMOD_CTRL and mods & pygame.KMOD_SHIFT and mods & pygame.KMOD_ALT:
print "pressed: CTRL+SHIFT+ALT + A"
elif mods & pygame.KMOD_CTRL and mods & pygame.KMOD_SHIFT:
print "pressed: CTRL+SHIFT + A"
elif mods & pygame.KMOD_CTRL and mods & pygame.KMOD_ALT:
print "pressed: CTRL+ALT + A"
elif mods & pygame.KMOD_SHIFT and mods & pygame.KMOD_ALT:
print "pressed: SHIFT+ALT + A"
elif mods & pygame.KMOD_SHIFT:
print "pressed: SHIFT + A"
elif mods & pygame.KMOD_CTRL:
print "pressed: CTRL + A"
elif mods & pygame.KMOD_ALT:
print "pressed: ALT + A"
else:
print "pressed: A"
pygame.quit()
BTW: On my computer is problem with Right Alt because it is used for native chars. It doesn't work with KMOD_ALT
and KMOD_RALT
.