# Record multiple keys

**URL:** <https://discourse.psychopy.org/t/record-multiple-keys/2163>\
**Category:** Coding\
**Created:** [March 21, 2017, 12:30pm UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163 "2017-03-21T12:30:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hokette2](https://avatars.discourse-cdn.com/v4/letter/h/f0a364/32.png) [@hokette2](https://discourse.psychopy.org/u/hokette2)\
**Post date:** [March 21, 2017, 12:30pm UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/1 "2017-03-21T12:30:40Z")

</div>

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. ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/3/3e79a03e18bf2b8eb850e92b2f8259640b7c6bff.PNG)

 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/d/d05c0a6cf6f279a9240195d3afb974235a29a29d.PNG)

I tried adding brackets around keys\_pressed, but it still gave me this error.

---

<div class="post-metadata">

**Author:** ![Oli](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/oli/32/520_2.png) [@Oli](https://discourse.psychopy.org/u/Oli)\
**Post date:** [March 21, 2017, 1:55pm UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/2 "2017-03-21T13:55:52Z")

</div>

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

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

```

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

```auto
keysPressed+keys 

```

with

```auto
keysPressed.append(keys)

```

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

---

<div class="post-metadata">

**Author:** ![Hokett\_Emily](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Hokett\_Emily](https://discourse.psychopy.org/u/Hokett_Emily)\
**Post date:** [March 21, 2017, 6:13pm UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/3 "2017-03-21T18:13:43Z")

</div>

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.

 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/5/543dcf0847fca237f980686616ae8fcc9c5fd6e9.PNG)

![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/f/f2a201f87212731c3de2a9ba3f2170d116ef422e.PNG)

 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/e/eda75ad4a9ef61e68eccaf0148fd4198c4b51aa0.PNG)

 ![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/2X/b/b142c4696b024cf39d5a5f65a7fb860361648ae2.PNG)

---

<div class="post-metadata">

**Author:** ![Oli](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/oli/32/520_2.png) [@Oli](https://discourse.psychopy.org/u/Oli)\
**Post date:** [March 21, 2017, 6:23pm UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/4 "2017-03-21T18:23:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hokette2](https://avatars.discourse-cdn.com/v4/letter/h/f0a364/32.png) [@hokette2](https://discourse.psychopy.org/u/hokette2)\
**Post date:** [March 21, 2017, 7:33pm UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/5 "2017-03-21T19:33:18Z")

</div>

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.”

---

<div class="post-metadata">

**Author:** ![jderrfuss](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jderrfuss/32/5245_2.png) [@jderrfuss](https://discourse.psychopy.org/u/jderrfuss)\
**Post date:** [March 21, 2017, 10:27pm UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/6 "2017-03-21T22:27:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [March 21, 2017, 10:34pm UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/7 "2017-03-21T22:34:13Z")

</div>

> [@jderrfuss](#):
>
> Specifically, why can’t you use the “Store: all keys” functionality of the Builder keyboard component?

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

- 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:

```auto
    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.

---

<div class="post-metadata">

**Author:** ![jderrfuss](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jderrfuss/32/5245_2.png) [@jderrfuss](https://discourse.psychopy.org/u/jderrfuss)\
**Post date:** [March 21, 2017, 11:22pm UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/8 "2017-03-21T23:22:49Z")

</div>

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”:

```auto
if t >= 2.:
    if len(key_resp.keys) >= 1:
        continueRoutine = False

```

Jan

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [March 22, 2017, 12:30am UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/9 "2017-03-22T00:30:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hokette2](https://avatars.discourse-cdn.com/v4/letter/h/f0a364/32.png) [@hokette2](https://discourse.psychopy.org/u/hokette2)\
**Post date:** [March 24, 2017, 3:19am UTC](https://discourse.psychopy.org/t/record-multiple-keys/2163/10 "2017-03-24T03:19:05Z")

</div>

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