# Negative keyboard reaction times

**URL:** <https://discourse.psychopy.org/t/negative-keyboard-reaction-times/30799>\
**Category:** Coding\
**Created:** [September 12, 2022, 7:01am UTC](https://discourse.psychopy.org/t/negative-keyboard-reaction-times/30799 "2022-09-12T07:01:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pv1313](https://avatars.discourse-cdn.com/v4/letter/p/e79b87/32.png) [@Pv1313](https://discourse.psychopy.org/u/Pv1313)\
**Post date:** [September 12, 2022, 7:01am UTC](https://discourse.psychopy.org/t/negative-keyboard-reaction-times/30799/1 "2022-09-12T07:01:42Z")

</div>

I’m a complete beginner to Psychopy and this is the first experiment I’m coding in it. So, the experiment I’m doing is to ask the user to press a key whenever the letter ‘s’ appears on the screen. Then, I am recording the reaction time of the key press. But I am getting negative reaction times. Can someone explain to me what is the problem in my code?

from psychopy import visual, core, clock  
from psychopy.hardware import keyboard  
import random, string

```auto
num = 30
reac_times = []
count = 0
incorr_press = 0
rand_str = list(''.join(random.choices(string.ascii_lowercase, k = num)))
for c in rand_str:
     if c == 's' or c =='m':
         rand_str.remove(c)
targ_str = ['ms']
for i in range(0,10):
     index = random.randint(0,len(rand_str))
     rand_str = rand_str[:index] + targ_str + rand_str[index:]
new_str = ''.join([str(elem) for elem in rand_str])

win = visual.Window([400,400])
instr = visual.TextStim(win, text = "Press any key whenever letter 's' appears on screen")
instr.pos = (0,0.8)
instr.autoDraw = True
win.flip()

kb = keyboard.Keyboard()
clock = core.Clock()

for i in range(0, len(new_str)):
     msg = visual.TextStim(win, text = new_str[i])
     msg.draw() 
     win.flip()
     core.wait(1.5)
     kb.clock.reset()
     keys = kb.getKeys()
     if keys:
         if new_str[i] == 's':
             count = count + 1
             reac_times.append(keys[0].rt)
             print(reac_times)
         else:
             incorr_press = incorr_press+1
     
    

print(reac_times)
print(count)

win.close()
core.quit()

```

---

<div class="post-metadata">

**Author:** ![Christophe\_Bossens](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/christophe_bossens/32/35415_2.png) [@Christophe\_Bossens](https://discourse.psychopy.org/u/Christophe_Bossens)\
**Post date:** [September 12, 2022, 8:07am UTC](https://discourse.psychopy.org/t/negative-keyboard-reaction-times/30799/2 "2022-09-12T08:07:51Z")

</div>

I think you have to move the kb.clock.reset() call before the core.wait(1.5) function. The timestamp is relative to the last reset, so if anyone pressed a key during the wait period that will come out as a negative reaction time.

---

<div class="post-metadata">

**Author:** ![Pv1313](https://avatars.discourse-cdn.com/v4/letter/p/e79b87/32.png) [@Pv1313](https://discourse.psychopy.org/u/Pv1313)\
**Post date:** [September 12, 2022, 2:13pm UTC](https://discourse.psychopy.org/t/negative-keyboard-reaction-times/30799/3 "2022-09-12T14:13:54Z")

</div>

Hey, Thanks! It worked.
