2

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)

1 Answers1

4

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]:
    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    the keydown event also has a `unicode` attribute which can be used directly. It will come event for special keys, for which the `.key` attribute would be inorrect anyway (for example code "273" for up arrow key) – jsbueno Jan 27 '20 at 17:23
  • What (Unicode or otherwise) gets returned for the left shift key? Why not use `key.name`? – Jongware Jan 27 '20 at 17:30
  • so `event.key` gives a number & `event.unicode` gives me the key but dosen't really work for the arrow keys (it returns them as a blank) – Avenger_ aawin Jan 27 '20 at 17:41
  • yeah I suppose I could do that for the keys that have no representable signs then just `else: slectedKey = event.unicode` – Avenger_ aawin Jan 27 '20 at 17:50
  • @Rabbid76 but I would still prefer if I didn't have to use a lot of elif statments – Avenger_ aawin Jan 27 '20 at 17:52
  • @Rabbid76 I want to print the selected key – Avenger_ aawin Jan 27 '20 at 17:58
  • I found this code wich seems to solve the problem(he did use sys tho) https://stackoverflow.com/a/47857397/12607177 Is there a way to do it without importing sys – Avenger_ aawin Jan 27 '20 at 18:20
  • @Avenger_aawin, Regarding the link you posted: read the code and search for `sys`, what do they use `sys` for? Do you need to do that? You can very quickly answer your own questions with a bit of research – SyntaxVoid Jan 27 '20 at 18:28
  • 1
    See https://www.pygame.org/docs/ref/key.html#pygame.key.name: "Get the descriptive name of the button from a keyboard button id constant." – Jongware Jan 27 '20 at 18:32
  • @usr2564301 Thank you. You never stop learning ;-) – Rabbid76 Jan 27 '20 at 18:40