Execute function when keyboard pressed

How I can to execute a function when keyboard pressed, without ending the routine?

I’m trying to move an image in the frame according to the arrow keys and cant get it done.

Begin Routine

keysPresses = 0

Each Frame

if key_resp.keys:
     if len(key_resp.keys) > keyPresses:
          if key_resp.keys[keyPresses] == 'right':
               # do stuff like xPos += .01
          elif key_resp.keys[keyPresses] == 'left':
               # do other stuff
          keyPresses += 1
1 Like