Hi everyone!
Description of the problem: I want to prepare an experiment in which a specific key should be pressed long enough to continue to the following routine. I could build the experiment on PsychoPy however I am struggling to run it online.
What I want to achieve? There will be a fixation (+) and then the space key should be pressed for 1 second or longer. Only if the space key is pressed for a minimum 1 second, the routine should end and the next routine with a new target (an image) should start.
What did I do to make it work? I created a keyboard component with custom code as follows for Python and Javascript
#Begin Routine in Py
my_keyboard = keyboard.Keyboard()
keys = []
keys_released = []
//Begin Routine in JS
my_keyboard = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
keys = [];
keys_released = [];
and then I created a list for key presses (keys) and another list for key releases (keys_released). In each frame tab, I check constantly whether there is a key press but not a key release (meaning that the key is still being pressed). If the space key is pressed for a minimum of 1 second, a new target should be defined/appear. However, if I press for a short time, the key press should be cleared and the code needs to wait for me to make another space press.
Here is the python code:
keys = my_keyboard.getKeys(keyList=['space'], waitRelease=False, clear=False)
keys_released = my_keyboard.getKeys(keyList=['space'], waitRelease=True, clear=False)
if len(keys) and not len(keys_released): #If I press "space" but do not release my finger
pressing_time = [(kk.rt) for kk in keys] # get the time that I pressed "space"
pressed_duration = t - pressing_time[0] #how many milliseconds have been passed since the key press
print("been pressed",pressed_duration)
if pressed_duration >= 1: # If I press space for a minimum 1 second , define the new target
target_now = "my_new_target_name"
elif pressed_duration < 1: # If I press space less than 1 second target should be the same
target_now = target_now
elif len(keys) and len(keys_released): # If I remove my finger from the space key
if pressed_duration > 1: # If I pressed longer than 1 second, end the routine
RTrelease = t
continueRoutine = False
elif pressed_duration < 0.75: # If I pressed shorter than 1 second clear my key presses and wait for a new space press (which needs to be 1 second or longer)
my_keyboard.clearEvents()
I tried to used Auto → JS, however, the code that is created automatically does not work online. I am a little bit stuck at the moment.
Should I modify my Python code to be able to translate it online?
Or what would be the Javascript equivalent of the following lines?
—my_keyboard.getKeys(keyList=[‘space’], waitRelease=True, clear=False)
—pressing_time = [(kk.rt) for kk in keys]
I have seen a few similar topics in the forum however, I still could not find a solution for my problem. For instance, I have tried the keys = event.getKeys(). But as far as I understand it collects any key presses but not one specific one.
Thanks in advance for your answers and help!
Cheers