# jsPsych.pluginAPI The pluginAPI module contains functions that are useful when developing new plugins. --- ## jsPsych.pluginAPI.cancelAllKeyboardResponses ``` jsPsych.pluginAPI.cancelAllKeyboardResponses() ``` ### Parameters None. ### Return value Returns nothing. ### Description Cancels all currently active keyboard listeners created by `jsPsych.pluginAPI.getKeyboardResponse`. ### Examples ```javascript jsPsych.pluginAPI.cancelAllKeyboardResponses(); ``` --- ## jsPsych.pluginAPI.cancelKeyboardResponse ``` jsPsych.pluginAPI.cancelKeyboardResponse(listener_id) ``` ### Parameters Parameter | Type | Description ----------|------|------------ listener_id | object | The listener_id object generated by the call to `jsPsych.pluginAPI.getKeyboardResponse`. ### Return value Returns nothing. ### Description Cancels a specific keyboard listener created by `jsPsych.pluginAPI.getKeyboardResponse`. ### Examples ```javascript // create a persistent keyboard listener var listener_id = jsPsych.pluginAPI.getKeyboardResponse(after_response, ['p','q'], 'date', true); // cancel keyboard listener jsPsych.pluginAPI.cancelKeyboardResponse(listener_id); ``` --- ## jsPsych.pluginAPI.convertKeyCharacterToKeyCode ``` jsPsych.pluginAPI.convertKeyCharacterToKeyCode(character) ``` ### Parameters Parameter | Type | Description ----------|------|------------ character | string | The string representation of keyboard key. ### Return value Returns the numeric keycode associated with the `character` parameter. ### Description Converts between the string representation of a key and the numeric key code associated with that key. ### Examples ```javascript var keycode = jsPsych.pluginAPI.convertKeyCharacterToKeyCode('a') // keycode is 65 keycode = jsPsych.pluginAPI.convertKeyCharacterToKeyCode('space') // keycode is 32 ``` --- ## jsPsych.pluginAPI.enforceArray ``` jsPsych.pluginAPI.enforceArray(params, possible_arrays) ``` ### Parameters Parameter | Type | Description ----------|------|------------ params | object | An object of `key: value` pairs. possible_arrays | array | Array of strings where each string is a key from `params` indicating which `keys` should have arrays as the `value`. ### Return value Returns a copy of `params` where all of the `keys` in `possible_arrays` are guaranteed to be arrays. ### Description This function checks if specified parameters are arrays. If they are not arrays, then it converts them to arrays. Practically, this is used for cases where a plugin calls for an array, but a single value is also a reasonable option. An example would be specifying a stimulus for the `single-stim` plugin. Technically, the plugin requires the stimuli to be specified in an array. Each element of the array is then given its own trial. However, a single element array to present one trial is possible. This function means that users can declare the single element array as either an array or just the value that would be the element in the array. ### Examples ```javascript // a snippet from the text plugin plugin.create = function(params) { params = jsPsych.pluginAPI.enforceArray(params, ['text','data']); // other stuff here ... }; ``` --- ## jsPsych.pluginAPI.getKeyboardResponse ``` jsPsych.pluginAPI.getKeyboardResponse(parameters) ``` ### Parameters The method accepts an object of parameter values (see example below). The valid keys for this object are listed in the table below. Parameter | Type | Description ----------|------|------------ callback_function | function | The function to execute whenever a valid keyboard response is generated. valid_responses | array | An array of key codes or character strings representing valid responses. Responses not on the list will be ignored. An empty array indicates that all responses are acceptable. rt_method | string | Indicates which method of recording time to use. The `'date'` method uses calls to `(new Date()).getTime()` to record timing information. The `'performance'` method uses calls to `performance.now()`, which is a more modern JavaScript feature. The `'performance'` approach is [not supported by all the major browsers yet](http://caniuse.com/#search=performance), but adoption rates are increasing. The `audio` method is used in conjuction with an `audio_context` (set as an additional parameter). This uses the clock time of the `audio_context` when audio stimuli are being played. audio_context | AudioContext object | The AudioContext of the audio file that is being played. audio_context_start_time | numeric | The scheduled time of the sound file in the AudioContext. This will be used as the start time. allow_held_key | boolean | If `true`, then responses will be registered from keys that are being held down. If `false`, then a held key can only register a response the first time that `getKeyboardResponse` is called for that key. For example, if a participant holds down the `A` key before the experiment starts, then the first time `getKeyboardResponse` is called, the `A` will register as a key press. However, any future calls to `getKeyboardResponse` will not register the `A` until the participant releases the key and presses it again. persist | boolean | If false, then the keyboard listener will only trigger the first time a valid key is pressed. If true, then it will trigger every time a valid key is pressed until it is explicitly cancelled by `jsPsych.pluginAPI.cancelKeyboardResponse` or `jsPsych.pluginAPI.cancelAllKeyboardResponses`. ### Return value Return an object that uniquely identifies the keyboard listener. This object can be passed to `jsPsych.pluginAPI.cancelKeyboardResponse` to cancel the keyboard listener. ### Description Gets a keyboard response from the subject, recording the response time from when the function is first called until a valid response is generated. A valid response triggers the `callback_function` specified in the parameters. A single argument is passed to the callback function. The argument contains an object with the properties `key` and `rt`. `key` contains the numeric key code of the response, and `rt` contains the response time. ### Examples #### Get a single response from any key ```javascript var after_response = function(info){ alert('You pressed key '+info.key+' after '+info.rt+'ms'); } jsPsych.pluginAPI.getKeyboardResponse({ callback_function:after_response, valid_responses: [], rt_method: 'date', persist: false }); ``` #### Get a responses from a key until the letter Q is pressed ```javascript var after_response = function(info){ alert('You pressed key '+info.key+' after '+info.rt+'ms'); if(info.key == 81){ // the key code for 'Q' is 81. jsPsych.pluginAPI.cancelKeyboardResponse(listener); } } var listener = jsPsych.pluginAPI.getKeyboardResponse({ callback_function:after_response, valid_responses: [], rt_method: 'date', persist: true }); ``` --- ## jsPsych.pluginAPI.evaluateFunctionParameters ``` jsPsych.pluginAPI.evaluateFunctionParameters(trial, protect) ``` ### Parameters Parameter | Type | Description ----------|------|------------ trial | object | An object representing the trial (typically the same variable that gets passed to the `plugin.trial` method). It contains `key: value` pairs describing all the trial parameters. protect | array | An array of strings, indicating which parameters in the `trial` object should be protected from normalization ### Return value Returns a new trial object with all values that were functions replaced by the return value of the function. ### Description This method replaces any parameters that are functions with the output of the function. ### Example ```javascript // a snippet from a trial method plugin.trial = function(display_element, trial) { trial = jsPsych.pluginAPI.evaluateFunctionParameters(trial); // the rest of the trial code... } ```