@jspsych/plugin-html-keyboard-response 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js +32 -7
- package/dist/index.browser.js.map +1 -1
- package/dist/index.browser.min.js +2 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +31 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +32 -0
- package/dist/index.js +31 -6
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.spec.ts +145 -3
- package/src/index.ts +29 -1
package/dist/index.browser.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
var version = "2.
|
|
4
|
+
var version = "2.2.0";
|
|
5
5
|
|
|
6
6
|
const info = {
|
|
7
7
|
name: "html-keyboard-response",
|
|
@@ -65,6 +65,18 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
|
65
65
|
response_ends_trial: {
|
|
66
66
|
type: jspsych.ParameterType.BOOL,
|
|
67
67
|
default: true
|
|
68
|
+
},
|
|
69
|
+
/**
|
|
70
|
+
* If true, the response is not registered until the participant releases the key. The response
|
|
71
|
+
* time (`rt`) still reflects when the key was pressed, and the additional data field
|
|
72
|
+
* `rt_key_duration` records how long the key was held down. Note that when this is true, the
|
|
73
|
+
* trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
|
|
74
|
+
* the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
|
|
75
|
+
* key is still held, no response is recorded for the trial.
|
|
76
|
+
*/
|
|
77
|
+
wait_for_key_release: {
|
|
78
|
+
type: jspsych.ParameterType.BOOL,
|
|
79
|
+
default: false
|
|
68
80
|
}
|
|
69
81
|
},
|
|
70
82
|
data: {
|
|
@@ -79,6 +91,10 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
|
79
91
|
/** The HTML content that was displayed on the screen. */
|
|
80
92
|
stimulus: {
|
|
81
93
|
type: jspsych.ParameterType.STRING
|
|
94
|
+
},
|
|
95
|
+
/** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
|
|
96
|
+
rt_key_duration: {
|
|
97
|
+
type: jspsych.ParameterType.INT
|
|
82
98
|
}
|
|
83
99
|
},
|
|
84
100
|
// prettier-ignore
|
|
@@ -102,7 +118,8 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
|
102
118
|
display_element.innerHTML = new_html;
|
|
103
119
|
var response = {
|
|
104
120
|
rt: null,
|
|
105
|
-
key: null
|
|
121
|
+
key: null,
|
|
122
|
+
rt_key_duration: null
|
|
106
123
|
};
|
|
107
124
|
const end_trial = () => {
|
|
108
125
|
if (typeof keyboardListener !== "undefined") {
|
|
@@ -111,14 +128,19 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
|
111
128
|
var trial_data = {
|
|
112
129
|
rt: response.rt,
|
|
113
130
|
stimulus: trial.stimulus,
|
|
114
|
-
response: response.key
|
|
131
|
+
response: response.key,
|
|
132
|
+
rt_key_duration: response.rt_key_duration
|
|
115
133
|
};
|
|
116
134
|
this.jsPsych.finishTrial(trial_data);
|
|
117
135
|
};
|
|
118
136
|
var after_response = (info2) => {
|
|
119
137
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className += " responded";
|
|
120
138
|
if (response.key == null) {
|
|
121
|
-
response =
|
|
139
|
+
response = {
|
|
140
|
+
rt: info2.rt,
|
|
141
|
+
key: info2.key,
|
|
142
|
+
rt_key_duration: info2.rt_key_duration ?? null
|
|
143
|
+
};
|
|
122
144
|
}
|
|
123
145
|
if (trial.response_ends_trial) {
|
|
124
146
|
end_trial();
|
|
@@ -130,7 +152,8 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
|
130
152
|
valid_responses: trial.choices,
|
|
131
153
|
rt_method: "performance",
|
|
132
154
|
persist: false,
|
|
133
|
-
allow_held_key: false
|
|
155
|
+
allow_held_key: false,
|
|
156
|
+
wait_for_key_release: trial.wait_for_key_release
|
|
134
157
|
});
|
|
135
158
|
}
|
|
136
159
|
if (trial.stimulus_duration !== null) {
|
|
@@ -157,8 +180,10 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
|
157
180
|
const default_data = {
|
|
158
181
|
stimulus: trial.stimulus,
|
|
159
182
|
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
160
|
-
response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
|
|
183
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
184
|
+
rt_key_duration: null
|
|
161
185
|
};
|
|
186
|
+
default_data.rt_key_duration = default_data.rt === null || !trial.wait_for_key_release ? null : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);
|
|
162
187
|
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
163
188
|
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
164
189
|
return data;
|
|
@@ -181,4 +206,4 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
|
181
206
|
return HtmlKeyboardResponsePlugin;
|
|
182
207
|
|
|
183
208
|
})(jsPsychModule);
|
|
184
|
-
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@2.
|
|
209
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@2.2.0/dist/index.browser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":[],"mappings":";;;EAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ICiFA,SAAA,EAAA;EAAA;;KAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.browser.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.2.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.3.4\",\n \"@jspsych/test-utils\": \"^1.3.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**\n * If true, the response is not registered until the participant releases the key. The response\n * time (`rt`) still reflects when the key was pressed, and the additional data field\n * `rt_key_duration` records how long the key was held down. Note that when this is true, the\n * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at\n * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the\n * key is still held, no response is recorded for the trial.\n */\n wait_for_key_release: {\n type: ParameterType.BOOL,\n default: false,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n /** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */\n rt_key_duration: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n rt_key_duration: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n rt_key_duration: response.rt_key_duration,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = {\n rt: info.rt,\n key: info.key,\n rt_key_duration: info.rt_key_duration ?? null,\n };\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n wait_for_key_release: trial.wait_for_key_release,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n rt_key_duration: null,\n };\n default_data.rt_key_duration =\n default_data.rt === null || !trial.wait_for_key_release\n ? null\n : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":[],"mappings":";;;EAEE,IAAA,OAAA,GAAW,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ICiGA,SAAA,EAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var jsPsychHtmlKeyboardResponse=function(
|
|
2
|
-
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@2.
|
|
1
|
+
var jsPsychHtmlKeyboardResponse=(function(r){"use strict";var y="2.2.0";const p={name:"html-keyboard-response",version:y,parameters:{stimulus:{type:r.ParameterType.HTML_STRING,default:void 0},choices:{type:r.ParameterType.KEYS,default:"ALL_KEYS"},prompt:{type:r.ParameterType.HTML_STRING,default:null},stimulus_duration:{type:r.ParameterType.INT,default:null},trial_duration:{type:r.ParameterType.INT,default:null},response_ends_trial:{type:r.ParameterType.BOOL,default:!0},wait_for_key_release:{type:r.ParameterType.BOOL,default:!1}},data:{response:{type:r.ParameterType.STRING},rt:{type:r.ParameterType.INT},stimulus:{type:r.ParameterType.STRING},rt_key_duration:{type:r.ParameterType.INT}},citations:{apa:"de Leeuw, J. R., Gilbert, R. A., & Luchterhandt, B. (2023). jsPsych: Enabling an Open-Source Collaborative Ecosystem of Behavioral Experiments. Journal of Open Source Software, 8(85), 5351. https://doi.org/10.21105/joss.05351 ",bibtex:'@article{Leeuw2023jsPsych, author = {de Leeuw, Joshua R. and Gilbert, Rebecca A. and Luchterhandt, Bj{\\" o}rn}, journal = {Journal of Open Source Software}, doi = {10.21105/joss.05351}, issn = {2475-9066}, number = {85}, year = {2023}, month = {may 11}, pages = {5351}, publisher = {Open Journals}, title = {jsPsych: Enabling an {Open}-{Source} {Collaborative} {Ecosystem} of {Behavioral} {Experiments}}, url = {https://joss.theoj.org/papers/10.21105/joss.05351}, volume = {8}, } '}};class n{constructor(t){this.jsPsych=t}trial(t,e){var a='<div id="jspsych-html-keyboard-response-stimulus">'+e.stimulus+"</div>";e.prompt!==null&&(a+=e.prompt),t.innerHTML=a;var s={rt:null,key:null,rt_key_duration:null};const l=()=>{typeof u!="undefined"&&this.jsPsych.pluginAPI.cancelKeyboardResponse(u);var i={rt:s.rt,stimulus:e.stimulus,response:s.key,rt_key_duration:s.rt_key_duration};this.jsPsych.finishTrial(i)};var m=i=>{var o;t.querySelector("#jspsych-html-keyboard-response-stimulus").className+=" responded",s.key==null&&(s={rt:i.rt,key:i.key,rt_key_duration:(o=i.rt_key_duration)!=null?o:null}),e.response_ends_trial&&l()};if(e.choices!="NO_KEYS")var u=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:m,valid_responses:e.choices,rt_method:"performance",persist:!1,allow_held_key:!1,wait_for_key_release:e.wait_for_key_release});e.stimulus_duration!==null&&this.jsPsych.pluginAPI.setTimeout(()=>{t.querySelector("#jspsych-html-keyboard-response-stimulus").style.visibility="hidden"},e.stimulus_duration),e.trial_duration!==null&&this.jsPsych.pluginAPI.setTimeout(l,e.trial_duration)}simulate(t,e,a,s){e=="data-only"&&(s(),this.simulate_data_only(t,a)),e=="visual"&&this.simulate_visual(t,a,s)}create_simulation_data(t,e){const a={stimulus:t.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,.006666666666666667,!0),response:this.jsPsych.pluginAPI.getValidKey(t.choices),rt_key_duration:null};a.rt_key_duration=a.rt===null||!t.wait_for_key_release?null:this.jsPsych.randomization.sampleExGaussian(150,30,.01,!0);const s=this.jsPsych.pluginAPI.mergeSimulationData(a,e);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(t,s),s}simulate_data_only(t,e){const a=this.create_simulation_data(t,e);this.jsPsych.finishTrial(a)}simulate_visual(t,e,a){const s=this.create_simulation_data(t,e),l=this.jsPsych.getDisplayElement();this.trial(l,t),a(),s.rt!==null&&this.jsPsych.pluginAPI.pressKey(s.response,s.rt)}}return n.info=p,n})(jsPsychModule);
|
|
2
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@2.2.0/dist/index.browser.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.min.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":["version"],"mappings":"yDAEEA,IAAAA,EAAW,8gBCiFA,UAAA,iuBAAe"}
|
|
1
|
+
{"version":3,"file":"index.browser.min.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.2.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.3.4\",\n \"@jspsych/test-utils\": \"^1.3.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**\n * If true, the response is not registered until the participant releases the key. The response\n * time (`rt`) still reflects when the key was pressed, and the additional data field\n * `rt_key_duration` records how long the key was held down. Note that when this is true, the\n * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at\n * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the\n * key is still held, no response is recorded for the trial.\n */\n wait_for_key_release: {\n type: ParameterType.BOOL,\n default: false,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n /** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */\n rt_key_duration: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n rt_key_duration: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n rt_key_duration: response.rt_key_duration,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = {\n rt: info.rt,\n key: info.key,\n rt_key_duration: info.rt_key_duration ?? null,\n };\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n wait_for_key_release: trial.wait_for_key_release,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n rt_key_duration: null,\n };\n default_data.rt_key_duration =\n default_data.rt === null || !trial.wait_for_key_release\n ? null\n : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":["version","_a"],"mappings":"0DACE,IACAA,EAAW,qnBCiGA,UAAA,iuBAAe,+aAnG5B,IAAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var jspsych = require('jspsych');
|
|
4
4
|
|
|
5
|
-
var version = "2.
|
|
5
|
+
var version = "2.2.0";
|
|
6
6
|
|
|
7
7
|
const info = {
|
|
8
8
|
name: "html-keyboard-response",
|
|
@@ -66,6 +66,18 @@ const info = {
|
|
|
66
66
|
response_ends_trial: {
|
|
67
67
|
type: jspsych.ParameterType.BOOL,
|
|
68
68
|
default: true
|
|
69
|
+
},
|
|
70
|
+
/**
|
|
71
|
+
* If true, the response is not registered until the participant releases the key. The response
|
|
72
|
+
* time (`rt`) still reflects when the key was pressed, and the additional data field
|
|
73
|
+
* `rt_key_duration` records how long the key was held down. Note that when this is true, the
|
|
74
|
+
* trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
|
|
75
|
+
* the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
|
|
76
|
+
* key is still held, no response is recorded for the trial.
|
|
77
|
+
*/
|
|
78
|
+
wait_for_key_release: {
|
|
79
|
+
type: jspsych.ParameterType.BOOL,
|
|
80
|
+
default: false
|
|
69
81
|
}
|
|
70
82
|
},
|
|
71
83
|
data: {
|
|
@@ -80,6 +92,10 @@ const info = {
|
|
|
80
92
|
/** The HTML content that was displayed on the screen. */
|
|
81
93
|
stimulus: {
|
|
82
94
|
type: jspsych.ParameterType.STRING
|
|
95
|
+
},
|
|
96
|
+
/** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
|
|
97
|
+
rt_key_duration: {
|
|
98
|
+
type: jspsych.ParameterType.INT
|
|
83
99
|
}
|
|
84
100
|
},
|
|
85
101
|
// prettier-ignore
|
|
@@ -103,7 +119,8 @@ class HtmlKeyboardResponsePlugin {
|
|
|
103
119
|
display_element.innerHTML = new_html;
|
|
104
120
|
var response = {
|
|
105
121
|
rt: null,
|
|
106
|
-
key: null
|
|
122
|
+
key: null,
|
|
123
|
+
rt_key_duration: null
|
|
107
124
|
};
|
|
108
125
|
const end_trial = () => {
|
|
109
126
|
if (typeof keyboardListener !== "undefined") {
|
|
@@ -112,14 +129,19 @@ class HtmlKeyboardResponsePlugin {
|
|
|
112
129
|
var trial_data = {
|
|
113
130
|
rt: response.rt,
|
|
114
131
|
stimulus: trial.stimulus,
|
|
115
|
-
response: response.key
|
|
132
|
+
response: response.key,
|
|
133
|
+
rt_key_duration: response.rt_key_duration
|
|
116
134
|
};
|
|
117
135
|
this.jsPsych.finishTrial(trial_data);
|
|
118
136
|
};
|
|
119
137
|
var after_response = (info2) => {
|
|
120
138
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className += " responded";
|
|
121
139
|
if (response.key == null) {
|
|
122
|
-
response =
|
|
140
|
+
response = {
|
|
141
|
+
rt: info2.rt,
|
|
142
|
+
key: info2.key,
|
|
143
|
+
rt_key_duration: info2.rt_key_duration ?? null
|
|
144
|
+
};
|
|
123
145
|
}
|
|
124
146
|
if (trial.response_ends_trial) {
|
|
125
147
|
end_trial();
|
|
@@ -131,7 +153,8 @@ class HtmlKeyboardResponsePlugin {
|
|
|
131
153
|
valid_responses: trial.choices,
|
|
132
154
|
rt_method: "performance",
|
|
133
155
|
persist: false,
|
|
134
|
-
allow_held_key: false
|
|
156
|
+
allow_held_key: false,
|
|
157
|
+
wait_for_key_release: trial.wait_for_key_release
|
|
135
158
|
});
|
|
136
159
|
}
|
|
137
160
|
if (trial.stimulus_duration !== null) {
|
|
@@ -158,8 +181,10 @@ class HtmlKeyboardResponsePlugin {
|
|
|
158
181
|
const default_data = {
|
|
159
182
|
stimulus: trial.stimulus,
|
|
160
183
|
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
161
|
-
response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
|
|
184
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
185
|
+
rt_key_duration: null
|
|
162
186
|
};
|
|
187
|
+
default_data.rt_key_duration = default_data.rt === null || !trial.wait_for_key_release ? null : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);
|
|
163
188
|
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
164
189
|
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
165
190
|
return data;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":[],"mappings":";;;;AAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECiFA,SAAA,EAAA;AAAA;;GAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.2.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.3.4\",\n \"@jspsych/test-utils\": \"^1.3.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**\n * If true, the response is not registered until the participant releases the key. The response\n * time (`rt`) still reflects when the key was pressed, and the additional data field\n * `rt_key_duration` records how long the key was held down. Note that when this is true, the\n * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at\n * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the\n * key is still held, no response is recorded for the trial.\n */\n wait_for_key_release: {\n type: ParameterType.BOOL,\n default: false,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n /** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */\n rt_key_duration: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n rt_key_duration: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n rt_key_duration: response.rt_key_duration,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = {\n rt: info.rt,\n key: info.key,\n rt_key_duration: info.rt_key_duration ?? null,\n };\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n wait_for_key_release: trial.wait_for_key_release,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n rt_key_duration: null,\n };\n default_data.rt_key_duration =\n default_data.rt === null || !trial.wait_for_key_release\n ? null\n : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":[],"mappings":";;;;AAEE,IAAA,OAAA,GAAW,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECiGA,SAAA,EAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -63,6 +63,18 @@ declare const info: {
|
|
|
63
63
|
readonly type: ParameterType.BOOL;
|
|
64
64
|
readonly default: true;
|
|
65
65
|
};
|
|
66
|
+
/**
|
|
67
|
+
* If true, the response is not registered until the participant releases the key. The response
|
|
68
|
+
* time (`rt`) still reflects when the key was pressed, and the additional data field
|
|
69
|
+
* `rt_key_duration` records how long the key was held down. Note that when this is true, the
|
|
70
|
+
* trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
|
|
71
|
+
* the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
|
|
72
|
+
* key is still held, no response is recorded for the trial.
|
|
73
|
+
*/
|
|
74
|
+
readonly wait_for_key_release: {
|
|
75
|
+
readonly type: ParameterType.BOOL;
|
|
76
|
+
readonly default: false;
|
|
77
|
+
};
|
|
66
78
|
};
|
|
67
79
|
readonly data: {
|
|
68
80
|
/** Indicates which key the participant pressed. */
|
|
@@ -77,6 +89,10 @@ declare const info: {
|
|
|
77
89
|
readonly stimulus: {
|
|
78
90
|
readonly type: ParameterType.STRING;
|
|
79
91
|
};
|
|
92
|
+
/** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
|
|
93
|
+
readonly rt_key_duration: {
|
|
94
|
+
readonly type: ParameterType.INT;
|
|
95
|
+
};
|
|
80
96
|
};
|
|
81
97
|
readonly citations: "__CITATIONS__";
|
|
82
98
|
};
|
|
@@ -154,6 +170,18 @@ declare class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
154
170
|
readonly type: ParameterType.BOOL;
|
|
155
171
|
readonly default: true;
|
|
156
172
|
};
|
|
173
|
+
/**
|
|
174
|
+
* If true, the response is not registered until the participant releases the key. The response
|
|
175
|
+
* time (`rt`) still reflects when the key was pressed, and the additional data field
|
|
176
|
+
* `rt_key_duration` records how long the key was held down. Note that when this is true, the
|
|
177
|
+
* trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
|
|
178
|
+
* the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
|
|
179
|
+
* key is still held, no response is recorded for the trial.
|
|
180
|
+
*/
|
|
181
|
+
readonly wait_for_key_release: {
|
|
182
|
+
readonly type: ParameterType.BOOL;
|
|
183
|
+
readonly default: false;
|
|
184
|
+
};
|
|
157
185
|
};
|
|
158
186
|
readonly data: {
|
|
159
187
|
/** Indicates which key the participant pressed. */
|
|
@@ -168,6 +196,10 @@ declare class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
168
196
|
readonly stimulus: {
|
|
169
197
|
readonly type: ParameterType.STRING;
|
|
170
198
|
};
|
|
199
|
+
/** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
|
|
200
|
+
readonly rt_key_duration: {
|
|
201
|
+
readonly type: ParameterType.INT;
|
|
202
|
+
};
|
|
171
203
|
};
|
|
172
204
|
readonly citations: "__CITATIONS__";
|
|
173
205
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ParameterType } from 'jspsych';
|
|
2
2
|
|
|
3
|
-
var version = "2.
|
|
3
|
+
var version = "2.2.0";
|
|
4
4
|
|
|
5
5
|
const info = {
|
|
6
6
|
name: "html-keyboard-response",
|
|
@@ -64,6 +64,18 @@ const info = {
|
|
|
64
64
|
response_ends_trial: {
|
|
65
65
|
type: ParameterType.BOOL,
|
|
66
66
|
default: true
|
|
67
|
+
},
|
|
68
|
+
/**
|
|
69
|
+
* If true, the response is not registered until the participant releases the key. The response
|
|
70
|
+
* time (`rt`) still reflects when the key was pressed, and the additional data field
|
|
71
|
+
* `rt_key_duration` records how long the key was held down. Note that when this is true, the
|
|
72
|
+
* trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
|
|
73
|
+
* the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
|
|
74
|
+
* key is still held, no response is recorded for the trial.
|
|
75
|
+
*/
|
|
76
|
+
wait_for_key_release: {
|
|
77
|
+
type: ParameterType.BOOL,
|
|
78
|
+
default: false
|
|
67
79
|
}
|
|
68
80
|
},
|
|
69
81
|
data: {
|
|
@@ -78,6 +90,10 @@ const info = {
|
|
|
78
90
|
/** The HTML content that was displayed on the screen. */
|
|
79
91
|
stimulus: {
|
|
80
92
|
type: ParameterType.STRING
|
|
93
|
+
},
|
|
94
|
+
/** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
|
|
95
|
+
rt_key_duration: {
|
|
96
|
+
type: ParameterType.INT
|
|
81
97
|
}
|
|
82
98
|
},
|
|
83
99
|
// prettier-ignore
|
|
@@ -101,7 +117,8 @@ class HtmlKeyboardResponsePlugin {
|
|
|
101
117
|
display_element.innerHTML = new_html;
|
|
102
118
|
var response = {
|
|
103
119
|
rt: null,
|
|
104
|
-
key: null
|
|
120
|
+
key: null,
|
|
121
|
+
rt_key_duration: null
|
|
105
122
|
};
|
|
106
123
|
const end_trial = () => {
|
|
107
124
|
if (typeof keyboardListener !== "undefined") {
|
|
@@ -110,14 +127,19 @@ class HtmlKeyboardResponsePlugin {
|
|
|
110
127
|
var trial_data = {
|
|
111
128
|
rt: response.rt,
|
|
112
129
|
stimulus: trial.stimulus,
|
|
113
|
-
response: response.key
|
|
130
|
+
response: response.key,
|
|
131
|
+
rt_key_duration: response.rt_key_duration
|
|
114
132
|
};
|
|
115
133
|
this.jsPsych.finishTrial(trial_data);
|
|
116
134
|
};
|
|
117
135
|
var after_response = (info2) => {
|
|
118
136
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className += " responded";
|
|
119
137
|
if (response.key == null) {
|
|
120
|
-
response =
|
|
138
|
+
response = {
|
|
139
|
+
rt: info2.rt,
|
|
140
|
+
key: info2.key,
|
|
141
|
+
rt_key_duration: info2.rt_key_duration ?? null
|
|
142
|
+
};
|
|
121
143
|
}
|
|
122
144
|
if (trial.response_ends_trial) {
|
|
123
145
|
end_trial();
|
|
@@ -129,7 +151,8 @@ class HtmlKeyboardResponsePlugin {
|
|
|
129
151
|
valid_responses: trial.choices,
|
|
130
152
|
rt_method: "performance",
|
|
131
153
|
persist: false,
|
|
132
|
-
allow_held_key: false
|
|
154
|
+
allow_held_key: false,
|
|
155
|
+
wait_for_key_release: trial.wait_for_key_release
|
|
133
156
|
});
|
|
134
157
|
}
|
|
135
158
|
if (trial.stimulus_duration !== null) {
|
|
@@ -156,8 +179,10 @@ class HtmlKeyboardResponsePlugin {
|
|
|
156
179
|
const default_data = {
|
|
157
180
|
stimulus: trial.stimulus,
|
|
158
181
|
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
159
|
-
response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
|
|
182
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
183
|
+
rt_key_duration: null
|
|
160
184
|
};
|
|
185
|
+
default_data.rt_key_duration = default_data.rt === null || !trial.wait_for_key_release ? null : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);
|
|
161
186
|
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
162
187
|
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
163
188
|
return data;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":[],"mappings":";;AAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECiFA,SAAA,EAAA;AAAA;;GAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.2.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.3.4\",\n \"@jspsych/test-utils\": \"^1.3.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**\n * If true, the response is not registered until the participant releases the key. The response\n * time (`rt`) still reflects when the key was pressed, and the additional data field\n * `rt_key_duration` records how long the key was held down. Note that when this is true, the\n * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at\n * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the\n * key is still held, no response is recorded for the trial.\n */\n wait_for_key_release: {\n type: ParameterType.BOOL,\n default: false,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n /** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */\n rt_key_duration: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n rt_key_duration: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n rt_key_duration: response.rt_key_duration,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = {\n rt: info.rt,\n key: info.key,\n rt_key_duration: info.rt_key_duration ?? null,\n };\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n wait_for_key_release: trial.wait_for_key_release,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n rt_key_duration: null,\n };\n default_data.rt_key_duration =\n default_data.rt === null || !trial.wait_for_key_release\n ? null\n : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":[],"mappings":";;AAEE,IAAA,OAAA,GAAW,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECiGA,SAAA,EAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jspsych/plugin-html-keyboard-response",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "jsPsych plugin for displaying a stimulus and getting a keyboard response",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"jspsych": ">=7.1.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@jspsych/config": "^3.
|
|
41
|
-
"@jspsych/test-utils": "^1.
|
|
40
|
+
"@jspsych/config": "^3.3.4",
|
|
41
|
+
"@jspsych/test-utils": "^1.3.0"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/src/index.spec.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
flushPromises,
|
|
3
|
+
keyDown,
|
|
4
|
+
keyUp,
|
|
5
|
+
pressKey,
|
|
6
|
+
simulateTimeline,
|
|
7
|
+
startTimeline,
|
|
8
|
+
} from "@jspsych/test-utils";
|
|
2
9
|
|
|
3
10
|
import htmlKeyboardResponse from ".";
|
|
4
11
|
|
|
@@ -113,7 +120,7 @@ describe("html-keyboard-response", () => {
|
|
|
113
120
|
});
|
|
114
121
|
|
|
115
122
|
test("class should say responded when key is pressed", async () => {
|
|
116
|
-
const { getHTML, expectRunning } = await startTimeline([
|
|
123
|
+
const { getHTML, expectRunning, displayElement } = await startTimeline([
|
|
117
124
|
{
|
|
118
125
|
type: htmlKeyboardResponse,
|
|
119
126
|
stimulus: "this is html",
|
|
@@ -128,7 +135,7 @@ describe("html-keyboard-response", () => {
|
|
|
128
135
|
|
|
129
136
|
await pressKey("f");
|
|
130
137
|
|
|
131
|
-
expect(
|
|
138
|
+
expect(displayElement.querySelector("#jspsych-html-keyboard-response-stimulus").className).toBe(
|
|
132
139
|
" responded"
|
|
133
140
|
);
|
|
134
141
|
|
|
@@ -136,6 +143,141 @@ describe("html-keyboard-response", () => {
|
|
|
136
143
|
});
|
|
137
144
|
});
|
|
138
145
|
|
|
146
|
+
describe("html-keyboard-response wait_for_key_release", () => {
|
|
147
|
+
test("does not end the trial until the key is released and records the hold duration", async () => {
|
|
148
|
+
const { getData, expectRunning, expectFinished } = await startTimeline([
|
|
149
|
+
{
|
|
150
|
+
type: htmlKeyboardResponse,
|
|
151
|
+
stimulus: "this is html",
|
|
152
|
+
choices: ["a"],
|
|
153
|
+
wait_for_key_release: true,
|
|
154
|
+
},
|
|
155
|
+
]);
|
|
156
|
+
|
|
157
|
+
jest.advanceTimersByTime(100);
|
|
158
|
+
await keyDown("a");
|
|
159
|
+
|
|
160
|
+
// the trial should still be running because the key has not been released
|
|
161
|
+
await expectRunning();
|
|
162
|
+
|
|
163
|
+
jest.advanceTimersByTime(250);
|
|
164
|
+
await keyUp("a");
|
|
165
|
+
|
|
166
|
+
await expectFinished();
|
|
167
|
+
|
|
168
|
+
expect(getData().values()[0].rt).toBe(100);
|
|
169
|
+
expect(getData().values()[0].rt_key_duration).toBe(250);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("records the hold duration when the trial ends by duration", async () => {
|
|
173
|
+
const { getData, expectFinished } = await startTimeline([
|
|
174
|
+
{
|
|
175
|
+
type: htmlKeyboardResponse,
|
|
176
|
+
stimulus: "this is html",
|
|
177
|
+
choices: ["a"],
|
|
178
|
+
wait_for_key_release: true,
|
|
179
|
+
response_ends_trial: false,
|
|
180
|
+
trial_duration: 1000,
|
|
181
|
+
},
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
await keyDown("a");
|
|
185
|
+
jest.advanceTimersByTime(250);
|
|
186
|
+
await keyUp("a");
|
|
187
|
+
|
|
188
|
+
jest.advanceTimersByTime(750);
|
|
189
|
+
await expectFinished();
|
|
190
|
+
|
|
191
|
+
expect(getData().values()[0].rt_key_duration).toBe(250);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("records a null response when the trial ends while the key is still held", async () => {
|
|
195
|
+
const { getData, expectFinished } = await startTimeline([
|
|
196
|
+
{
|
|
197
|
+
type: htmlKeyboardResponse,
|
|
198
|
+
stimulus: "this is html",
|
|
199
|
+
choices: ["a"],
|
|
200
|
+
wait_for_key_release: true,
|
|
201
|
+
response_ends_trial: false,
|
|
202
|
+
trial_duration: 1000,
|
|
203
|
+
},
|
|
204
|
+
]);
|
|
205
|
+
|
|
206
|
+
await keyDown("a");
|
|
207
|
+
jest.advanceTimersByTime(1000);
|
|
208
|
+
await expectFinished();
|
|
209
|
+
|
|
210
|
+
expect(getData().values()[0].response).toBe(null);
|
|
211
|
+
expect(getData().values()[0].rt).toBe(null);
|
|
212
|
+
expect(getData().values()[0].rt_key_duration).toBe(null);
|
|
213
|
+
|
|
214
|
+
await keyUp("a");
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("rt_key_duration is null on default trials (wait_for_key_release false)", async () => {
|
|
218
|
+
const { getData, expectFinished } = await startTimeline([
|
|
219
|
+
{
|
|
220
|
+
type: htmlKeyboardResponse,
|
|
221
|
+
stimulus: "this is html",
|
|
222
|
+
choices: ["a"],
|
|
223
|
+
},
|
|
224
|
+
]);
|
|
225
|
+
|
|
226
|
+
await keyDown("a");
|
|
227
|
+
await expectFinished();
|
|
228
|
+
|
|
229
|
+
expect(getData().values()[0].rt_key_duration).toBe(null);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("a key held past trial_duration does not contaminate the next trial", async () => {
|
|
233
|
+
const { getData, expectRunning, expectFinished } = await startTimeline([
|
|
234
|
+
{
|
|
235
|
+
type: htmlKeyboardResponse,
|
|
236
|
+
stimulus: "trial 1",
|
|
237
|
+
choices: ["a"],
|
|
238
|
+
wait_for_key_release: true,
|
|
239
|
+
trial_duration: 1000,
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
type: htmlKeyboardResponse,
|
|
243
|
+
stimulus: "trial 2",
|
|
244
|
+
choices: ["a"],
|
|
245
|
+
wait_for_key_release: true,
|
|
246
|
+
},
|
|
247
|
+
]);
|
|
248
|
+
|
|
249
|
+
// trial 1: press and hold the key, then let the trial time out
|
|
250
|
+
await keyDown("a");
|
|
251
|
+
jest.advanceTimersByTime(1000);
|
|
252
|
+
await flushPromises();
|
|
253
|
+
|
|
254
|
+
// trial 1 ended by duration with no response because the key was never released
|
|
255
|
+
expect(getData().values()[0].response).toBe(null);
|
|
256
|
+
expect(getData().values()[0].rt).toBe(null);
|
|
257
|
+
expect(getData().values()[0].rt_key_duration).toBe(null);
|
|
258
|
+
|
|
259
|
+
// trial 2 is now running with the key still held; a key-repeat must not register
|
|
260
|
+
await keyDown("a");
|
|
261
|
+
await expectRunning();
|
|
262
|
+
|
|
263
|
+
// releasing the held-over key must not register a response in trial 2
|
|
264
|
+
await keyUp("a");
|
|
265
|
+
await expectRunning();
|
|
266
|
+
|
|
267
|
+
// a fresh press/release cycle in trial 2 works normally
|
|
268
|
+
jest.advanceTimersByTime(200);
|
|
269
|
+
await keyDown("a");
|
|
270
|
+
jest.advanceTimersByTime(125);
|
|
271
|
+
await keyUp("a");
|
|
272
|
+
|
|
273
|
+
await expectFinished();
|
|
274
|
+
|
|
275
|
+
expect(getData().values()[1].response).toBe("a");
|
|
276
|
+
expect(getData().values()[1].rt).toBe(200);
|
|
277
|
+
expect(getData().values()[1].rt_key_duration).toBe(125);
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
|
|
139
281
|
describe("html-keyboard-response simulation", () => {
|
|
140
282
|
test("data mode works", async () => {
|
|
141
283
|
const timeline = [
|
package/src/index.ts
CHANGED
|
@@ -65,6 +65,18 @@ const info = <const>{
|
|
|
65
65
|
type: ParameterType.BOOL,
|
|
66
66
|
default: true,
|
|
67
67
|
},
|
|
68
|
+
/**
|
|
69
|
+
* If true, the response is not registered until the participant releases the key. The response
|
|
70
|
+
* time (`rt`) still reflects when the key was pressed, and the additional data field
|
|
71
|
+
* `rt_key_duration` records how long the key was held down. Note that when this is true, the
|
|
72
|
+
* trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
|
|
73
|
+
* the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
|
|
74
|
+
* key is still held, no response is recorded for the trial.
|
|
75
|
+
*/
|
|
76
|
+
wait_for_key_release: {
|
|
77
|
+
type: ParameterType.BOOL,
|
|
78
|
+
default: false,
|
|
79
|
+
},
|
|
68
80
|
},
|
|
69
81
|
data: {
|
|
70
82
|
/** Indicates which key the participant pressed. */
|
|
@@ -79,6 +91,10 @@ const info = <const>{
|
|
|
79
91
|
stimulus: {
|
|
80
92
|
type: ParameterType.STRING,
|
|
81
93
|
},
|
|
94
|
+
/** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
|
|
95
|
+
rt_key_duration: {
|
|
96
|
+
type: ParameterType.INT,
|
|
97
|
+
},
|
|
82
98
|
},
|
|
83
99
|
// prettier-ignore
|
|
84
100
|
citations: '__CITATIONS__',
|
|
@@ -113,6 +129,7 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
113
129
|
var response = {
|
|
114
130
|
rt: null,
|
|
115
131
|
key: null,
|
|
132
|
+
rt_key_duration: null,
|
|
116
133
|
};
|
|
117
134
|
|
|
118
135
|
// function to end trial when it is time
|
|
@@ -127,6 +144,7 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
127
144
|
rt: response.rt,
|
|
128
145
|
stimulus: trial.stimulus,
|
|
129
146
|
response: response.key,
|
|
147
|
+
rt_key_duration: response.rt_key_duration,
|
|
130
148
|
};
|
|
131
149
|
|
|
132
150
|
// move on to the next trial
|
|
@@ -142,7 +160,11 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
142
160
|
|
|
143
161
|
// only record the first response
|
|
144
162
|
if (response.key == null) {
|
|
145
|
-
response =
|
|
163
|
+
response = {
|
|
164
|
+
rt: info.rt,
|
|
165
|
+
key: info.key,
|
|
166
|
+
rt_key_duration: info.rt_key_duration ?? null,
|
|
167
|
+
};
|
|
146
168
|
}
|
|
147
169
|
|
|
148
170
|
if (trial.response_ends_trial) {
|
|
@@ -158,6 +180,7 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
158
180
|
rt_method: "performance",
|
|
159
181
|
persist: false,
|
|
160
182
|
allow_held_key: false,
|
|
183
|
+
wait_for_key_release: trial.wait_for_key_release,
|
|
161
184
|
});
|
|
162
185
|
}
|
|
163
186
|
|
|
@@ -196,7 +219,12 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
196
219
|
stimulus: trial.stimulus,
|
|
197
220
|
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
198
221
|
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
222
|
+
rt_key_duration: null,
|
|
199
223
|
};
|
|
224
|
+
default_data.rt_key_duration =
|
|
225
|
+
default_data.rt === null || !trial.wait_for_key_release
|
|
226
|
+
? null
|
|
227
|
+
: this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);
|
|
200
228
|
|
|
201
229
|
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
202
230
|
|