How do i detect keypresses in codecombat?

ive been trying to make a lot of different projects, but most of them need keypress detection! the dumb codecombat ai doesnt want to even bat one eye at that and chatgpt gives ten different answers that never work. How do i detect keypresses in codecombat (python) gamedev levels???

for the record, this works fine:

def heroJump():
jump = game.spawnXY(“fire-trap”, 36, 30)
jump.attackDamage = 0
jump.attackRange = 1

game.on(“keydown”, heroJump)

BUT i just can’t detect a SPECIFIC key (for example, Q) so the player spawns a jump fire trap whenever they press down ANY key on the keyboard.

You should be able to take the input key as an argument.[1]

def heroJump(event)
    if event.key == "q": #if the key is "Q" for example

I don’t even know Python very well, so please respond if this doesn’t work.


  1. ↩︎

this is the first thing i tried to do (chatgpt told me) but it didn’t work. i was really confused, because tecnically the input key should be able to be detected, but it didn’t for some unknown reason. it doesn’t even give an error message though, whitch is weird

can you serialize (convert to string) the event and then print it? Then it should show you the structure of the event and where to access the key.

something like this?

def heroJump(event):
    player.say(event.key)

lemme try that

1 Like

i tried this:

def heroJump(event):
    var = str(event.key)
    player.say(var)

gives an error:

TypeError: cannot read propery tostring of undefined

i personally think that event.key is non-existent but feel free to prove me wrong if it does exist

Convert the whole event object to a string, not just event.key.

thank you, i got slightly more knowledge from this:

def heroJump(event):
    var = str(event)
    player.say(var)

after doing this, the player says [object undefined] instead of a straight up error

Where do you run this code? I’d like to give it a shot.

gamedev 3 final project (works exactly the same in gamedev 2 final project btw)

Do you need to pay to get there?

i think you do, cuz they require beating backwoods forest and sarven desert

yeah I can’t help