event.clearEvents()

Hello, I’m coding my experiment in pycharm.
The event.clearEvents() is not working for me. The right/left keys are still stored and used in the next stimuli presented.
I also have another event.clearEvents() during the fixation stimuli and but it did not help.
I’m attaching my code. am I missing something?
thank you very much

def runningSlotMachines(block, win, info, blockNum, feedback, timer, kb, t, part, WL_variables, group, training):

     money = 0
     timer.reset()
 
     # event.clearEvents()
     while timer.getTime() < 3:  # 3 seconds for a response
         event.clearEvents()
         presentTime = dataClock.getTime()  # save the current time the decision was made for later to write in csv
         # (you can't put it here because reward value is defined only later
         trial_start = dataClock.getTime()
 
         slotMachineImage(win, block[part][t][1], 0.5)  # right image
         slotMachineImage(win, block[part][t][0], -0.55)  # left image
         win.flip()
         keys = kb.getKeys(keyList=['left', 'right'])
         if timer.getTime() > 3:
             # x = visual.TextStim(win=win, text='Too slow - you lost ${}'.format(WL_variables["tooSlow"]), color="black",
             #                     bold=True) # english
             x = visual.TextStim(win=win, text='איטי מידי! הפסדת {} שקלים '.format(WL_variables["tooSlow"]), color="black",
                                 bold=True, languageStyle='RTL', units='norm')
             x.draw()
             win.flip()
             core.wait(5)
             timer.reset()
             reward = float("NAN")
             response = 'TooSlow'
             money -= WL_variables["tooSlow"]  # because it is 0.75 and not -0.75
             trial_end = float('nan')
             rt = float('nan')
             datawriter.writerow([presentTime, info['SubjectID'], info['Dominant hand'], group, blockNum,
                                  part, block[part][t][0], block[part][t][1], response, reward, '', '', '', '', '', '',
                                  trial_start, trial_end,
                                  rt,'', '', ''])
             # event.clearEvents() # ensures that you only respond to a key that was actually pressed after the onset of the target,
             #  and discard any that may have been pressed during the image display time
             continue
         if 'left' in keys:
             trial_end = dataClock.getTime()
             rt = trial_end - trial_start
             response = block[part][t][0]
             position_image = -0.55
             reward, money1 = winningProbabilitiesMachines(settings, part, response, feedback, win, position_image,
                                                           timer, WL_variables, training, t)
             money += money1
             datawriter.writerow([presentTime, info['SubjectID'], info['Dominant hand'], group, blockNum,
                                  part, block[part][t][0], block[part][t][1], response, reward, '', '', '', '', '', '',
                                  trial_start, trial_end,
                                  rt, '','', ''])
             if feedback == 0:
                 win.flip()
                 randomWait(win, 1)
             break
         elif 'right' in keys:
             trial_end = dataClock.getTime()
             rt = trial_end - trial_start
             response = block[part][t][1]
             position_image = 0.5
             reward, money1 = winningProbabilitiesMachines(settings, part, response, feedback, win, position_image,
                                                           timer, WL_variables, training, t)
             money += money1
             datawriter.writerow([presentTime, info['SubjectID'], info['Dominant hand'], group, blockNum,
                                  part, block[part][t][0], block[part][t][1], response, reward, '', '', '', '', '', '',
                                  trial_start, trial_end,
                                  rt,'', '', ''])
             if feedback == 0:
                 win.flip()
                 randomWait(win, 1)
             break
         event.clearEvents()
     return money

It’s quite tricky to get one’s head around that code - there is a while loop that only runs while a timer is less than 3.0 s, but embedded within that is code that only runs after 3 s, including waiting for 5 s???

But to guess what your issue is, I think it is because you are mixing calls to the event module despite actually getting the keypresses from an instance of the Keyboard class. The former has no influence over that latter: these two modules are quite independent. The new Keyboard class really supersedes the event module for keyboard handling, as it is superior in every regard. The event module should be regarded as deprecated for keyboard handling (it is still responsible for dealing with the mouse, however).

To get the behaviour you want, delete all calls to event.clearEvents(), and instead call the equivalent method on your keyboard instance, e.g. kb.clearEvents().

I’m not that familiar with the Keyboard class, but when you initiate one, I think you could specify waitForStart=True, and then turn recording on and off at specific times - that might also achieve what you want.

thank you! it worked with kb.clearEvents()

"but embedded within that is code that only runs after 3 s, including waiting for 5 " - yes, it is a “penalty” if you didn’t make the decision in time, you need to make it under 3 seconds.

1 Like