Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.