diff --git a/docs/markdown_docs/plugins/jspsych-animation.md b/docs/markdown_docs/plugins/jspsych-animation.md index 790b8ad7..83df6576 100644 --- a/docs/markdown_docs/plugins/jspsych-animation.md +++ b/docs/markdown_docs/plugins/jspsych-animation.md @@ -12,7 +12,7 @@ stimuli | array | *undefined* | Each element of the array is a path to an image frame_time | numeric | 250 | How long to display each image (in milliseconds). frame_isi | numeric | 0 | If greater than 0, then a gap will be shown between each image in the sequence. This parameter specifies the length of the gap. sequence_reps | numeric | 1 | How many times to show the entire sequence. There will be no gap (other than the gap specified by `frame_isi`) between repetitions. -choices | array | [ ] | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of an empty array means that all keys will be accepted as valid responses. +choices | array of keycodes | `jsPsych.ALL_KEYS` | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of `jsPsych.ALL_KEYS` means that all keys will be accepted as valid responses. Specifying `jsPsych.NO_KEYS` will mean that no responses are allowed. prompt | string | "" | This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can be used to provide a reminder about the action the subject is supposed to take (e.g. which key to press). diff --git a/docs/markdown_docs/plugins/jspsych-categorize-animation.md b/docs/markdown_docs/plugins/jspsych-categorize-animation.md index 7ff15b92..24365c26 100644 --- a/docs/markdown_docs/plugins/jspsych-categorize-animation.md +++ b/docs/markdown_docs/plugins/jspsych-categorize-animation.md @@ -10,7 +10,7 @@ Parameter | Type | Default Value | Description ----------|------|---------------|------------ stimuli | array | *undefined* | Each element of the array is a path to an image file. key_answer | numeric | *undefined* | A [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) indicating the correct response. -choices | array | *undefined* | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). +choices | array of keycodes | `jsPsych.ALL_KEYS` | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of `jsPsych.ALL_KEYS` means that all keys will be accepted as valid responses. Specifying `jsPsych.NO_KEYS` will mean that no responses are allowed. text_answer | string | "" | A text label that describes the correct answer. Used in conjunction with the `correct_text` and `incorrect_text` parameters. correct_text | string | "Correct." | String to show when the correct answer is given. Can contain HTML formatting. The special string `%ANS%` can be used within the string. If present, the plugin will put the `text_answer` for the trial in place of the %ANS% string (see example below). incorrect_text | string | "Wrong." | String to show when the wrong answer is given. Can contain HTML formatting. The special string `%ANS%` can be used within the string. If present, the plugin will put the `text_answer` for the trial in place of the %ANS% string (see example below). diff --git a/docs/markdown_docs/plugins/jspsych-categorize.md b/docs/markdown_docs/plugins/jspsych-categorize.md index 9105108c..f9fc6ae7 100644 --- a/docs/markdown_docs/plugins/jspsych-categorize.md +++ b/docs/markdown_docs/plugins/jspsych-categorize.md @@ -11,7 +11,7 @@ Parameter | Type | Default Value | Description stimulus | string | *undefined* | The stimulus to display. Either HTML-formatted, or the path to an image. is_html | boolean | false | If `stimulus` is an HTML-formatted string, this parameter needs to be set to `true`. key_answer | numeric | *undefined* | The [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) indicating the correct response. -choices | array | *undefined* | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). +choices | array of keycodes | `jsPsych.ALL_KEYS` | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of `jsPsych.ALL_KEYS` means that all keys will be accepted as valid responses. Specifying `jsPsych.NO_KEYS` will mean that no responses are allowed. text_answer | string | "" | A label that is associated with the correct answer. Used in conjunction with the `correct_text` and `incorrect_text` parameters. correct_text | string | "Correct." | String to show when the correct answer is given. Can contain HTML formatting. The special string `%ANS%` can be used within the string. If present, the plugin will put the `text_answer` for the trial in place of the %ANS% string (see example below). incorrect_text | string | "Wrong." | String to show when the wrong answer is given. Can contain HTML formatting. The special string `%ANS%` can be used within the string. If present, the plugin will put the `text_answer` for the trial in place of the %ANS% string (see example below). diff --git a/docs/markdown_docs/plugins/jspsych-single-audio.md b/docs/markdown_docs/plugins/jspsych-single-audio.md index 49a4defc..ff5e59d5 100644 --- a/docs/markdown_docs/plugins/jspsych-single-audio.md +++ b/docs/markdown_docs/plugins/jspsych-single-audio.md @@ -13,7 +13,7 @@ This table lists the parameters associated with this plugin. Parameters with a d Parameter | Type | Default Value | Description ----------|------|---------------|------------ stimulus | string | *undefined* | Path to an audio file. -choices | array | [ ] | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of an empty array means that all keys will be accepted as valid responses. +choices | array of keycodes | `jsPsych.ALL_KEYS` | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of `jsPsych.ALL_KEYS` means that all keys will be accepted as valid responses. Specifying `jsPsych.NO_KEYS` will mean that no responses are allowed. prompt | string | "" | This string can contain HTML markup. Any content here will be displayed on the screen. The intention is that it can be used to provide a reminder about the action the subject is supposed to take (e.g. which key to press). timing_response | numeric | -1 | How long to wait for the subject to make a response before ending the trial in milliseconds. If the subject fails to make a response before this timer is reached, the the subject's response will be recorded as -1 for the trial and the trial will end. If the value of this parameter is -1, then the trial will wait for a response indefinitely. response_ends_trial | boolean | true | If true, then the trial will end whenever the subject makes a response (assuming they make their response before the cutoff specified by the `timing_response` parameter). If false, then the trial will continue until the value for `timing_response` is reached. You can use this parameter to force the subject to view a stimulus for a fixed amount of time, even if they respond before the time is complete. diff --git a/docs/markdown_docs/plugins/jspsych-single-stim.md b/docs/markdown_docs/plugins/jspsych-single-stim.md index 5262dc4f..59ae6e33 100644 --- a/docs/markdown_docs/plugins/jspsych-single-stim.md +++ b/docs/markdown_docs/plugins/jspsych-single-stim.md @@ -12,7 +12,7 @@ Parameter | Type | Default Value | Description ----------|------|---------------|------------ stimulus | string | *undefined* | The stimulus to display. Either HTML-formatted, or the path to an image. is_html | boolean | false | If `stimulus` is an HTML-formatted string, this parameter needs to be set to `true`. -choices | array | [ ] | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of an empty array means that all keys will be accepted as valid responses. +choices | array of keycodes | `jsPsych.ALL_KEYS` | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of `jsPsych.ALL_KEYS` means that all keys will be accepted as valid responses. Specifying `jsPsych.NO_KEYS` will mean that no responses are allowed. prompt | string | "" | This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can be used to provide a reminder about the action the subject is supposed to take (e.g. which key to press). timing_stim | numeric | -1 | How long to show the stimulus for in milliseconds. If the value is -1, then the stimulus will be shown until the subject makes a response. timing_response | numeric | -1 | How long to wait for the subject to make a response before ending the trial in milliseconds. If the subject fails to make a response before this timer is reached, the the subject's response will be recorded as -1 for the trial and the trial will end. If the value of this parameter is -1, then the trial will wait for a response indefinitely. diff --git a/docs/markdown_docs/plugins/jspsych-vsl-animate-occlusion.md b/docs/markdown_docs/plugins/jspsych-vsl-animate-occlusion.md index 9235d7aa..88535cf9 100644 --- a/docs/markdown_docs/plugins/jspsych-vsl-animate-occlusion.md +++ b/docs/markdown_docs/plugins/jspsych-vsl-animate-occlusion.md @@ -19,7 +19,7 @@ canvas_size | array | `[400, 400]` | Array specifying the width and height of th image_size | array | `[100, 100]` | Array specifying the width and height of the images to show. The occluding rectangle will have a width equal to the width of image_size. initial_direction | string | "left" | Which direction the stimulus should move first (subsequent directions will alternate). Choices are "left" or "right". occlude_center | boolean | true | If true, display a rectangle in the center of the screen that is just wide enough to occlude the image completely as it passes behind. -choices | array | [ ] | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of an empty array means that all keys will be accepted as valid responses. +choices | array of keycodes | `jsPsych.ALL_KEYS` | This array contains the keys that the subject is allowed to press in order to respond to the stimulus. Keys can be specified as their [numeric key code](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) or as characters (e.g. `'a'`, `'q'`). The default value of `jsPsych.ALL_KEYS` means that all keys will be accepted as valid responses. Specifying `jsPsych.NO_KEYS` will mean that no responses are allowed. timing_cycle | numeric | 1000 | How long it takes for a stimulus in the sequence to make a complete cycle (move to the edge and back to the center) in milliseconds. timing_pre_movement | numeric | 500 | How long to wait before the stimuli starts moving from behind the center rectangle. diff --git a/jspsych.js b/jspsych.js index b728e481..0af163d6 100755 --- a/jspsych.js +++ b/jspsych.js @@ -30,6 +30,10 @@ var jsPsych = (function() { // done loading? var loaded = false; + // enumerated variables for special parameter types + core.ALL_KEYS = 'allkeys'; + core.NO_KEYS = 'none'; + // // public methods // @@ -1400,20 +1404,23 @@ jsPsych.pluginAPI = (function() { } var valid_response = false; - if (typeof parameters.valid_responses === 'undefined' || parameters.valid_responses.length === 0) { + if (typeof parameters.valid_responses === 'undefined' || parameters.valid_responses == jsPsych.ALL_KEYS) { valid_response = true; - } - for (var i = 0; i < parameters.valid_responses.length; i++) { - if (typeof parameters.valid_responses[i] == 'string') { - if (typeof keylookup[parameters.valid_responses[i]] !== 'undefined') { - if (e.which == keylookup[parameters.valid_responses[i]]) { + } else { + if(parameters.valid_responses != jsPsych.NO_KEYS){ + for (var i = 0; i < parameters.valid_responses.length; i++) { + if (typeof parameters.valid_responses[i] == 'string') { + if (typeof keylookup[parameters.valid_responses[i]] !== 'undefined') { + if (e.which == keylookup[parameters.valid_responses[i]]) { + valid_response = true; + } + } else { + throw new Error('Invalid key string specified for getKeyboardResponse'); + } + } else if (e.which == parameters.valid_responses[i]) { valid_response = true; } - } else { - throw new Error('Invalid key string specified for getKeyboardResponse'); } - } else if (e.which == parameters.valid_responses[i]) { - valid_response = true; } } // check if key was already held down diff --git a/plugins/jspsych-animation.js b/plugins/jspsych-animation.js index 6772e958..50acc6af 100644 --- a/plugins/jspsych-animation.js +++ b/plugins/jspsych-animation.js @@ -42,7 +42,7 @@ jsPsych.plugins.animation = (function() { }, choices: { type: [jsPsych.plugins.parameterType.KEYCODE], - default: [], + default: jsPsych.ALL_KEYS, no_function: false, array: true, description: '' @@ -61,7 +61,7 @@ jsPsych.plugins.animation = (function() { trial.frame_time = trial.frame_time || 250; trial.frame_isi = trial.frame_isi || 0; trial.sequence_reps = trial.sequence_reps || 1; - trial.choices = trial.choices || []; + trial.choices = trial.choices || jsPsych.ALL_KEYS; trial.prompt = (typeof trial.prompt === 'undefined') ? "" : trial.prompt; // if any trial variables are functions diff --git a/plugins/jspsych-categorize-animation.js b/plugins/jspsych-categorize-animation.js index 3daef053..64cc53ec 100644 --- a/plugins/jspsych-categorize-animation.js +++ b/plugins/jspsych-categorize-animation.js @@ -30,7 +30,7 @@ jsPsych.plugins["categorize-animation"] = (function() { }, choices: { type: [jsPsych.plugins.parameterType.KEYCODE], - default: [], + default: jsPsych.ALL_KEYS, no_function: false, array: true, description: '' @@ -89,6 +89,7 @@ jsPsych.plugins["categorize-animation"] = (function() { plugin.trial = function(display_element, trial) { // set default values + trial.choices = trial.choices || jsPsych.ALL_KEYS; trial.sequence_reps = trial.sequence_reps || 1; trial.key_answer = trial.key_answer; trial.text_answer = (typeof trial.text_answer === 'undefined') ? "" : trial.text_answer; diff --git a/plugins/jspsych-categorize.js b/plugins/jspsych-categorize.js index 771f2fec..1de7decc 100644 --- a/plugins/jspsych-categorize.js +++ b/plugins/jspsych-categorize.js @@ -36,7 +36,7 @@ jsPsych.plugins.categorize = (function() { }, choices: { type: [jsPsych.plugins.parameterType.KEYCODE], - default: [], + default: jsPsych.ALL_KEYS, array: true, no_function: false, description: '' @@ -113,6 +113,7 @@ jsPsych.plugins.categorize = (function() { plugin.trial = function(display_element, trial) { // default parameters + trial.choices = triaul.choices || jsPsych.ALL_KEYS; trial.text_answer = (typeof trial.text_answer === 'undefined') ? "" : trial.text_answer; trial.correct_text = (typeof trial.correct_text === 'undefined') ? "

Correct

" : trial.correct_text; trial.incorrect_text = (typeof trial.incorrect_text === 'undefined') ? "

Incorrect

" : trial.incorrect_text; diff --git a/plugins/jspsych-single-audio.js b/plugins/jspsych-single-audio.js index f13968b5..de11190f 100644 --- a/plugins/jspsych-single-audio.js +++ b/plugins/jspsych-single-audio.js @@ -27,7 +27,7 @@ jsPsych.plugins["single-audio"] = (function() { choices: { type: [jsPsych.plugins.parameterType.KEYCODE], array: true - default: [], + default: jsPsych.ALL_KEYS, no_function: false, description: '' }, @@ -61,7 +61,7 @@ jsPsych.plugins["single-audio"] = (function() { plugin.trial = function(display_element, trial) { // default parameters - trial.choices = trial.choices || []; + trial.choices = trial.choices || jsPsych.ALL_KEYS; trial.response_ends_trial = (typeof trial.response_ends_trial === 'undefined') ? true : trial.response_ends_trial; trial.trial_ends_after_audio = (typeof trial.trial_ends_after_audio === 'undefined') ? false : trial.trial_ends_after_audio; trial.timing_response = trial.timing_response || -1; // if -1, then wait for response forever diff --git a/plugins/jspsych-single-stim.js b/plugins/jspsych-single-stim.js index 9aff8c18..3935cfe6 100644 --- a/plugins/jspsych-single-stim.js +++ b/plugins/jspsych-single-stim.js @@ -34,7 +34,7 @@ jsPsych.plugins["single-stim"] = (function() { choices: { type: [jsPsych.plugins.parameterType.KEYCODE], array: true, - default: [], + default: jsPsych.ALL_KEYS, no_function: false, description: '' }, @@ -74,7 +74,7 @@ jsPsych.plugins["single-stim"] = (function() { trial = jsPsych.pluginAPI.evaluateFunctionParameters(trial); // set default values for the parameters - trial.choices = trial.choices || []; + trial.choices = trial.choices || jsPsych.ALL_KEYS; trial.response_ends_trial = (typeof trial.response_ends_trial == 'undefined') ? true : trial.response_ends_trial; trial.timing_stim = trial.timing_stim || -1; trial.timing_response = trial.timing_response || -1; diff --git a/plugins/jspsych-vsl-animate-occlusion.js b/plugins/jspsych-vsl-animate-occlusion.js index 04cd2150..461d1851 100644 --- a/plugins/jspsych-vsl-animate-occlusion.js +++ b/plugins/jspsych-vsl-animate-occlusion.js @@ -31,7 +31,7 @@ jsPsych.plugins['vsl-animate-occlusion'] = (function() { choices: { type: [jsPsych.plugins.parameterType.KEYCODE], array: true, - default: [], + default: jsPsych.ALL_KEYS, no_function: false, description: '' }, @@ -85,7 +85,7 @@ jsPsych.plugins['vsl-animate-occlusion'] = (function() { trial.image_size = trial.image_size || [100, 100]; trial.initial_direction = trial.initial_direction || "left"; trial.occlude_center = (typeof trial.occlude_center === 'undefined') ? true : trial.occlude_center; - trial.choices = trial.choices || []; + trial.choices = trial.choices || jsPsych.ALL_KEYS; trial.timing_pre_movement = (typeof trial.timing_pre_movement === 'undefined') ? 500 : trial.timing_pre_movement; // if any trial variables are functions diff --git a/tests&examples/all-keys-no-keys.html b/tests&examples/all-keys-no-keys.html new file mode 100644 index 00000000..1cb0d017 --- /dev/null +++ b/tests&examples/all-keys-no-keys.html @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + +