Record multiple keys

Hi,

I’m having a problem coding multiple key presses. I want to be able to see all responses instead of just the last responses, but I am getting an error that I will attach to this post. Please let me know if you have any suggestions.

I tried adding brackets around keys_pressed, but it still gave me this error.

It’s because you’re trying to add a string to a list -

["foo", "bar"] + "hello world" = ["foo","bar"]"hello world"

which doesn’t work - you need to use the list.append() function. Try replacing:

keysPressed+keys 

with

keysPressed.append(keys)

You’d then need to move the thisExp.addData section to the end of routine tab in the code component.

Okay, I tried replacing keys_pressed with the new function. I’m still getting errors. Does it have something to do with “keys = event.getKeys()” being in parentheses? I thought that brackets were needed to output multiple responses. I will attach the new code and screenshots of the errors. Error 1 shows the error with the entire code (using code in the begin routine and each frame tab), and error 2 is only showing it for the each frame tab, without adding the definition in the begin routine tab.

Why have you put keysPressed.append() in begin routine tab when it is in each frame in your original screenshot? If you put that line where it was suggested above it should work fine. The error relates to trying to assign an empty string to the function keysPressed.append() - just delete the =’’ and the error will go away.

I’m sorry, but can you explain exactly what you mean? I keep getting the error that the “‘str’ object has no attribute to append.”

Hi Emily,

Could you perhaps briefly explain what exactly it is you’d like to achieve? Specifically, why can’t you use the “Store: all keys” functionality of the Builder keyboard component?

Jan

Hi Jan,
Because Emily needs to handle other aspects of the keys conditionally in code, and that would conflict with a keyboard component.

Emily:

  • You can’t assign a value to keysPressed.append(). It is a function, not a variable. That line should be

    keysPressed = ‘’

  • You don’t seem to have actually inserted the code I suggested in the other thread under the section of code for when t < 2:

    elif t < 2.0: 
        # for the first key press:
        if not end_at_2s:
            thisExp.addData('RT', t)
            end_at_2s = True 
            
        # for all key presses:
        for key in keys:
            keys_pressed = keys_pressed + key # concatenate multiple keys
        thisExp.addData('rating', keys_pressed) # update the data

which should now fix the issue about adding keys to a list of keys.

Hi Michael,

It seemed to me (please correct me if I’m wrong though!) that Emily would like to

  • record all key presses below 2 s, but end the routine at t = 2 s if there is at least one response at that point in time
  • end the routine immediately when a response is given between 2 and 4 s
  • end the routine at t = 4 s if no response is given

I thought it might be possible to achieve this by

  • Using “Store: all keys”
  • Setting the overall trial length to 4 s
  • Using the following code component in “Each Frame”:
if t >= 2.:
    if len(key_resp.keys) >= 1:
        continueRoutine = False

Jan

1 Like

Useful to have fresh eyes looking at a problem from scratch :wink: That could be a much more elegant way of doing things.

Thank you all for helping me out with this problem. I can now record double responses. :smile: