3

I want to be able to change the keys of a game and I am using tkinter to recieve the keys as chars:

key = 'a'

I want to convert those chars to pygame event variables like this:

pygame.K_a

and keep them in a dictionary that i have already prepared.

I have seen ways to do the opposite but i have not seen a way to do this.

I want to use the arrow keys and special characters, can i do it? or do i need to manually set up keywords?

Nimrod Rappaport
  • 134
  • 1
  • 1
  • 12
  • I'm not sure of getting the point of this. So you want to capture the key pressed as character, and then convert it to a key event, instead of directly getting the key event. Why? If it's because you have objects in the game listening to key events and you want to be able to change the key mappings, the clean way to do this is to make your objects listen to "action" events instead of key events, an then map the key event to the action event. – Javier Paz Sedano Mar 19 '19 at 12:15
  • Its because I don't get the chars directly from pygame. I take the chars from tkinter entries – Nimrod Rappaport Mar 19 '19 at 12:21

1 Answers1

2

The values of the pygame.key constants art the ASCII values of the corresponding characters.

Instead of the the key

pygame.K_a

the ASCII value of the character 'a' can be used. The "value" of 'a' can be get by ord():

e.g.

pressed_a = False
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == ord(`a`):
            pressed_a = True

or

k = pygame.key.get_pressed()
pressed_a = k[ord('a')]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174