'Using pygame key.name to convert keys to str. How to convert it to pygame key? [duplicate]
In order to build controller setting i want convert key pressed to string.
for that i built a setting controller class:
class KeyboardSettings():
def __init__(self,description,keyboard,rect):
self.description = description
self.keyboard = keyboard
self.active = False
self.default = keyboard
self.rect = pygame.Rect(rect)
def active_key(self):
pos = pygame.mouse.get_pos()
if self.rect.collidepoint((pos)) and pygame.mouse.get_pressed()[0]:
if pygame.mouse.get_pressed()[0]:
self.active = True
elif pygame.mouse.get_pressed()[0]:
self.active = False
All game keys are added to key_obj_lst and In game loop:
for event in pygame.event.get():
if event.type == dict_key:
for k in SETTINGS.key_obj_lst:
# define each key by user
if k.active:
k.keyboard = pygame.key.name(event.key)
SETTINGS.default = False
Now i able to get the key as a string but i wounder if it possible to convert it to pygame.event.key?
Solution 1:[1]
it's good or you can do
while True:
for event in pygame.event.get():
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
moving_left = True
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
