@jspsych/plugin-html-keyboard-response 1.0.0 → 1.1.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 +35 -5
- package/dist/index.browser.js.map +1 -1
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +35 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +35 -5
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/index.spec.ts +44 -1
- package/src/index.ts +51 -5
package/dist/index.browser.js
CHANGED
|
@@ -99,7 +99,7 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
|
99
99
|
this.jsPsych.finishTrial(trial_data);
|
|
100
100
|
};
|
|
101
101
|
// function to handle responses by the subject
|
|
102
|
-
var after_response =
|
|
102
|
+
var after_response = (info) => {
|
|
103
103
|
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
104
104
|
// which can be used to provide visual feedback that a response was recorded
|
|
105
105
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className +=
|
|
@@ -124,15 +124,45 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
|
|
|
124
124
|
}
|
|
125
125
|
// hide stimulus if stimulus_duration is set
|
|
126
126
|
if (trial.stimulus_duration !== null) {
|
|
127
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
127
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
128
128
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").style.visibility = "hidden";
|
|
129
129
|
}, trial.stimulus_duration);
|
|
130
130
|
}
|
|
131
131
|
// end trial if trial_duration is set
|
|
132
132
|
if (trial.trial_duration !== null) {
|
|
133
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
134
|
-
|
|
135
|
-
|
|
133
|
+
this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
137
|
+
if (simulation_mode == "data-only") {
|
|
138
|
+
load_callback();
|
|
139
|
+
this.simulate_data_only(trial, simulation_options);
|
|
140
|
+
}
|
|
141
|
+
if (simulation_mode == "visual") {
|
|
142
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
create_simulation_data(trial, simulation_options) {
|
|
146
|
+
const default_data = {
|
|
147
|
+
stimulus: trial.stimulus,
|
|
148
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
149
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
150
|
+
};
|
|
151
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
152
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
153
|
+
return data;
|
|
154
|
+
}
|
|
155
|
+
simulate_data_only(trial, simulation_options) {
|
|
156
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
157
|
+
this.jsPsych.finishTrial(data);
|
|
158
|
+
}
|
|
159
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
160
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
161
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
162
|
+
this.trial(display_element, trial);
|
|
163
|
+
load_callback();
|
|
164
|
+
if (data.rt !== null) {
|
|
165
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
136
166
|
}
|
|
137
167
|
}
|
|
138
168
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\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 any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\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 // clear the display\n display_element.innerHTML = \"\";\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 =
|
|
1
|
+
{"version":3,"file":"index.browser.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\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 any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\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 // clear the display\n display_element.innerHTML = \"\";\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":["ParameterType"],"mappings":";;;EAEA,MAAM,IAAI,GAAU;MAClB,IAAI,EAAE,wBAAwB;MAC9B,UAAU,EAAE;;;;UAIV,QAAQ,EAAE;cACR,IAAI,EAAEA,qBAAa,CAAC,WAAW;cAC/B,WAAW,EAAE,UAAU;cACvB,OAAO,EAAE,SAAS;WACnB;;;;UAID,OAAO,EAAE;cACP,IAAI,EAAEA,qBAAa,CAAC,IAAI;cACxB,WAAW,EAAE,SAAS;cACtB,OAAO,EAAE,UAAU;WACpB;;;;UAID,MAAM,EAAE;cACN,IAAI,EAAEA,qBAAa,CAAC,WAAW;cAC/B,WAAW,EAAE,QAAQ;cACrB,OAAO,EAAE,IAAI;WACd;;;;UAID,iBAAiB,EAAE;cACjB,IAAI,EAAEA,qBAAa,CAAC,GAAG;cACvB,WAAW,EAAE,mBAAmB;cAChC,OAAO,EAAE,IAAI;WACd;;;;UAID,cAAc,EAAE;cACd,IAAI,EAAEA,qBAAa,CAAC,GAAG;cACvB,WAAW,EAAE,gBAAgB;cAC7B,OAAO,EAAE,IAAI;WACd;;;;UAID,mBAAmB,EAAE;cACnB,I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function(s){"use strict";const t={name:"html-keyboard-response",parameters:{stimulus:{type:s.ParameterType.HTML_STRING,pretty_name:"Stimulus",default:void 0},choices:{type:s.ParameterType.KEYS,pretty_name:"Choices",default:"ALL_KEYS"},prompt:{type:s.ParameterType.HTML_STRING,pretty_name:"Prompt",default:null},stimulus_duration:{type:s.ParameterType.INT,pretty_name:"Stimulus duration",default:null},trial_duration:{type:s.ParameterType.INT,pretty_name:"Trial duration",default:null},response_ends_trial:{type:s.ParameterType.BOOL,pretty_name:"Response ends trial",default:!0}}};class e{constructor(s){this.jsPsych=s}trial(s,t){var e='<div id="jspsych-html-keyboard-response-stimulus">'+t.stimulus+"</div>";null!==t.prompt&&(e+=t.prompt),s.innerHTML=e;var i={rt:null,key:null};const a=()=>{this.jsPsych.pluginAPI.clearAllTimeouts(),void 0!==l&&this.jsPsych.pluginAPI.cancelKeyboardResponse(l);var e={rt:i.rt,stimulus:t.stimulus,response:i.key};s.innerHTML="",this.jsPsych.finishTrial(e)};if("NO_KEYS"!=t.choices)var l=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:e=>{s.querySelector("#jspsych-html-keyboard-response-stimulus").className+=" responded",null==i.key&&(i=e),t.response_ends_trial&&a()},valid_responses:t.choices,rt_method:"performance",persist:!1,allow_held_key:!1});null!==t.stimulus_duration&&this.jsPsych.pluginAPI.setTimeout((()=>{s.querySelector("#jspsych-html-keyboard-response-stimulus").style.visibility="hidden"}),t.stimulus_duration),null!==t.trial_duration&&this.jsPsych.pluginAPI.setTimeout(a,t.trial_duration)}simulate(s,t,e,i){"data-only"==t&&(i(),this.simulate_data_only(s,e)),"visual"==t&&this.simulate_visual(s,e,i)}create_simulation_data(s,t){const e={stimulus:s.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,1/150,!0),response:this.jsPsych.pluginAPI.getValidKey(s.choices)},i=this.jsPsych.pluginAPI.mergeSimulationData(e,t);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(s,i),i}simulate_data_only(s,t){const e=this.create_simulation_data(s,t);this.jsPsych.finishTrial(e)}simulate_visual(s,t,e){const i=this.create_simulation_data(s,t),a=this.jsPsych.getDisplayElement();this.trial(a,s),e(),null!==i.rt&&this.jsPsych.pluginAPI.pressKey(i.response,i.rt)}}return e.info=t,e}(jsPsychModule);
|
|
2
2
|
//# sourceMappingURL=index.browser.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.min.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\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 any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\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 // clear the display\n display_element.innerHTML = \"\";\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 =
|
|
1
|
+
{"version":3,"file":"index.browser.min.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\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 any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\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 // clear the display\n display_element.innerHTML = \"\";\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":["info","name","parameters","stimulus","type","ParameterType","HTML_STRING","pretty_name","default","undefined","choices","KEYS","prompt","stimulus_duration","INT","trial_duration","response_ends_trial","BOOL","HtmlKeyboardResponsePlugin","constructor","jsPsych","this","trial","display_element","new_html","innerHTML","response","rt","key","end_trial","pluginAPI","clearAllTimeouts","keyboardListener","cancelKeyboardResponse","trial_data","finishTrial","getKeyboardResponse","callback_function","querySelector","className","valid_responses","rt_method","persist","allow_held_key","setTimeout","style","visibility","simulate","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","create_simulation_data","default_data","randomization","sampleExGaussian","getValidKey","data","mergeSimulationData","ensureSimulationDataConsistency","getDisplayElement","pressKey"],"mappings":"yDAEA,MAAMA,EAAc,CAClBC,KAAM,yBACNC,WAAY,CAIVC,SAAU,CACRC,KAAMC,gBAAcC,YACpBC,YAAa,WACbC,aAASC,GAKXC,QAAS,CACPN,KAAMC,gBAAcM,KACpBJ,YAAa,UACbC,QAAS,YAKXI,OAAQ,CACNR,KAAMC,gBAAcC,YACpBC,YAAa,SACbC,QAAS,MAKXK,kBAAmB,CACjBT,KAAMC,gBAAcS,IACpBP,YAAa,oBACbC,QAAS,MAKXO,eAAgB,CACdX,KAAMC,gBAAcS,IACpBP,YAAa,iBACbC,QAAS,MAKXQ,oBAAqB,CACnBZ,KAAMC,gBAAcY,KACpBV,YAAa,sBACbC,SAAS,KAef,MAAMU,EAGJC,YAAoBC,GAAAC,aAAAD,EAEpBE,MAAMC,EAA8BD,GAClC,IAAIE,EAAW,qDAAuDF,EAAMnB,SAAW,SAGlE,OAAjBmB,EAAMV,SACRY,GAAYF,EAAMV,QAIpBW,EAAgBE,UAAYD,EAG5B,IAAIE,EAAW,CACbC,GAAI,KACJC,IAAK,MAIP,MAAMC,EAAY,KAEhBR,KAAKD,QAAQU,UAAUC,wBAGS,IAArBC,GACTX,KAAKD,QAAQU,UAAUG,uBAAuBD,GAIhD,IAAIE,EAAa,CACfP,GAAID,EAASC,GACbxB,SAAUmB,EAAMnB,SAChBuB,SAAUA,EAASE,KAIrBL,EAAgBE,UAAY,GAG5BJ,KAAKD,QAAQe,YAAYD,IAqB3B,GAAqB,WAAjBZ,EAAMZ,QACR,IAAIsB,EAAmBX,KAAKD,QAAQU,UAAUM,oBAAoB,CAChEC,kBAnBkBrC,IAGpBuB,EAAgBe,cAAc,4CAA4CC,WACxE,aAGkB,MAAh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100
100
|
this.jsPsych.finishTrial(trial_data);
|
|
101
101
|
};
|
|
102
102
|
// function to handle responses by the subject
|
|
103
|
-
var after_response =
|
|
103
|
+
var after_response = (info) => {
|
|
104
104
|
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
105
105
|
// which can be used to provide visual feedback that a response was recorded
|
|
106
106
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className +=
|
|
@@ -125,15 +125,45 @@ class HtmlKeyboardResponsePlugin {
|
|
|
125
125
|
}
|
|
126
126
|
// hide stimulus if stimulus_duration is set
|
|
127
127
|
if (trial.stimulus_duration !== null) {
|
|
128
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
128
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
129
129
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").style.visibility = "hidden";
|
|
130
130
|
}, trial.stimulus_duration);
|
|
131
131
|
}
|
|
132
132
|
// end trial if trial_duration is set
|
|
133
133
|
if (trial.trial_duration !== null) {
|
|
134
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
135
|
-
|
|
136
|
-
|
|
134
|
+
this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
138
|
+
if (simulation_mode == "data-only") {
|
|
139
|
+
load_callback();
|
|
140
|
+
this.simulate_data_only(trial, simulation_options);
|
|
141
|
+
}
|
|
142
|
+
if (simulation_mode == "visual") {
|
|
143
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
create_simulation_data(trial, simulation_options) {
|
|
147
|
+
const default_data = {
|
|
148
|
+
stimulus: trial.stimulus,
|
|
149
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
150
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
151
|
+
};
|
|
152
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
153
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
154
|
+
return data;
|
|
155
|
+
}
|
|
156
|
+
simulate_data_only(trial, simulation_options) {
|
|
157
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
158
|
+
this.jsPsych.finishTrial(data);
|
|
159
|
+
}
|
|
160
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
161
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
162
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
163
|
+
this.trial(display_element, trial);
|
|
164
|
+
load_callback();
|
|
165
|
+
if (data.rt !== null) {
|
|
166
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
137
167
|
}
|
|
138
168
|
}
|
|
139
169
|
}
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\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 any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\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 // clear the display\n display_element.innerHTML = \"\";\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 =
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\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 any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\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 // clear the display\n display_element.innerHTML = \"\";\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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AAC,KAAsB,EAAE,kBAAkB;QACnE,MAAM,IAAI,GAAG,IAAI,CAAC,sBAAsB,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC;QAEpE,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;KAChC;IAEO,eAAe,CAAC,KAAsB,EAAE,kBAAkB,EAAE,aAAyB;QAC3F,MAAM,IAAI,GAAG,IAAI,CAAC,sBAAsB,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC;QAEpE,MAAM,eAAe,GAAG,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE,CAAC;QAEzD,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;QACnC,aAAa,EAAE,CAAC;QAEhB,IAAI,IAAI,CAAC,EAAE,KAAK,IAAI,EAAE;YACpB,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;SACzD;KACF;;AAtIM,+BAAI,GAAG,IAAI;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -118,5 +118,9 @@ declare class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
118
118
|
};
|
|
119
119
|
constructor(jsPsych: JsPsych);
|
|
120
120
|
trial(display_element: HTMLElement, trial: TrialType<Info>): void;
|
|
121
|
+
simulate(trial: TrialType<Info>, simulation_mode: any, simulation_options: any, load_callback: () => void): void;
|
|
122
|
+
private create_simulation_data;
|
|
123
|
+
private simulate_data_only;
|
|
124
|
+
private simulate_visual;
|
|
121
125
|
}
|
|
122
126
|
export default HtmlKeyboardResponsePlugin;
|
package/dist/index.js
CHANGED
|
@@ -98,7 +98,7 @@ class HtmlKeyboardResponsePlugin {
|
|
|
98
98
|
this.jsPsych.finishTrial(trial_data);
|
|
99
99
|
};
|
|
100
100
|
// function to handle responses by the subject
|
|
101
|
-
var after_response =
|
|
101
|
+
var after_response = (info) => {
|
|
102
102
|
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
103
103
|
// which can be used to provide visual feedback that a response was recorded
|
|
104
104
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className +=
|
|
@@ -123,15 +123,45 @@ class HtmlKeyboardResponsePlugin {
|
|
|
123
123
|
}
|
|
124
124
|
// hide stimulus if stimulus_duration is set
|
|
125
125
|
if (trial.stimulus_duration !== null) {
|
|
126
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
126
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
127
127
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").style.visibility = "hidden";
|
|
128
128
|
}, trial.stimulus_duration);
|
|
129
129
|
}
|
|
130
130
|
// end trial if trial_duration is set
|
|
131
131
|
if (trial.trial_duration !== null) {
|
|
132
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
133
|
-
|
|
134
|
-
|
|
132
|
+
this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
136
|
+
if (simulation_mode == "data-only") {
|
|
137
|
+
load_callback();
|
|
138
|
+
this.simulate_data_only(trial, simulation_options);
|
|
139
|
+
}
|
|
140
|
+
if (simulation_mode == "visual") {
|
|
141
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
create_simulation_data(trial, simulation_options) {
|
|
145
|
+
const default_data = {
|
|
146
|
+
stimulus: trial.stimulus,
|
|
147
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
148
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
149
|
+
};
|
|
150
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
151
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
152
|
+
return data;
|
|
153
|
+
}
|
|
154
|
+
simulate_data_only(trial, simulation_options) {
|
|
155
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
156
|
+
this.jsPsych.finishTrial(data);
|
|
157
|
+
}
|
|
158
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
159
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
160
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
161
|
+
this.trial(display_element, trial);
|
|
162
|
+
load_callback();
|
|
163
|
+
if (data.rt !== null) {
|
|
164
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
135
165
|
}
|
|
136
166
|
}
|
|
137
167
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\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 any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\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 // clear the display\n display_element.innerHTML = \"\";\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 =
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\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 any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\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 // clear the display\n display_element.innerHTML = \"\";\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":";;AAEA,MAAM,IAAI,GA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AW,CAAC,IAAI,CAAC,CAAC;KAChC;IAEO,eAAe,CAAC,KAAsB,EAAE,kBAAkB,EAAE,aAAyB;QAC3F,MAAM,IAAI,GAAG,IAAI,CAAC,sBAAsB,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC;QAEpE,MAAM,eAAe,GAAG,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE,CAAC;QAEzD,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;QACnC,aAAa,EAAE,CAAC;QAEhB,IAAI,IAAI,CAAC,EAAE,KAAK,IAAI,EAAE;YACpB,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;SACzD;KACF;;AAtIM,+BAAI,GAAG,IAAI;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jspsych/plugin-html-keyboard-response",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.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",
|
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
},
|
|
35
35
|
"homepage": "https://www.jspsych.org/latest/plugins/html-keyboard-response",
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"jspsych": ">=7.
|
|
37
|
+
"jspsych": ">=7.1.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@jspsych/config": "^1.
|
|
41
|
-
"@jspsych/test-utils": "^1.
|
|
40
|
+
"@jspsych/config": "^1.1.0",
|
|
41
|
+
"@jspsych/test-utils": "^1.1.0"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/src/index.spec.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { pressKey, startTimeline } from "@jspsych/test-utils";
|
|
1
|
+
import { pressKey, simulateTimeline, startTimeline } from "@jspsych/test-utils";
|
|
2
2
|
|
|
3
3
|
import htmlKeyboardResponse from ".";
|
|
4
4
|
|
|
@@ -134,3 +134,46 @@ describe("html-keyboard-response", () => {
|
|
|
134
134
|
await expectRunning();
|
|
135
135
|
});
|
|
136
136
|
});
|
|
137
|
+
|
|
138
|
+
describe("html-keyboard-response simulation", () => {
|
|
139
|
+
test("data mode works", async () => {
|
|
140
|
+
const timeline = [
|
|
141
|
+
{
|
|
142
|
+
type: htmlKeyboardResponse,
|
|
143
|
+
stimulus: "foo",
|
|
144
|
+
},
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
const { expectFinished, getData } = await simulateTimeline(timeline);
|
|
148
|
+
|
|
149
|
+
await expectFinished();
|
|
150
|
+
|
|
151
|
+
expect(getData().values()[0].rt).toBeGreaterThan(0);
|
|
152
|
+
expect(typeof getData().values()[0].response).toBe("string");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("visual mode works", async () => {
|
|
156
|
+
const timeline = [
|
|
157
|
+
{
|
|
158
|
+
type: htmlKeyboardResponse,
|
|
159
|
+
stimulus: "foo",
|
|
160
|
+
},
|
|
161
|
+
];
|
|
162
|
+
|
|
163
|
+
const { expectFinished, expectRunning, getHTML, getData } = await simulateTimeline(
|
|
164
|
+
timeline,
|
|
165
|
+
"visual"
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
await expectRunning();
|
|
169
|
+
|
|
170
|
+
expect(getHTML()).toContain("foo");
|
|
171
|
+
|
|
172
|
+
jest.runAllTimers();
|
|
173
|
+
|
|
174
|
+
await expectFinished();
|
|
175
|
+
|
|
176
|
+
expect(getData().values()[0].rt).toBeGreaterThan(0);
|
|
177
|
+
expect(typeof getData().values()[0].response).toBe("string");
|
|
178
|
+
});
|
|
179
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -111,7 +111,7 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
111
111
|
};
|
|
112
112
|
|
|
113
113
|
// function to handle responses by the subject
|
|
114
|
-
var after_response =
|
|
114
|
+
var after_response = (info) => {
|
|
115
115
|
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
116
116
|
// which can be used to provide visual feedback that a response was recorded
|
|
117
117
|
display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className +=
|
|
@@ -140,7 +140,7 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
140
140
|
|
|
141
141
|
// hide stimulus if stimulus_duration is set
|
|
142
142
|
if (trial.stimulus_duration !== null) {
|
|
143
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
143
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
144
144
|
display_element.querySelector<HTMLElement>(
|
|
145
145
|
"#jspsych-html-keyboard-response-stimulus"
|
|
146
146
|
).style.visibility = "hidden";
|
|
@@ -149,9 +149,55 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
149
149
|
|
|
150
150
|
// end trial if trial_duration is set
|
|
151
151
|
if (trial.trial_duration !== null) {
|
|
152
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
153
|
-
|
|
154
|
-
|
|
152
|
+
this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
simulate(
|
|
157
|
+
trial: TrialType<Info>,
|
|
158
|
+
simulation_mode,
|
|
159
|
+
simulation_options: any,
|
|
160
|
+
load_callback: () => void
|
|
161
|
+
) {
|
|
162
|
+
if (simulation_mode == "data-only") {
|
|
163
|
+
load_callback();
|
|
164
|
+
this.simulate_data_only(trial, simulation_options);
|
|
165
|
+
}
|
|
166
|
+
if (simulation_mode == "visual") {
|
|
167
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private create_simulation_data(trial: TrialType<Info>, simulation_options) {
|
|
172
|
+
const default_data = {
|
|
173
|
+
stimulus: trial.stimulus,
|
|
174
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
175
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
179
|
+
|
|
180
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
181
|
+
|
|
182
|
+
return data;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private simulate_data_only(trial: TrialType<Info>, simulation_options) {
|
|
186
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
187
|
+
|
|
188
|
+
this.jsPsych.finishTrial(data);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {
|
|
192
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
193
|
+
|
|
194
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
195
|
+
|
|
196
|
+
this.trial(display_element, trial);
|
|
197
|
+
load_callback();
|
|
198
|
+
|
|
199
|
+
if (data.rt !== null) {
|
|
200
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
155
201
|
}
|
|
156
202
|
}
|
|
157
203
|
}
|