How to stop keyboard from detecting keypresses?

Hi,

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:

stimulus.setImage(block['stimulus'][j]) 
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

self.__clock.reset()

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

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

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