Presenting stimuli for fixed duration despite keypress
Hello,
I wondered if anyone knew how to present a word stimuli on the screen and have it remain for a fixed duration, even if I need to collect a keypress? They keypress automatically changes the screen to the next page/stimulus. I need to collect a keypress response, but I also need to have each stimuli on screen for 1800 ms. If anybody knows how/if this is possible, please let me know!
Thanks,
Nikita
Comments
Hi Nikita,
The easiest way to do this is using
coroutines, which allow you to run multiple items in parallel. In your case, you could present the stimulussketchpadat time 0, a blanksketchpadat time 1800, and then have akeyboard_responseactive from time 0 to 2500 ms (or however long you want the response period to be).Important:
coroutinesare currently not supported by OSWeb, which is important if you want to run this experiment online.Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
Thank you for the response. I am actually planning to eventually run it as an online experiment, so are there other options for doing this? Half of my stimuli require getting one keyboard response (either a "z" or "/"), while the other half require getting two (either a "z" or "/" paired with "space"). I understand that this will likely need an inline script, but I am not familiar with coding. I looked through other forum discussions that asked similar questions, but have been struggling for a long time now to find a code that will work for my experiment so that I can get keyboard responses within a fixed duration of 1800 ms (with an interstimulus interval of 200 ms), and also collect two correct keyboard responses for some of the presented word stimuli. Do you have any ideas?
Let me know if this is confusing, or if you need more information. Again, thank you!
Best,
Nikita
Hi Nikita,
One way to do this is to take control of key-press events directly in JavaScript. You can do this by registering a function that receives
keydownevents and processes them. To make sure that this function doesn't interfere with the rest of the experiment, you should have it unregister itself after the second keypress. The script below illustrates how to do this.The trick will be to insert this code into the Run phase of an
inline_javascriptitem just before you start showing a series ofsketchpaditems, which simply can be presented as a slideshow without being intermixed withkeyboard_responseitems.Hope this helps!
Cheers,
Sebastiaan
function onKeyPress(event) { console.log(event) // On the first key press we simply log the key code and update // the n_key variable if (n_key === 0) { vars.first_key = event.code vars.first_rt = new Date().getTime() - t0 n_key += 1 // On the second key press we log the key code again and also // remove the key listener } else { vars.second_key = event.code vars.second_rt = new Date().getTime() - t0 window.removeEventListener('keydown', onKeyPress) } } let n_key = 0 let t0 = new Date().getTime() window.addEventListener('keydown', onKeyPress)Check out SigmundAI.eu for our OpenSesame AI assistant!
Hello Sebastiaan,
Thank you so much for the code. However, when I used it I got an error message that I've attached below. I did try to define the variable, but it did not work. Would you know how to possibly fix this error? Thank you so much!
Best,
Nikita
Right, this code requires that you run the experiment in a browser! The
inline_javascriptitem works to some extent on the desktop, but not when you're really diving into the browser internals, like this script does.Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
The code seems to work fine when I run the experiment in the browser. Thank you so much for all of your help!
Best,
Nikita
Hello Sebastiaan,
I have a question again about this code you provided me. I no longer need to collect two key responses, but I still need to make sure my stimuli remains on screen for a fixed amount of time despite the keypress, so I modified this by deleting the second half of the code you provided in my experiment (I have the image attached below). While I am running through the experiment in Jatos it works fine, but when I export my results into Excel it shows that a key was pressed for stimuli even when I did not press any key.
For example, during my practice run I pressed no keys for the first three stimuli, pressed the / key for the fourth stimuli, then did not press any keys for the remaining four stimuli. When I looked at my results, it showed that from the fourth stimuli and onward, the / keys were pressed even though I had only pressed it one time.
Is there a way I can modify this code so that the experiment does not think I am pressing keys when I am not?
Best,
Nikita
Hi @nikitaadhikari1 ,
This is probably because you're not resetting the
vars.first_keyvariable at the start of the trial, thus getting carry-over effects. If you add add the following lines at the end of the script, this problem should go away:Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
This worked perfectly, thank you so much!
Best,
Nikita
Hi Sebastiaan,
Sorry for asking so many questions but I have one more for this experiment. For my program that requires getting two responses, I can only have responses such as "z" and "space" in one trial or "/" and "space" in one trial. If the participants press "z" and "/" in one trial, the results are unclear. Do you know how I can make it so that if the participants hit one of the keypresses, the other one becomes unavailable?
For example, if they press the "z" key, I would like the "/" key to be unavailable and vice versa. The spacebar can remain available throughout. Is there a code I can add to have this in my experiment?
Thank you in advance!
Hi @nikitaadhikari1 ,
I think you can try to do this yourself 😀 To give you some idea of how to implement this.
The
event.codevariable contains the key. If you define anallowed_responsein yourlooptable, then you can check ifevent.codematches this (vars.allowed_responsein the script), or if it matches a 'space' (which is always allowed). If you perform this check at the start ofonKeyPressEvent(), then you canreturnfrom the function if the check fails, thus ignoring all keys except the allowed ones.Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
I noticed that in the example code above
event.codecontains more characters than just the response (e.g. "KeyN" if you press n). If you only need "n" you can useevent.key