change direct key comparisons to use jsPsych.pluginAPI.compareKeys to allow for case sensitive or insensitive comparison #396

This commit is contained in:
Becky Gilbert 2021-02-18 12:52:10 -08:00
parent 0b952934c1
commit 2c28278ad5
27 changed files with 48 additions and 44 deletions

View File

@ -205,7 +205,7 @@ var block = {
choices: ['y', 'n'],
prompt: '<p>Press "y" to Continue. Press "n" to end this node of the experiment.</p>',
on_finish: function(data) {
if (data.response == 'n') {
if (jsPsych.pluginAPI.compareKeys(data.response, 'n')) {
jsPsych.endCurrentTimeline();
}
},
@ -258,7 +258,7 @@ var trial = {
choices: ['y', 'n']
prompt: '<p>Press "y" to Continue. Press "n" to end the experiment</p>',
on_finish: function(data){
if(data.response == "n"){
if(jsPsych.pluginAPI.compareKeys(data.response, "n")){
jsPsych.endExperiment('The experiment was ended by pressing "n".');
}
}
@ -466,7 +466,7 @@ var trial = {
stimulus: 'Press p to take a 30 second break. Otherwise, press c to continue immediately.',
choices: ['p','c'],
on_finish: function(data){
if(data.response == "p") {
if(jsPsych.pluginAPI.compareKeys(data.response, "p")) {
jsPsych.pauseExperiment();
setTimeout(jsPsych.resumeExperiment, 30000);
}
@ -535,7 +535,7 @@ var trial = {
stimulus: 'Press p to take a 30 second break. Otherwise, press c to continue immediately.',
choices: ['p','c'],
on_finish: function(data){
if(data.response == "p") {
if(jsPsych.pluginAPI.compareKeys(data.response, "p")) {
jsPsych.pauseExperiment();
setTimeout(jsPsych.resumeExperiment, 30000);
}

View File

@ -218,7 +218,7 @@ jsPsych.pluginAPI.getKeyboardResponse({
var after_response = function(info){
alert('You pressed key '+info.key+' after '+info.rt+'ms');
if(info.key == 'q'){ /
if(jsPsych.pluginAPI.compareKeys(info.key,'q')){ /
jsPsych.pluginAPI.cancelKeyboardResponse(listener);
}
}

View File

@ -46,7 +46,7 @@ var trial = {
type: 'image-keyboard-response',
stimulus: 'imgA.png',
on_finish: function(data) {
if(data.response == 'j'){
if(jsPsych.pluginAPI.compareKeys(data.response, 'j')){
data.correct = true;
} else {
data.correct = false;

View File

@ -61,7 +61,7 @@ var trial = {
type: 'image-keyboard-response',
stimulus: 'imgA.jpg',
on_finish: function(data){
if(data.response == 'j'){
if(jsPsych.pluginAPI.compareKeys(data.response, 'j')){
data.correct = true;
} else {
data.correct = false;

View File

@ -22,7 +22,7 @@ var trial = {
},
on_finish: function(data){
// Score the response as correct or incorrect.
if(data.response == "f"){
if(jsPsych.pluginAPI.compareKeys(data.response, "f")){
data.correct = true;
} else {
data.correct = false;

View File

@ -132,7 +132,7 @@ var trial = {
correct_response: 'f'
},
on_finish: function(data){
if(data.response == data.correct_response){
if(jsPsych.pluginAPI.compareKeys(data.response, data.correct_response)){
data.correct = true;
} else {
data.correct = false;

View File

@ -352,7 +352,7 @@ var trial = {
var loop_node = {
timeline: [trial],
loop_function: function(data){
if(data.values()[0].response == 'r'){
if(jsPsych.pluginAPI.compareKeys(data.values()[0].response, 'r')){
return true;
} else {
return false;
@ -382,7 +382,7 @@ var if_node = {
// get the data from the previous trial,
// and check which key was pressed
var data = jsPsych.data.get().last(1).values()[0];
if(data.response == 's'){
if(jsPsych.pluginAPI.compareKeys(data.response, 's')){
return false;
} else {
return true;

View File

@ -1059,7 +1059,7 @@ var test = {
correct_response: jsPsych.timelineVariable('correct_response')
},
on_finish: function(data){
data.correct = data.response == data.correct_response;
data.correct = jsPsych.pluginAPI.compareKeys(data.response, data.correct_response);
}
}
```
@ -1147,7 +1147,7 @@ The `data.response` value is a string representation of the key the subject pres
correct_response: jsPsych.timelineVariable('correct_response')
},
on_finish: function(data){
data.correct = data.response == data.correct_response;
data.correct = jsPsych.pluginAPI.compareKeys(data.response, data.correct_response);
}
}
@ -1291,7 +1291,7 @@ This code is available in the `/examples` folder in the jsPsych release download
correct_response: jsPsych.timelineVariable('correct_response')
},
on_finish: function(data){
data.correct = data.response == data.correct_response;
data.correct = jsPsych.pluginAPI.compareKeys(data.response, data.correct_response);
}
}

View File

@ -17,7 +17,8 @@
var loop_node = {
timeline: [trial],
loop_function: function(data){
if(data.values()[0].response == 'r'){
var key_response = data.values()[0].response;
if(jsPsych.pluginAPI.compareKeys(key_response,'r')){
return true;
} else {
return false;
@ -40,7 +41,7 @@
timeline: [if_trial],
conditional_function: function(){
var data = jsPsych.data.get().last(1).values()[0];
if(data.response == 's'){
if(jsPsych.pluginAPI.compareKeys(data.response, 's')){
return false;
} else {
return true;

View File

@ -61,9 +61,9 @@
data: jsPsych.timelineVariable('data'),
on_finish: function(data){
var correct = false;
if(data.direction == 'left' && data.response == 'ArrowLeft' && data.rt > -1){
if(data.direction == 'left' && jsPsych.pluginAPI.compareKeys(data.response, 'ArrowLeft') && data.rt > -1){
correct = true;
} else if(data.direction == 'right' && data.response == 'ArrowRight' && data.rt > -1){
} else if(data.direction == 'right' && jsPsych.pluginAPI.compareKeys(data.response, 'ArrowRight') && data.rt > -1){
correct = true;
}
data.correct = correct;

View File

@ -77,7 +77,7 @@
correct_response: jsPsych.timelineVariable('correct_response')
},
on_finish: function (data) {
data.correct = data.response == data.correct_response;
data.correct = jsPsych.pluginAPI.compareKeys(data.response, data.correct_response);
}
}

View File

@ -24,7 +24,7 @@
render_on_canvas: false,
prompt: '<p>Press "y" to continue. Press "n" to end this node of the experiment.</p>',
on_finish: function(data) {
if (data.response == "n") {
if (jsPsych.pluginAPI.compareKeys(data.response, "n")) {
jsPsych.endCurrentTimeline();
}
},

View File

@ -28,7 +28,7 @@
prompt: '<p>Press "y" to continue. Press "n" to end the experiment.</p>',
render_on_canvas: false,
on_finish: function(data) {
if (data.response == 'n') {
if (jsPsych.pluginAPI.compareKeys(data.response, 'n')) {
jsPsych.endExperiment('The experiment was ended. This is the end message.');
}
},

View File

@ -90,9 +90,9 @@
},
on_finish: function(data){
if(data.word_validity == 'valid'){
var correct = data.response == 'y';
var correct = jsPsych.pluginAPI.compareKeys(data.response, 'y');
} else {
var correct = data.response == 'n';
var correct = jsPsych.pluginAPI.compareKeys(data.response, 'n');
}
data.correct = correct;
}

View File

@ -230,7 +230,7 @@ jsPsych.plugins["categorize-animation"] = (function() {
}
correct = false;
if (trial.key_answer == info.key) {
if (jsPsych.pluginAPI.compareKeys(trial.key_answer, info.key)) {
correct = true;
}

View File

@ -130,7 +130,7 @@ jsPsych.plugins['categorize-html'] = (function() {
jsPsych.pluginAPI.cancelAllKeyboardResponses();
var correct = false;
if (trial.key_answer == info.key) {
if (jsPsych.pluginAPI.compareKeys(trial.key_answer,info.key)) {
correct = true;
}

View File

@ -132,7 +132,7 @@ jsPsych.plugins['categorize-image'] = (function() {
jsPsych.pluginAPI.cancelAllKeyboardResponses();
var correct = false;
if (trial.key_answer == info.key) {
if (jsPsych.pluginAPI.compareKeys(trial.key_answer, info.key)) {
correct = true;
}

View File

@ -88,7 +88,7 @@ jsPsych.plugins['external-html'] = (function() {
if (trial.cont_btn) { display_element.querySelector('#'+trial.cont_btn).addEventListener('click', finish); }
if (trial.cont_key) {
var key_listener = function(e) {
if (e.key == trial.cont_key) finish();
if (jsPsych.pluginAPI.compareKeys(e.key,trial.cont_key)) finish();
};
display_element.addEventListener('keydown', key_listener);
}

View File

@ -194,7 +194,7 @@
}
if(trial.stim_key_association == "right") {
if(response.rt !== null && response.key == rightKeyCode) {
if(response.rt !== null && jsPsych.pluginAPI.compareKeys(response.key, rightKeyCode)) {
response.correct = true;
if (trial.response_ends_trial) {
end_trial();
@ -226,7 +226,7 @@
}
}
} else if(trial.stim_key_association == "left") {
if(response.rt !== null && response.key == leftKeyCode) {
if(response.rt !== null && jsPsych.pluginAPI.compareKeys(response.key, leftKeyCode)) {
response.correct = true;
if (trial.response_ends_trial) {
end_trial();

View File

@ -196,7 +196,7 @@
}
if(trial.stim_key_association == "right") {
if(response.rt !== null && response.key == rightKeyCode) {
if(response.rt !== null && jsPsych.pluginAPI.compareKeys(response.key, rightKeyCode)) {
response.correct = true;
if (trial.response_ends_trial) {
end_trial();
@ -228,7 +228,7 @@
}
}
} else if(trial.stim_key_association == "left") {
if(response.rt !== null && response.key == leftKeyCode) {
if(response.rt !== null && jsPsych.pluginAPI.compareKeys(response.key, leftKeyCode)) {
response.correct = true;
if (trial.response_ends_trial) {
end_trial();

View File

@ -209,13 +209,13 @@ jsPsych.plugins.instructions = (function() {
allow_held_key: false
});
// check if key is forwards or backwards and update page
if (info.key == trial.key_backward) {
if (jsPsych.pluginAPI.compareKeys(info.key, trial.key_backward)) {
if (current_page !== 0 && trial.allow_backward) {
back();
}
}
if (info.key == trial.key_forward) {
if (jsPsych.pluginAPI.compareKeys(info.key, trial.key_forward)) {
next();
}

View File

@ -600,7 +600,10 @@ jsPsych.plugins["rdk"] = (function() {
if(trial.correct_choice.constructor === Array){ //If it is an array
//If the elements are characters
if(typeof trial.correct_choice[0] === 'string' || trial.correct_choice[0] instanceof String){
return trial.correct_choice.includes(response.key); //If the response is included in the correct_choice array, return true. Else, return false.
var key_in_choices = trial.correct_choice.every(function(x) {
return jsPsych.pluginAPI.compareKeys(x,response.key);
});
return key_in_choices; //If the response is included in the correct_choice array, return true. Else, return false.
}
//Else if the elements are numbers (javascript character codes)
else if (typeof trial.correct_choice[0] === 'number'){
@ -612,7 +615,7 @@ jsPsych.plugins["rdk"] = (function() {
//If the element is a character
if(typeof trial.correct_choice === 'string' || trial.correct_choice instanceof String){
//Return true if the user's response matches the correct answer. Return false otherwise.
return response.key == trial.correct_choice;
return jsPsych.pluginAPI.compareKeys(response.key, trial.correct_choice);
}
//Else if the element is a number (javascript character codes)
else if (typeof trial.correct_choice === 'number'){

View File

@ -71,9 +71,9 @@ jsPsych.plugins['reconstruction'] = (function() {
var key_d = trial.key_decrease;
// get new param value
if (info.key == key_i) {
if (jsPsych.pluginAPI.compareKeys(info.key, key_i)) {
param = param + trial.step_size;
} else if (info.key == key_d) {
} else if (jsPsych.pluginAPI.compareKeys(info.key, key_d)) {
param = param - trial.step_size;
}
param = Math.max(Math.min(1, param), 0);

View File

@ -127,11 +127,11 @@ jsPsych.plugins['same-different-html'] = (function() {
var skey = trial.same_key;
var dkey = trial.different_key;
if (info.key == skey && trial.answer == 'same') {
if (jsPsych.pluginAPI.compareKeys(info.key, skey) && trial.answer == 'same') {
correct = true;
}
if (info.key == dkey && trial.answer == 'different') {
if (jsPsych.pluginAPI.compareKeys(info.key, dkey) && trial.answer == 'different') {
correct = true;
}

View File

@ -128,11 +128,11 @@ jsPsych.plugins['same-different-image'] = (function() {
var skey = trial.same_key;
var dkey = trial.different_key;
if (info.key == skey && trial.answer == 'same') {
if (jsPsych.pluginAPI.compareKeys(info.key,skey) && trial.answer == 'same') {
correct = true;
}
if (info.key == dkey && trial.answer == 'different') {
if (jsPsych.pluginAPI.compareKeys(info.key, dkey) && trial.answer == 'different') {
correct = true;
}

View File

@ -197,7 +197,7 @@ jsPsych.plugins["serial-reaction-time"] = (function() {
for(var i=0; i<trial.choices.length; i++){
for(var j=0; j<trial.choices[i].length; j++){
var t = trial.choices[i][j];
if(info.key == t){
if(jsPsych.pluginAPI.compareKeys(info.key, t)){
responseLoc = [i,j];
break;
}

View File

@ -179,8 +179,8 @@ jsPsych.plugins["visual-search-circle"] = (function() {
var correct = false;
if ((info.key == trial.target_present_key) && trial.target_present ||
(info.key == trial.target_absent_key) && !trial.target_present) {
if ((jsPsych.pluginAPI.compareKeys(info.key, trial.target_present_key)) && trial.target_present ||
(jsPsych.pluginAPI.compareKeys(info.key, trial.target_absent_key)) && !trial.target_present) {
correct = true;
}