Cloze plugin key press? #1471
-
Hello. In the cloze plugin, in order to move to the next trial, the participant has to click a buttton. However, I was wondering if it can be possible for them to just press a key and move to the next trial? I have got a lot of cloze trials and it gets frustrating to constantly move your hand away from the keys and click the button. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Yep you can do this by modifying the cloze plugin. I would start by looking at a different plugin that allows a key press to end the trial, such as the html-keyboard-response plugin. In the jsPsych/plugins/jspsych-html-keyboard-response.js Lines 26 to 32 in 21e7fa3 Instead of using jsPsych.ALL_KEYS as the default value, you'll want to use 'enter' or some other non-alphanumeric key, so that participants don't accidentally submit their response while typing into the textboxes. And then in the jsPsych/plugins/jspsych-html-keyboard-response.js Lines 121 to 130 in 21e7fa3 In the jsPsych.pluginAPI.getKeyboardResponse function above, the callback_function parameter is the function that you want to be called after a valid key is pressed. In the cloze plugin, I think the function you want to use here is called check .
Please feel free to follow up here if you need further guidance. And you should also feel free to submit a pull request to add this change to jsPsych's version of the cloze plugin, since this feature might be generally useful for others 😃 |
Beta Was this translation helpful? Give feedback.
-
@becky-gilbert Hi, thank you for your quick response! I followed what you did and sadly I have not been able to make it work. Forgive me for this, I am rather new to jsPsych. The only thing that I have managed to do is that the clickable box to move to the next trial is now gone and the task script is not registering 'enter' as the submit key and therefore there is no way to submit the response with this edited plugin: /**
* jspsych-cloze
* Philipp Sprengholz
*
* Plugin for displaying a cloze test and checking participants answers against a correct solution.
*
* documentation: docs.jspsych.org
**/
jsPsych.plugins['cloze-keypress'] = (function () {
var plugin = {};
plugin.info = {
name: 'cloze-keypress',
description: '',
parameters: {
text: {
type: jsPsych.plugins.parameterType.STRING,
pretty_name: 'Cloze text',
default: undefined,
description: 'The cloze text to be displayed. Blanks are indicated by %% signs and automatically replaced by input fields. If there is a correct answer you want the system to check against, it must be typed between the two percentage signs (i.e. %solution%).'
},
submit_answer: {
type: jsPsych.plugins.parameterType.KEYCODE,
array: true,
pretty_name: 'Submit',
default: 13,
description: 'The key the subject is allowed to submit their answer to the stimulus.'
},
check_answers: {
type: jsPsych.plugins.parameterType.BOOL,
pretty_name: 'Check answers',
default: false,
description: 'Boolean value indicating if the answers given by participants should be compared against a correct solution given in the text (between % signs) after the button was clicked.'
},
mistake_fn: {
type: jsPsych.plugins.parameterType.FUNCTION,
pretty_name: 'Mistake function',
default: function () {},
description: 'Function called if check_answers is set to TRUE and there is a difference between the participants answers and the correct solution provided in the text.'
}
}
};
plugin.trial = function (display_element, trial) {
var html = '<div class="cloze-keypress">';
var elements = trial.text.split('%');
var solutions = [];
for (i=0; i<elements.length; i++)
{
if (i%2 === 0)
{
html += elements[i];
}
else
{
solutions.push(elements[i].trim());
html += '<input type="text" id="input'+(solutions.length-1)+'" value="">';
}
}
html += '</div>';
display_element.innerHTML = html;
var check = function() {
var answers = [];
var answers_correct = true;
for (i=0; i<solutions.length; i++)
{
var field = document.getElementById('input'+i);
answers.push(field.value.trim());
if (trial.check_answers)
{
if (answers[i] !== solutions[i])
{
field.style.color = 'red';
answers_correct = false;
}
else
{
field.style.color = 'black';
}
}
}
if (!trial.check_answers || (trial.check_answers && answers_correct))
{
var trial_data = {
'answers' : answers
};
display_element.innerHTML = '';
jsPsych.finishTrial(trial_data);
}
else
{
trial.mistake_fn();
}
};
// start the response listener
if (trial.submit_answer != jsPsych.NO_KEYS) {
var keyboardListener = jsPsych.pluginAPI.getKeyboardResponse({
callback_function: check,
valid_responses: trial.choices,
rt_method: 'performance',
persist: false,
allow_held_key: false
});
}
if (typeof keyboardListener !== 'undefined') {
jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
}
};
return plugin;
})(); Also, in order to make the cursor appear on the answer box, one would anyway have to click the answer box and therefore the use of the mouse/click is not entirely removed. This would defeat the purpose of having to remove the submit click box. Is there a way to change entirely remove the need to click anything? |
Beta Was this translation helpful? Give feedback.
@becky-gilbert Hi, thank you for your quick response! I followed what you did and sadly I have not been able to make it work. Forgive me for this, I am rather new to jsPsych. The only thing that I have managed to do is that the clickable box to move to the next trial is now gone and the task script is not registering 'enter' as the submit key and therefore there is no way to submit the response with this edited plugin: