@jspsych/plugin-html-keyboard-response 2.0.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.
@@ -1,96 +1,115 @@
1
1
  var jsPsychHtmlKeyboardResponse = (function (jspsych) {
2
2
  'use strict';
3
3
 
4
- var _package = {
5
- name: "@jspsych/plugin-html-keyboard-response",
6
- version: "2.0.0",
7
- description: "jsPsych plugin for displaying a stimulus and getting a keyboard response",
8
- type: "module",
9
- main: "dist/index.cjs",
10
- exports: {
11
- import: "./dist/index.js",
12
- require: "./dist/index.cjs"
13
- },
14
- typings: "dist/index.d.ts",
15
- unpkg: "dist/index.browser.min.js",
16
- files: [
17
- "src",
18
- "dist"
19
- ],
20
- source: "src/index.ts",
21
- scripts: {
22
- test: "jest",
23
- "test:watch": "npm test -- --watch",
24
- tsc: "tsc",
25
- build: "rollup --config",
26
- "build:watch": "npm run build -- --watch"
27
- },
28
- repository: {
29
- type: "git",
30
- url: "git+https://github.com/jspsych/jsPsych.git",
31
- directory: "packages/plugin-html-keyboard-response"
32
- },
33
- author: "Josh de Leeuw",
34
- license: "MIT",
35
- bugs: {
36
- url: "https://github.com/jspsych/jsPsych/issues"
37
- },
38
- homepage: "https://www.jspsych.org/latest/plugins/html-keyboard-response",
39
- peerDependencies: {
40
- jspsych: ">=7.1.0"
41
- },
42
- devDependencies: {
43
- "@jspsych/config": "^3.0.0",
44
- "@jspsych/test-utils": "^1.2.0"
45
- }
46
- };
4
+ var version = "2.2.0";
47
5
 
48
6
  const info = {
49
7
  name: "html-keyboard-response",
50
- version: _package.version,
8
+ version,
51
9
  parameters: {
10
+ /**
11
+ * The string to be displayed.
12
+ */
52
13
  stimulus: {
53
14
  type: jspsych.ParameterType.HTML_STRING,
54
15
  default: void 0
55
16
  },
17
+ /**
18
+ * This array contains the key(s) that the participant is allowed to press in order to respond
19
+ * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see
20
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}
21
+ * and
22
+ * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}
23
+ * for more examples. Any key presses that are not listed in the
24
+ * array will be ignored. The default value of `"ALL_KEYS"` means that all keys will be accepted as valid responses.
25
+ * Specifying `"NO_KEYS"` will mean that no responses are allowed.
26
+ */
56
27
  choices: {
57
28
  type: jspsych.ParameterType.KEYS,
58
29
  default: "ALL_KEYS"
59
30
  },
31
+ /**
32
+ * This string can contain HTML markup. Any content here will be displayed below the stimulus.
33
+ * The intention is that it can be used to provide a reminder about the action the participant
34
+ * is supposed to take (e.g., which key to press).
35
+ */
60
36
  prompt: {
61
37
  type: jspsych.ParameterType.HTML_STRING,
62
38
  default: null
63
39
  },
40
+ /**
41
+ * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus
42
+ * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will
43
+ * remain visible until the trial ends.
44
+ */
64
45
  stimulus_duration: {
65
46
  type: jspsych.ParameterType.INT,
66
47
  default: null
67
48
  },
49
+ /**
50
+ * How long to wait for the participant to make a response before ending the trial in milliseconds.
51
+ * If the participant fails to make a response before this timer is reached, the participant's response
52
+ * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,
53
+ * then the trial will wait for a response indefinitely.
54
+ */
68
55
  trial_duration: {
69
56
  type: jspsych.ParameterType.INT,
70
57
  default: null
71
58
  },
59
+ /**
60
+ * If true, then the trial will end whenever the participant makes a response (assuming they make their
61
+ * response before the cutoff specified by the trial_duration parameter). If false, then the trial will
62
+ * continue until the value for trial_duration is reached. You can set this parameter to false to force
63
+ * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.
64
+ */
72
65
  response_ends_trial: {
73
66
  type: jspsych.ParameterType.BOOL,
74
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
75
80
  }
76
81
  },
77
82
  data: {
83
+ /** Indicates which key the participant pressed. */
78
84
  response: {
79
85
  type: jspsych.ParameterType.STRING
80
86
  },
87
+ /** 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. */
81
88
  rt: {
82
89
  type: jspsych.ParameterType.INT
83
90
  },
91
+ /** The HTML content that was displayed on the screen. */
84
92
  stimulus: {
85
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
86
98
  }
99
+ },
100
+ // prettier-ignore
101
+ citations: {
102
+ "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 ",
103
+ "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}, } '
87
104
  }
88
105
  };
89
106
  class HtmlKeyboardResponsePlugin {
90
107
  constructor(jsPsych) {
91
108
  this.jsPsych = jsPsych;
92
109
  }
93
- static info = info;
110
+ static {
111
+ this.info = info;
112
+ }
94
113
  trial(display_element, trial) {
95
114
  var new_html = '<div id="jspsych-html-keyboard-response-stimulus">' + trial.stimulus + "</div>";
96
115
  if (trial.prompt !== null) {
@@ -99,7 +118,8 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
99
118
  display_element.innerHTML = new_html;
100
119
  var response = {
101
120
  rt: null,
102
- key: null
121
+ key: null,
122
+ rt_key_duration: null
103
123
  };
104
124
  const end_trial = () => {
105
125
  if (typeof keyboardListener !== "undefined") {
@@ -108,14 +128,19 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
108
128
  var trial_data = {
109
129
  rt: response.rt,
110
130
  stimulus: trial.stimulus,
111
- response: response.key
131
+ response: response.key,
132
+ rt_key_duration: response.rt_key_duration
112
133
  };
113
134
  this.jsPsych.finishTrial(trial_data);
114
135
  };
115
136
  var after_response = (info2) => {
116
137
  display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className += " responded";
117
138
  if (response.key == null) {
118
- response = info2;
139
+ response = {
140
+ rt: info2.rt,
141
+ key: info2.key,
142
+ rt_key_duration: info2.rt_key_duration ?? null
143
+ };
119
144
  }
120
145
  if (trial.response_ends_trial) {
121
146
  end_trial();
@@ -127,7 +152,8 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
127
152
  valid_responses: trial.choices,
128
153
  rt_method: "performance",
129
154
  persist: false,
130
- allow_held_key: false
155
+ allow_held_key: false,
156
+ wait_for_key_release: trial.wait_for_key_release
131
157
  });
132
158
  }
133
159
  if (trial.stimulus_duration !== null) {
@@ -154,8 +180,10 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
154
180
  const default_data = {
155
181
  stimulus: trial.stimulus,
156
182
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
157
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
183
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
184
+ rt_key_duration: null
158
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);
159
187
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
160
188
  this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
161
189
  return data;
@@ -178,4 +206,4 @@ var jsPsychHtmlKeyboardResponse = (function (jspsych) {
178
206
  return HtmlKeyboardResponsePlugin;
179
207
 
180
208
  })(jsPsychModule);
181
- //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@2.0.0/dist/index.browser.js.map
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":["../src/index.ts"],"sourcesContent":["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};\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","ParameterType","info"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAIA,MAAM,IAAc,GAAA;EAAA,EAClB,IAAM,EAAA,wBAAA;EAAA,WACNA,gBAAA;EAAA,EACA,UAAY,EAAA;EAAA,IAIV,QAAU,EAAA;EAAA,MACR,MAAMC,qBAAc,CAAA,WAAA;EAAA,MACpB,OAAS,EAAA,KAAA,CAAA;EAAA,KACX;EAAA,IAWA,OAAS,EAAA;EAAA,MACP,MAAMA,qBAAc,CAAA,IAAA;EAAA,MACpB,OAAS,EAAA,UAAA;EAAA,KACX;EAAA,IAMA,MAAQ,EAAA;EAAA,MACN,MAAMA,qBAAc,CAAA,WAAA;EAAA,MACpB,OAAS,EAAA,IAAA;EAAA,KACX;EAAA,IAMA,iBAAmB,EAAA;EAAA,MACjB,MAAMA,qBAAc,CAAA,GAAA;EAAA,MACpB,OAAS,EAAA,IAAA;EAAA,KACX;EAAA,IAOA,cAAgB,EAAA;EAAA,MACd,MAAMA,qBAAc,CAAA,GAAA;EAAA,MACpB,OAAS,EAAA,IAAA;EAAA,KACX;EAAA,IAOA,mBAAqB,EAAA;EAAA,MACnB,MAAMA,qBAAc,CAAA,IAAA;EAAA,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E,IAAM,MAAA,eAAA,GAAkB,IAAK,CAAA,OAAA,CAAQ,iBAAkB,EAAA,CAAA;EAEvD,IAAK,IAAA,CAAA,KAAA,CAAM,iBAAiB,KAAK,CAAA,CAAA;EACjC,IAAc,aAAA,EAAA,CAAA;EAEd,IAAI,IAAA,IAAA,CAAK,OAAO,IAAM,EAAA;EACpB,MAAA,IAAA,CAAK,QAAQ,SAAU,CAAA,QAAA,CAAS,IAAK,CAAA,QAAA,EAAU,KAAK,EAAE,CAAA,CAAA;EAAA,KACxD;EAAA,GACF;EACF;;;;;;;;"}
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(a){"use strict";var o={name:"@jspsych/plugin-html-keyboard-response",version:"2.0.0",description:"jsPsych plugin for displaying a stimulus and getting a keyboard response",type:"module",main:"dist/index.cjs",exports:{import:"./dist/index.js",require:"./dist/index.cjs"},typings:"dist/index.d.ts",unpkg:"dist/index.browser.min.js",files:["src","dist"],source:"src/index.ts",scripts:{test:"jest","test:watch":"npm test -- --watch",tsc:"tsc",build:"rollup --config","build:watch":"npm run build -- --watch"},repository:{type:"git",url:"git+https://github.com/jspsych/jsPsych.git",directory:"packages/plugin-html-keyboard-response"},author:"Josh de Leeuw",license:"MIT",bugs:{url:"https://github.com/jspsych/jsPsych/issues"},homepage:"https://www.jspsych.org/latest/plugins/html-keyboard-response",peerDependencies:{jspsych:">=7.1.0"},devDependencies:{"@jspsych/config":"^3.0.0","@jspsych/test-utils":"^1.2.0"}};const p={name:"html-keyboard-response",version:o.version,parameters:{stimulus:{type:a.ParameterType.HTML_STRING,default:void 0},choices:{type:a.ParameterType.KEYS,default:"ALL_KEYS"},prompt:{type:a.ParameterType.HTML_STRING,default:null},stimulus_duration:{type:a.ParameterType.INT,default:null},trial_duration:{type:a.ParameterType.INT,default:null},response_ends_trial:{type:a.ParameterType.BOOL,default:!0}},data:{response:{type:a.ParameterType.STRING},rt:{type:a.ParameterType.INT},stimulus:{type:a.ParameterType.STRING}}};class n{constructor(e){this.jsPsych=e}trial(e,s){var i='<div id="jspsych-html-keyboard-response-stimulus">'+s.stimulus+"</div>";s.prompt!==null&&(i+=s.prompt),e.innerHTML=i;var t={rt:null,key:null};const r=()=>{typeof u!="undefined"&&this.jsPsych.pluginAPI.cancelKeyboardResponse(u);var l={rt:t.rt,stimulus:s.stimulus,response:t.key};this.jsPsych.finishTrial(l)};var c=l=>{e.querySelector("#jspsych-html-keyboard-response-stimulus").className+=" responded",t.key==null&&(t=l),s.response_ends_trial&&r()};if(s.choices!="NO_KEYS")var u=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:c,valid_responses:s.choices,rt_method:"performance",persist:!1,allow_held_key:!1});s.stimulus_duration!==null&&this.jsPsych.pluginAPI.setTimeout(()=>{e.querySelector("#jspsych-html-keyboard-response-stimulus").style.visibility="hidden"},s.stimulus_duration),s.trial_duration!==null&&this.jsPsych.pluginAPI.setTimeout(r,s.trial_duration)}simulate(e,s,i,t){s=="data-only"&&(t(),this.simulate_data_only(e,i)),s=="visual"&&this.simulate_visual(e,i,t)}create_simulation_data(e,s){const i={stimulus:e.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,.006666666666666667,!0),response:this.jsPsych.pluginAPI.getValidKey(e.choices)},t=this.jsPsych.pluginAPI.mergeSimulationData(i,s);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(e,t),t}simulate_data_only(e,s){const i=this.create_simulation_data(e,s);this.jsPsych.finishTrial(i)}simulate_visual(e,s,i){const t=this.create_simulation_data(e,s),r=this.jsPsych.getDisplayElement();this.trial(r,e),i(),t.rt!==null&&this.jsPsych.pluginAPI.pressKey(t.response,t.rt)}}return n.info=p,n}(jsPsychModule);
2
- //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@2.0.0/dist/index.browser.min.js.map
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":["../src/index.ts"],"sourcesContent":["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};\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":["info","version","ParameterType","HtmlKeyboardResponsePlugin","jsPsych","display_element","trial","new_html","response","end_trial","keyboardListener","trial_data","after_response","simulation_mode","simulation_options","load_callback","default_data","data"],"mappings":"q7BAIA,MAAMA,EAAc,CAClB,KAAM,yBACN,QAASC,UACT,WAAY,CAIV,SAAU,CACR,KAAMC,EAAAA,cAAc,YACpB,QAAS,MACX,EAWA,QAAS,CACP,KAAMA,EAAAA,cAAc,KACpB,QAAS,UACX,EAMA,OAAQ,CACN,KAAMA,EAAc,cAAA,YACpB,QAAS,IACX,EAMA,kBAAmB,CACjB,KAAMA,gBAAc,IACpB,QAAS,IACX,EAOA,eAAgB,CACd,KAAMA,EAAAA,cAAc,IACpB,QAAS,IACX,EAOA,oBAAqB,CACnB,KAAMA,EAAAA,cAAc,KACpB,QAAS,EACX,CACF,EACA,KAAM,CAEJ,SAAU,CACR,KAAMA,gBAAc,MACtB,EAEA,GAAI,CACF,KAAMA,EAAAA,cAAc,GACtB,EAEA,SAAU,CACR,KAAMA,EAAAA,cAAc,MACtB,CACF,CACF,EAYA,MAAMC,CAA0D,CAE9D,YAAoBC,EAAkB,CAAlB,aAAAA,CAAmB,CAEvC,MAAMC,EAA8BC,EAAwB,CAC1D,IAAIC,EAAW,qDAAuDD,EAAM,SAAW,SAGnFA,EAAM,SAAW,OACnBC,GAAYD,EAAM,QAIpBD,EAAgB,UAAYE,EAG5B,IAAIC,EAAW,CACb,GAAI,KACJ,IAAK,IACP,EAGA,MAAMC,EAAY,IAAM,CAElB,OAAOC,GAAqB,aAC9B,KAAK,QAAQ,UAAU,uBAAuBA,CAAgB,EAIhE,IAAIC,EAAa,CACf,GAAIH,EAAS,GACb,SAAUF,EAAM,SAChB,SAAUE,EAAS,GACrB,EAGA,KAAK,QAAQ,YAAYG,CAAU,CACrC,EAGA,IAAIC,EAAkBZ,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K,MAAMA,EAAiBC,CAAK,EACjCS,EAEIE,EAAAA,EAAK,KAAO,MACd,KAAK,QAAQ,UAAU,SAASA,EAAK,SAAUA,EAAK,EAAE,CAE1D,CACF,CAjIMd,OAAAA,EACG,KAAOH"}
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,95 +2,115 @@
2
2
 
3
3
  var jspsych = require('jspsych');
4
4
 
5
- var _package = {
6
- name: "@jspsych/plugin-html-keyboard-response",
7
- version: "2.0.0",
8
- description: "jsPsych plugin for displaying a stimulus and getting a keyboard response",
9
- type: "module",
10
- main: "dist/index.cjs",
11
- exports: {
12
- import: "./dist/index.js",
13
- require: "./dist/index.cjs"
14
- },
15
- typings: "dist/index.d.ts",
16
- unpkg: "dist/index.browser.min.js",
17
- files: [
18
- "src",
19
- "dist"
20
- ],
21
- source: "src/index.ts",
22
- scripts: {
23
- test: "jest",
24
- "test:watch": "npm test -- --watch",
25
- tsc: "tsc",
26
- build: "rollup --config",
27
- "build:watch": "npm run build -- --watch"
28
- },
29
- repository: {
30
- type: "git",
31
- url: "git+https://github.com/jspsych/jsPsych.git",
32
- directory: "packages/plugin-html-keyboard-response"
33
- },
34
- author: "Josh de Leeuw",
35
- license: "MIT",
36
- bugs: {
37
- url: "https://github.com/jspsych/jsPsych/issues"
38
- },
39
- homepage: "https://www.jspsych.org/latest/plugins/html-keyboard-response",
40
- peerDependencies: {
41
- jspsych: ">=7.1.0"
42
- },
43
- devDependencies: {
44
- "@jspsych/config": "^3.0.0",
45
- "@jspsych/test-utils": "^1.2.0"
46
- }
47
- };
5
+ var version = "2.2.0";
48
6
 
49
7
  const info = {
50
8
  name: "html-keyboard-response",
51
- version: _package.version,
9
+ version,
52
10
  parameters: {
11
+ /**
12
+ * The string to be displayed.
13
+ */
53
14
  stimulus: {
54
15
  type: jspsych.ParameterType.HTML_STRING,
55
16
  default: void 0
56
17
  },
18
+ /**
19
+ * This array contains the key(s) that the participant is allowed to press in order to respond
20
+ * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see
21
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}
22
+ * and
23
+ * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}
24
+ * for more examples. Any key presses that are not listed in the
25
+ * array will be ignored. The default value of `"ALL_KEYS"` means that all keys will be accepted as valid responses.
26
+ * Specifying `"NO_KEYS"` will mean that no responses are allowed.
27
+ */
57
28
  choices: {
58
29
  type: jspsych.ParameterType.KEYS,
59
30
  default: "ALL_KEYS"
60
31
  },
32
+ /**
33
+ * This string can contain HTML markup. Any content here will be displayed below the stimulus.
34
+ * The intention is that it can be used to provide a reminder about the action the participant
35
+ * is supposed to take (e.g., which key to press).
36
+ */
61
37
  prompt: {
62
38
  type: jspsych.ParameterType.HTML_STRING,
63
39
  default: null
64
40
  },
41
+ /**
42
+ * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus
43
+ * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will
44
+ * remain visible until the trial ends.
45
+ */
65
46
  stimulus_duration: {
66
47
  type: jspsych.ParameterType.INT,
67
48
  default: null
68
49
  },
50
+ /**
51
+ * How long to wait for the participant to make a response before ending the trial in milliseconds.
52
+ * If the participant fails to make a response before this timer is reached, the participant's response
53
+ * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,
54
+ * then the trial will wait for a response indefinitely.
55
+ */
69
56
  trial_duration: {
70
57
  type: jspsych.ParameterType.INT,
71
58
  default: null
72
59
  },
60
+ /**
61
+ * If true, then the trial will end whenever the participant makes a response (assuming they make their
62
+ * response before the cutoff specified by the trial_duration parameter). If false, then the trial will
63
+ * continue until the value for trial_duration is reached. You can set this parameter to false to force
64
+ * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.
65
+ */
73
66
  response_ends_trial: {
74
67
  type: jspsych.ParameterType.BOOL,
75
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
76
81
  }
77
82
  },
78
83
  data: {
84
+ /** Indicates which key the participant pressed. */
79
85
  response: {
80
86
  type: jspsych.ParameterType.STRING
81
87
  },
88
+ /** 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. */
82
89
  rt: {
83
90
  type: jspsych.ParameterType.INT
84
91
  },
92
+ /** The HTML content that was displayed on the screen. */
85
93
  stimulus: {
86
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
87
99
  }
100
+ },
101
+ // prettier-ignore
102
+ citations: {
103
+ "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 ",
104
+ "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}, } '
88
105
  }
89
106
  };
90
107
  class HtmlKeyboardResponsePlugin {
91
108
  constructor(jsPsych) {
92
109
  this.jsPsych = jsPsych;
93
110
  }
111
+ static {
112
+ this.info = info;
113
+ }
94
114
  trial(display_element, trial) {
95
115
  var new_html = '<div id="jspsych-html-keyboard-response-stimulus">' + trial.stimulus + "</div>";
96
116
  if (trial.prompt !== null) {
@@ -99,7 +119,8 @@ class HtmlKeyboardResponsePlugin {
99
119
  display_element.innerHTML = new_html;
100
120
  var response = {
101
121
  rt: null,
102
- key: null
122
+ key: null,
123
+ rt_key_duration: null
103
124
  };
104
125
  const end_trial = () => {
105
126
  if (typeof keyboardListener !== "undefined") {
@@ -108,14 +129,19 @@ class HtmlKeyboardResponsePlugin {
108
129
  var trial_data = {
109
130
  rt: response.rt,
110
131
  stimulus: trial.stimulus,
111
- response: response.key
132
+ response: response.key,
133
+ rt_key_duration: response.rt_key_duration
112
134
  };
113
135
  this.jsPsych.finishTrial(trial_data);
114
136
  };
115
137
  var after_response = (info2) => {
116
138
  display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className += " responded";
117
139
  if (response.key == null) {
118
- response = info2;
140
+ response = {
141
+ rt: info2.rt,
142
+ key: info2.key,
143
+ rt_key_duration: info2.rt_key_duration ?? null
144
+ };
119
145
  }
120
146
  if (trial.response_ends_trial) {
121
147
  end_trial();
@@ -127,7 +153,8 @@ class HtmlKeyboardResponsePlugin {
127
153
  valid_responses: trial.choices,
128
154
  rt_method: "performance",
129
155
  persist: false,
130
- allow_held_key: false
156
+ allow_held_key: false,
157
+ wait_for_key_release: trial.wait_for_key_release
131
158
  });
132
159
  }
133
160
  if (trial.stimulus_duration !== null) {
@@ -154,8 +181,10 @@ class HtmlKeyboardResponsePlugin {
154
181
  const default_data = {
155
182
  stimulus: trial.stimulus,
156
183
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
157
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
184
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
185
+ rt_key_duration: null
158
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);
159
188
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
160
189
  this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
161
190
  return data;
@@ -174,7 +203,6 @@ class HtmlKeyboardResponsePlugin {
174
203
  }
175
204
  }
176
205
  }
177
- HtmlKeyboardResponsePlugin.info = info;
178
206
 
179
207
  module.exports = HtmlKeyboardResponsePlugin;
180
208
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["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};\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","ParameterType","info"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,MAAM,IAAc,GAAA;AAAA,EAClB,IAAM,EAAA,wBAAA;AAAA,WACNA,gBAAA;AAAA,EACA,UAAY,EAAA;AAAA,IAIV,QAAU,EAAA;AAAA,MACR,MAAMC,qBAAc,CAAA,WAAA;AAAA,MACpB,OAAS,EAAA,KAAA,CAAA;AAAA,KACX;AAAA,IAWA,OAAS,EAAA;AAAA,MACP,MAAMA,qBAAc,CAAA,IAAA;AAAA,MACpB,OAAS,EAAA,UAAA;AAAA,KACX;AAAA,IAMA,MAAQ,EAAA;AAAA,MACN,MAAMA,qBAAc,CAAA,WAAA;AAAA,MACpB,OAAS,EAAA,IAAA;AAAA,KACX;AAAA,IAMA,iBAAmB,EAAA;AAAA,MACjB,MAAMA,qBAAc,CAAA,GAAA;AAAA,MACpB,OAAS,EAAA,IAAA;AAAA,KACX;AAAA,IAOA,cAAgB,EAAA;AAAA,MACd,MAAMA,qBAAc,C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,MAAM,IAAO,GAAA,IAAA,CAAK,sBAAuB,CAAA,KAAA,EAAO,kBAAkB,CAAA,CAAA;AAElE,IAAM,MAAA,eAAA,GAAkB,IAAK,CAAA,OAAA,CAAQ,iBAAkB,EAAA,CAAA;AAEvD,IAAK,IAAA,CAAA,KAAA,CAAM,iBAAiB,KAAK,CAAA,CAAA;AACjC,IAAc,aAAA,EAAA,CAAA;AAEd,IAAI,IAAA,IAAA,CAAK,OAAO,IAAM,EAAA;AACpB,MAAA,IAAA,CAAK,QAAQ,SAAU,CAAA,QAAA,CAAS,IAAK,CAAA,QAAA,EAAU,KAAK,EAAE,CAAA,CAAA;AAAA,KACxD;AAAA,GACF;AACF,CAAA;AAjIM,0BAAA,CACG,IAAO,GAAA,IAAA;;;;"}
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,7 +89,12 @@ 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
  };
97
+ readonly citations: "__CITATIONS__";
81
98
  };
82
99
  type Info = typeof info;
83
100
  /**
@@ -153,6 +170,18 @@ declare class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
153
170
  readonly type: ParameterType.BOOL;
154
171
  readonly default: true;
155
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
+ };
156
185
  };
157
186
  readonly data: {
158
187
  /** Indicates which key the participant pressed. */
@@ -167,7 +196,12 @@ declare class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
167
196
  readonly stimulus: {
168
197
  readonly type: ParameterType.STRING;
169
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
+ };
170
203
  };
204
+ readonly citations: "__CITATIONS__";
171
205
  };
172
206
  constructor(jsPsych: JsPsych);
173
207
  trial(display_element: HTMLElement, trial: TrialType<Info>): void;
package/dist/index.js CHANGED
@@ -1,94 +1,114 @@
1
1
  import { ParameterType } from 'jspsych';
2
2
 
3
- var _package = {
4
- name: "@jspsych/plugin-html-keyboard-response",
5
- version: "2.0.0",
6
- description: "jsPsych plugin for displaying a stimulus and getting a keyboard response",
7
- type: "module",
8
- main: "dist/index.cjs",
9
- exports: {
10
- import: "./dist/index.js",
11
- require: "./dist/index.cjs"
12
- },
13
- typings: "dist/index.d.ts",
14
- unpkg: "dist/index.browser.min.js",
15
- files: [
16
- "src",
17
- "dist"
18
- ],
19
- source: "src/index.ts",
20
- scripts: {
21
- test: "jest",
22
- "test:watch": "npm test -- --watch",
23
- tsc: "tsc",
24
- build: "rollup --config",
25
- "build:watch": "npm run build -- --watch"
26
- },
27
- repository: {
28
- type: "git",
29
- url: "git+https://github.com/jspsych/jsPsych.git",
30
- directory: "packages/plugin-html-keyboard-response"
31
- },
32
- author: "Josh de Leeuw",
33
- license: "MIT",
34
- bugs: {
35
- url: "https://github.com/jspsych/jsPsych/issues"
36
- },
37
- homepage: "https://www.jspsych.org/latest/plugins/html-keyboard-response",
38
- peerDependencies: {
39
- jspsych: ">=7.1.0"
40
- },
41
- devDependencies: {
42
- "@jspsych/config": "^3.0.0",
43
- "@jspsych/test-utils": "^1.2.0"
44
- }
45
- };
3
+ var version = "2.2.0";
46
4
 
47
5
  const info = {
48
6
  name: "html-keyboard-response",
49
- version: _package.version,
7
+ version,
50
8
  parameters: {
9
+ /**
10
+ * The string to be displayed.
11
+ */
51
12
  stimulus: {
52
13
  type: ParameterType.HTML_STRING,
53
14
  default: void 0
54
15
  },
16
+ /**
17
+ * This array contains the key(s) that the participant is allowed to press in order to respond
18
+ * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see
19
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}
20
+ * and
21
+ * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}
22
+ * for more examples. Any key presses that are not listed in the
23
+ * array will be ignored. The default value of `"ALL_KEYS"` means that all keys will be accepted as valid responses.
24
+ * Specifying `"NO_KEYS"` will mean that no responses are allowed.
25
+ */
55
26
  choices: {
56
27
  type: ParameterType.KEYS,
57
28
  default: "ALL_KEYS"
58
29
  },
30
+ /**
31
+ * This string can contain HTML markup. Any content here will be displayed below the stimulus.
32
+ * The intention is that it can be used to provide a reminder about the action the participant
33
+ * is supposed to take (e.g., which key to press).
34
+ */
59
35
  prompt: {
60
36
  type: ParameterType.HTML_STRING,
61
37
  default: null
62
38
  },
39
+ /**
40
+ * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus
41
+ * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will
42
+ * remain visible until the trial ends.
43
+ */
63
44
  stimulus_duration: {
64
45
  type: ParameterType.INT,
65
46
  default: null
66
47
  },
48
+ /**
49
+ * How long to wait for the participant to make a response before ending the trial in milliseconds.
50
+ * If the participant fails to make a response before this timer is reached, the participant's response
51
+ * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,
52
+ * then the trial will wait for a response indefinitely.
53
+ */
67
54
  trial_duration: {
68
55
  type: ParameterType.INT,
69
56
  default: null
70
57
  },
58
+ /**
59
+ * If true, then the trial will end whenever the participant makes a response (assuming they make their
60
+ * response before the cutoff specified by the trial_duration parameter). If false, then the trial will
61
+ * continue until the value for trial_duration is reached. You can set this parameter to false to force
62
+ * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.
63
+ */
71
64
  response_ends_trial: {
72
65
  type: ParameterType.BOOL,
73
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
74
79
  }
75
80
  },
76
81
  data: {
82
+ /** Indicates which key the participant pressed. */
77
83
  response: {
78
84
  type: ParameterType.STRING
79
85
  },
86
+ /** 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. */
80
87
  rt: {
81
88
  type: ParameterType.INT
82
89
  },
90
+ /** The HTML content that was displayed on the screen. */
83
91
  stimulus: {
84
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
85
97
  }
98
+ },
99
+ // prettier-ignore
100
+ citations: {
101
+ "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 ",
102
+ "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}, } '
86
103
  }
87
104
  };
88
105
  class HtmlKeyboardResponsePlugin {
89
106
  constructor(jsPsych) {
90
107
  this.jsPsych = jsPsych;
91
108
  }
109
+ static {
110
+ this.info = info;
111
+ }
92
112
  trial(display_element, trial) {
93
113
  var new_html = '<div id="jspsych-html-keyboard-response-stimulus">' + trial.stimulus + "</div>";
94
114
  if (trial.prompt !== null) {
@@ -97,7 +117,8 @@ class HtmlKeyboardResponsePlugin {
97
117
  display_element.innerHTML = new_html;
98
118
  var response = {
99
119
  rt: null,
100
- key: null
120
+ key: null,
121
+ rt_key_duration: null
101
122
  };
102
123
  const end_trial = () => {
103
124
  if (typeof keyboardListener !== "undefined") {
@@ -106,14 +127,19 @@ class HtmlKeyboardResponsePlugin {
106
127
  var trial_data = {
107
128
  rt: response.rt,
108
129
  stimulus: trial.stimulus,
109
- response: response.key
130
+ response: response.key,
131
+ rt_key_duration: response.rt_key_duration
110
132
  };
111
133
  this.jsPsych.finishTrial(trial_data);
112
134
  };
113
135
  var after_response = (info2) => {
114
136
  display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className += " responded";
115
137
  if (response.key == null) {
116
- response = info2;
138
+ response = {
139
+ rt: info2.rt,
140
+ key: info2.key,
141
+ rt_key_duration: info2.rt_key_duration ?? null
142
+ };
117
143
  }
118
144
  if (trial.response_ends_trial) {
119
145
  end_trial();
@@ -125,7 +151,8 @@ class HtmlKeyboardResponsePlugin {
125
151
  valid_responses: trial.choices,
126
152
  rt_method: "performance",
127
153
  persist: false,
128
- allow_held_key: false
154
+ allow_held_key: false,
155
+ wait_for_key_release: trial.wait_for_key_release
129
156
  });
130
157
  }
131
158
  if (trial.stimulus_duration !== null) {
@@ -152,8 +179,10 @@ class HtmlKeyboardResponsePlugin {
152
179
  const default_data = {
153
180
  stimulus: trial.stimulus,
154
181
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
155
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
182
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
183
+ rt_key_duration: null
156
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);
157
186
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
158
187
  this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
159
188
  return data;
@@ -172,7 +201,6 @@ class HtmlKeyboardResponsePlugin {
172
201
  }
173
202
  }
174
203
  }
175
- HtmlKeyboardResponsePlugin.info = info;
176
204
 
177
205
  export { HtmlKeyboardResponsePlugin as default };
178
206
  //# sourceMappingURL=index.js.map
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\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};\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","info"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,MAAM,IAAc,GAAA;AAAA,EAClB,IAAM,EAAA,wBAAA;AAAA,WACNA,gBAAA;AAAA,EACA,UAAY,EAAA;AAAA,IAIV,QAAU,EAAA;AAAA,MACR,MAAM,aAAc,CAAA,WAAA;AAAA,MACpB,OAAS,EAAA,KAAA,CAAA;AAAA,KACX;AAAA,IAWA,OAAS,EAAA;AAAA,MACP,MAAM,aAAc,CAAA,IAAA;AAAA,MACpB,OAAS,EAAA,UAAA;AAAA,KACX;AAAA,IAMA,MAAQ,EAAA;AAAA,MACN,MAAM,aAAc,CAAA,WAAA;AAAA,MACpB,OAAS,EAAA,IAAA;AAAA,KACX;AAAA,IAMA,iBAAmB,EAAA;AAAA,MACjB,MAAM,aAAc,CAAA,GAAA;AAAA,MACpB,OAAS,EAAA,IAAA;AAAA,KACX;AAAA,IAOA,cAAgB,EAAA;AAAA,MACd,MAAM,aAAc,CAAA,GAAA;AAAA,MACpB,OAAS,EAAA,IAAA;AAAA,KACX;AAAA,IAOA,mBAAqB,EAAA;AAAA,MACnB,MAAM,aAAc,CAAA,IAAA;AAAA,MACpB,OAAS,EAAA,IAAA;AAAA,KACX;AAAA,GACF;AAAA,EACA,IAAM,EAAA;AAAA,IAEJ,QAAU,EAAA;AAAA,MACR,MAAM,aAAc,CAAA,MAAA;AAAA,KACtB;AAAA,IAEA,EAAI,EAAA;AAAA,MACF,MAAM,aAAc,CAAA,GAAA;AAAA,KACtB;AAAA,IAEA,QAAU,EAAA;AAAA,MACR,MAAM,aAAc,CAAA,MAAA;AAAA,KACtB;AAAA,GACF;AACF,CAAA,CAAA;AAYA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AA,CACG,IAAO,GAAA,IAAA;;;;"}
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.0.0",
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.0.0",
41
- "@jspsych/test-utils": "^1.2.0"
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 { pressKey, simulateTimeline, startTimeline } from "@jspsych/test-utils";
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(document.querySelector("#jspsych-html-keyboard-response-stimulus").className).toBe(
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,7 +91,13 @@ 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
  },
99
+ // prettier-ignore
100
+ citations: '__CITATIONS__',
83
101
  };
84
102
 
85
103
  type Info = typeof info;
@@ -111,6 +129,7 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
111
129
  var response = {
112
130
  rt: null,
113
131
  key: null,
132
+ rt_key_duration: null,
114
133
  };
115
134
 
116
135
  // function to end trial when it is time
@@ -125,6 +144,7 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
125
144
  rt: response.rt,
126
145
  stimulus: trial.stimulus,
127
146
  response: response.key,
147
+ rt_key_duration: response.rt_key_duration,
128
148
  };
129
149
 
130
150
  // move on to the next trial
@@ -140,7 +160,11 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
140
160
 
141
161
  // only record the first response
142
162
  if (response.key == null) {
143
- response = info;
163
+ response = {
164
+ rt: info.rt,
165
+ key: info.key,
166
+ rt_key_duration: info.rt_key_duration ?? null,
167
+ };
144
168
  }
145
169
 
146
170
  if (trial.response_ends_trial) {
@@ -156,6 +180,7 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
156
180
  rt_method: "performance",
157
181
  persist: false,
158
182
  allow_held_key: false,
183
+ wait_for_key_release: trial.wait_for_key_release,
159
184
  });
160
185
  }
161
186
 
@@ -194,7 +219,12 @@ class HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {
194
219
  stimulus: trial.stimulus,
195
220
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
196
221
  response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
222
+ rt_key_duration: null,
197
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);
198
228
 
199
229
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
200
230