To capture more than one keyboard input during a response task in jsPsych, you can use the on_finish
parameter within the trial definition to obtain the response data.
Here's an example code snippet that shows how to capture more than one keyboard input during a response task using jsPsych:
var trial = {
type: 'html-keyboard-response',
stimulus: 'Press "a" and "b" at the same time',
choices: jsPsych.NO_KEYS,
trial_duration: 5000,
on_start: function(){
var keyboardListener = jsPsych.pluginAPI.getKeyboardResponse({
callback_function: end_trial,
valid_responses: ['a', 'b'],
rt_method: 'performance',
persist: true,
allow_held_key: true
});
},
on_finish: function(data){
var key_presses = keyboardListener.getHeldKeys();
data.key_presses = key_presses;
}
};
function end_trial(){
jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
jsPsych.finishTrial();
}
In this example, jsPsych.NO_KEYS
is used as the choices
parameter to prevent the initial keyboard press from being registered. The on_start
function creates a keyboardListener
variable that listens for the a
and b
keys to be held down simultaneously. The on_finish
function then obtains the held keys using the getHeldKeys
function of the keyboardListener
object and stores the data in the key_presses
property of the trial data object.
Note that the allow_held_key
parameter is set to true
in the getKeyboardResponse
call, which allows multiple key presses to be recorded simultaneously.
Asked: 2021-11-15 11:00:00 +0000
Seen: 2 times
Last updated: Feb 25 '22