# RT and key response not recording in data

**URL:** <https://discourse.psychopy.org/t/rt-and-key-response-not-recording-in-data/4049>\
**Category:** Builder\
**Created:** [March 3, 2018, 12:28am UTC](https://discourse.psychopy.org/t/rt-and-key-response-not-recording-in-data/4049 "2018-03-03T00:28:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![4d4c](https://avatars.discourse-cdn.com/v4/letter/4/da6949/32.png) [@4d4c](https://discourse.psychopy.org/u/4d4c)\
**Post date:** [March 3, 2018, 12:28am UTC](https://discourse.psychopy.org/t/rt-and-key-response-not-recording-in-data/4049/1 "2018-03-03T00:28:16Z")

</div>

OS: Windows 10  
Psychopy version: 1.85.6

Hi everyone,

I am trying to create my own rating scale instead of using the actual rating scale provided by Psychopy.

I am just going to create an image for the rating scale and have polygons appear over the selected choice.

First I created 3 circle polygons for 3 different choices.  
Then I added the key component to correspond to choices ‘1’,‘2’,‘3’.

I am doing this by inserting a code component and then in the ‘Each Routine’ I add

```auto

event.getKeys('1'): # if 1 key pushed,
    polygon.opacity = .5
    polygon_2.opacity=0
    polygon_3.opacity=0
if event.getKeys('2'):
    polygon_2.opacity=.5
    polygon.opacity=0
    polygon_3.opacity=0
if event.getKeys('3'):
    polygon.opacity=0
    polygon_2.opacity=0
    polygon_3.opacity=.5

```

This way if a 1 is pressed a circle appears over the choice corresponding to the ‘1’ key.

In the ‘End Routine’ section I added this:

```auto

Exp.addData('reaction_time', key_resp_2.rt). 

```

However, in the excel file I only see a [] in the “reaction\_time” section and the key\_resp is blank. Does anyone have any suggestions on how to fix this?

---

<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 4, 2018, 6:48am UTC](https://discourse.psychopy.org/t/rt-and-key-response-not-recording-in-data/4049/2 "2018-03-04T06:48:19Z")

</div>

> [@Please surround python code with backticks](https://discourse.psychopy.org/t/please-surround-python-code-with-backticks/1775):
>
> When you include python code on this forum please put triple “backticks” like this ``` on the line before and the line after your code. If you’ve done it correctly then your code will appear in a grey box with correct indentation like this (check the preview of your post on the right hand side before posting it) and other people will be better able to work out what might be right/wrong with your code. if myVal==True: break If you’ve already posted something that didn’t follow this rule th…

---

<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 4, 2018, 6:58am UTC](https://discourse.psychopy.org/t/rt-and-key-response-not-recording-in-data/4049/3 "2018-03-04T06:58:34Z")

</div>

> [@4d4c](#):
>
> in the ‘Each Routine’

There is no code component tab named that so it is hard to know when this code runs, but I’m guessing it is on every frame. In that case, your code can interfere with any Builder keyboard components. i.e. by calling `event.getKeys()` you clear the event buffer. That means that a subsequent keyboard component would not register the keypress. Also note that your own code is fighting itself in that regard. e.g. if the subject pushes `'2'`, the first call to `event.getKeys(`1`)` will prevent the detection of the `'2'` except in the vanishingly small chance that that key is pressed between the time of the first and the second `event.getKeys()`

You should _store_ the results of `event.getKeys()`, and then cycle through that. e.g.

```auto
response = event.getKeys()

if '1' in response:
    # do something 
elif '2' in response:
    # do something else
# etc

```

You will also need to handle the storing of reaction times and so on, as this code will muck up the keyboard component, by clearing the event buffer.

Alternatively, put the code component beneath the keyboard component, and instead of calling `event.getKeys()`, query what the keyboard component received. e.g.

```auto
if `1` in your_keyboard_component_name.keys():
    # do something 
#etc

```

That way, the keyboard component will still be able to do its job, and store the RT, etc.

---

<div class="post-metadata">

**Author:** ![4d4c](https://avatars.discourse-cdn.com/v4/letter/4/da6949/32.png) [@4d4c](https://discourse.psychopy.org/u/4d4c)\
**Post date:** [March 5, 2018, 12:59am UTC](https://discourse.psychopy.org/t/rt-and-key-response-not-recording-in-data/4049/4 "2018-03-05T00:59:46Z")

</div>

Sorry I meant ‘Each Frame’

Just used

```auto
if `1` in your_keyboard_component_name.keys():

```

and it works! Thanks!
