How to stop keyboard from detecting keypresses?


I am creating an experiment where (1) an image is shown for 3000ms, and then (2) a prompt saying ‘Indoor or outdoor?’ is shown for 2000ms. The participant has to select (using the arrow keys) whether the image shown shows the indoors or outdoors when the prompt is shown and not when the image is shown. I have it so that the keyboard components only resets on the first flip of the text prompt so the recorded reaction time is recorded only from when the prompt is shown.

Everything works fine except if you click an arrow key late (i.e. accidentally late by 0.8s and on the next image), it is recorded as 2.8s (for example) for the trial after. I’ve tried using kb.start() and kb.stop() but this does not do anything. Would anyone know how to ensure that the keyboard component is only listening during those 2000ms? Thank you! I have included the full snippet of code below:

correct_answer = block[str(correct_answer_column)][j]
img_trigger = block['trigger'][j]
ans_trigger = img_trigger + 'a'

img_trigger_sent = False
ans_trigger_sent = False
key_pressed = False
clock_reset = False


while self.__clock.getTime() < 5:
     if self.__clock.getTime() < 3:
          if not img_trigger_sent:
          self.__win.callOnFlip(self.__port.write, img_trigger.encode())
          img_trigger_sent = True
          if not ans_trigger_sent:
               self.__win.callOnFlip(self.__port.write, ans_trigger.encode())
               ans_trigger_sent = True
          if not clock_reset:
               clock_reset = True
          if not key_pressed:
               keys = self.__kb.getKeys(keyList=['left', 'right'], waitRelease=False)
               if len(keys) > 0:
                    key_pressed = True

   if key_pressed:
          response = str(keys[-1].name)
          reaction_time = keys[-1].rt
          if response == correct_answer:
                result = 1
               result = 0
   elif not key_pressed:
           result = np.nan
           reaction_time = np.nan
           response = np.nan

I think you need something like event.clearEvents().