How to present the response from the keyboard component

Hi there,

Sorry, i’m new to this.

I have a loop running and I’m using the keyboard component to get a response. There are two options on the screen and you can press ‘a’ or ‘k’ to make a selection. As soon as a selection is made, I would like to display the key the user pressed but bolded. I tried using $key_resp.keys but that resulted in only square brackets being displayed [].

How would I go about doing this?

Thanks!

Hello Aalim Makani

insert a feedback-routine following your response-routine in which the participants press ‘a’ or ‘k’. In the feedback-routine add a code-component. Insert
keyPressed = event.getKeys()
in the Begin Routine tab of this component. Add a text-component to the feedback-routine following the just mentioned code-component. Add as text to the text-component something like
$'You pressed: ' + str(keyPressed)
Set the text to set every repeat

Best wishes Jens

Hi Jens,

Thank you so much for your help with this. It works.

Much appreciated.

Aalim

I’ve noticed (using PsychoPy 2024.1.5) that if the focus is not on the PsychoPy window when the keyboard response is made, which it seems not to be if a mouse click has not yet occurred since the experiment info dialogue at the start of the program, then the keyboard component registers the keyboard response (and the program responds accordingly, for example by advancing the screen if that is what it has been told to do) but the ‘event.keys’ method does not register the keyboard response, with the result that ‘event.keys’ returns an empty list. This can cause the program to crash (for example if an attempt is made to read a value from the ‘keys’ list). The result is that the program can crash intermittently for no reason that is apparent to the user. Calling the ‘keys’ method on the keyboard component instead of on the ‘event’ object avoids this issue but does not necessarily help much, because in this case the ‘keys’ list is not updated until after the end of the routine that contains the keyboard component, so the keyboard response cannot be checked until the following routine.

You can check key_resp.keys each frame.

I skipped 2024.1.5 going straight from 2024.1.4 to 2024.2 because I thought it introduced some new bugs (relevant to me but I can’t remember what).

Thanks for your response. This sounds like it should work, but unfortunately it doesn’t. The ‘keys’ list comes up empty and the program crashes! I think I’ll live with the behaviour of ‘event.keys’ for now. (Switching to a later version of PsychoPy is not so easy because our IT team has complete control over what we can install and there is a long process of approving a new version and making it available.)

key_resp.keys doesn’t exist until the first key is pressed so I check that first, e.g.

if key_resp.keys:
      if 'space' in key_resp.keys:
             blah