UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

1,286 lines 47.1 kB
export default TinyGamepad; /** * Describes the status of a key or button. */ export type KeyStatus = { /** * - Whether the key is currently pressed. */ pressed: boolean; /** * - Optional analog value associated with the key. */ value?: number | undefined; /** * - Optional second analog value. */ value2?: number | undefined; }; /** * Defines the available input modes. */ export type InputMode = "gamepad-only" | "keyboard-only" | "both"; /** * A callback function that is invoked when a mapped logical input is activated or deactivated. * * This function receives the logical name associated with the input (e.g., "Jump", "Shoot", "Menu") * and can be used to handle input-related actions such as triggering game mechanics or UI behavior. */ export type MappedInputCallback = (payload: { logicalName: string; activeTime: number; comboTime: number; }) => void; /** * A callback function that is invoked when a mapped key is activated or deactivated. * * This function receives the key name associated with the input (e.g., "KeyA", "KeyB", "KeyC") * and can be used to handle input-related actions such as triggering game mechanics or UI behavior. */ export type MappedKeyCallback = (payload: { key: string; activeTime: number; }) => void; /** * Callback for handling input events. */ export type PayloadCallback = (payload: InputPayload | InputAnalogPayload) => void; /** * Callback for handling gamepad connection events. */ export type ConnectionCallback = (payload: ConnectionPayload) => void; /** * A callback function that is triggered when a registered input sequence is fully activated. */ export type InputSequenceCallback = (timestamp: number) => any; /** * A callback function that is triggered when a registered key sequence is fully activated. */ export type KeySequenceCallback = (timestamp: number) => any; /** * Represents any valid callback type used in the TinyGamepad event system. * This is a union of all supported callback signatures. */ export type CallbackList = ConnectionCallback | PayloadCallback | MappedInputCallback | MappedKeyCallback; /** * Represents a specific input source from a gamepad. * - 'gamepad-analog' refers to analog sticks or analog triggers. * - 'gamepad-button' refers to digital buttons on the gamepad. */ export type GamepadDeviceSource = "gamepad-analog" | "gamepad-button"; /** * Represents any possible physical device source that can be used for input. * - 'mouse': Input from a mouse. * - 'keyboard': Input from a keyboard. * - 'gamepad-analog': Analog input from a gamepad (e.g., sticks, triggers). * - 'gamepad-button': Digital button input from a gamepad. */ export type DeviceSource = "mouse" | "keyboard" | GamepadDeviceSource; /** * Represents the type of input interaction detected. * - 'up': Input was released. * - 'down': Input was pressed. * - 'hold': Input is being held. * - 'change': Input value changed (e.g., pressure or axis). * - 'move': Analog movement detected (e.g., joystick motion). */ export type DeviceInputType = "up" | "down" | "hold" | "change" | "move"; /** * Structure for digital button input payload. */ export type InputPayload = { /** * - Unique identifier for the input. */ id: string; /** * - Optional DOM event reference. */ event?: Event | undefined; /** * - Type of input event (down, up, hold). */ type: DeviceInputType; /** * - Input source (keyboard, mouse, gamepad). */ source: DeviceSource; /** * - Physical input identifier. */ key: string; /** * - Whether the input is pressure sensitive. */ isPressure: boolean; /** * - Logical name associated with the input. */ logicalName: string; /** * - Primary analog value. */ value: number; /** * - Secondary analog value. */ value2: number; /** * - Current pressed status. */ pressed: boolean; /** * - Previous pressed status. */ prevPressed?: boolean | null | undefined; /** * - Timestamp of the event. */ timestamp: number; /** * - Reference to the originating Gamepad. */ gp?: Gamepad | undefined; }; /** * Structure for analog input payload. */ export type InputAnalogPayload = { /** * - Unique identifier for the input. */ id: string; /** * - Optional DOM event reference. */ event?: Event | undefined; /** * - Type of input event (change). */ type: DeviceInputType; /** * - Input source. */ source: DeviceSource; /** * - Physical input identifier. */ key: string; /** * - Logical name associated with the input. */ logicalName: string; /** * - Analog value. */ value: number; /** * - Secondary analog value. */ value2: number; /** * - Timestamp of the event. */ timestamp: number; /** * - Reference to the originating Gamepad. */ gp?: Gamepad | undefined; }; /** * Internal structure for digital input events. */ export type InputEvents = { /** * - Unique identifier for the input. */ id: string; /** * - Optional DOM event reference. */ event?: Event | undefined; /** * - Input key identifier. */ key: string; /** * - Source of the input. */ source: DeviceSource; /** * - Value of the input. */ value: number; /** * - Secondary value. */ value2: number; /** * - Type of input event. */ type: DeviceInputType; /** * - Whether it is pressure-sensitive. */ isPressure: boolean; /** * - Pressed status. */ pressed: boolean; /** * - Previous pressed status. */ prevPressed?: boolean | null | undefined; /** * - Timestamp of the event. */ timestamp: number; /** * - Reference to the gamepad. */ gp?: Gamepad | undefined; }; /** * Internal structure for analog input events. */ export type InputAnalogEvents = { /** * - Unique identifier for the input. */ id: string; /** * - Optional DOM event reference. */ event?: Event | undefined; /** * - Analog key. */ key: string; /** * - Source of input. */ source: DeviceSource; /** * - Main analog value. */ value: number; /** * - Secondary analog value. */ value2: number; /** * - Type of event. */ type: DeviceInputType; /** * - Timestamp. */ timestamp: number; /** * - Gamepad reference. */ gp?: Gamepad | undefined; }; /** * Payload for connection-related events. */ export type ConnectionPayload = { /** * - ID of the gamepad. */ id: string; /** * - Timestamp of the event. */ timestamp: number; /** * - Gamepad instance. */ gp: Gamepad; }; export type ExportedConfig = { /** * - The expected identifier for a specific gamepad device, or null if not set. */ expectedId: string | null; /** * - Array of device IDs to be ignored (excluded from input detection). */ ignoreIds: string[]; /** * - The threshold value below which analog stick inputs are ignored (dead zone). */ deadZone: number; /** * - Time in milliseconds allowed between consecutive inputs in a combo sequence before it resets. */ timeoutComboKeys: number; /** * - Sensitivity threshold for detecting significant analog axis movement (0 = most sensitive, 1 = least sensitive). */ axisActiveSensitivity: number; /** * - Array of key-value pairs representing the mapping between logical input names and physical input(s). */ inputMap: [string, string | string[]][]; }; /** * @typedef {Object} KeyStatus * Describes the status of a key or button. * @property {boolean} pressed - Whether the key is currently pressed. * @property {number} [value] - Optional analog value associated with the key. * @property {number} [value2] - Optional second analog value. */ /** * @typedef {'gamepad-only' | 'keyboard-only' | 'both'} InputMode * Defines the available input modes. */ /** * A callback function that is invoked when a mapped logical input is activated or deactivated. * * This function receives the logical name associated with the input (e.g., "Jump", "Shoot", "Menu") * and can be used to handle input-related actions such as triggering game mechanics or UI behavior. * * @typedef {(payload: { logicalName: string, activeTime: number, comboTime: number }) => void} MappedInputCallback */ /** * A callback function that is invoked when a mapped key is activated or deactivated. * * This function receives the key name associated with the input (e.g., "KeyA", "KeyB", "KeyC") * and can be used to handle input-related actions such as triggering game mechanics or UI behavior. * * @typedef {(payload: { key: string, activeTime: number }) => void} MappedKeyCallback */ /** * @typedef {(payload: InputPayload|InputAnalogPayload) => void} PayloadCallback * Callback for handling input events. */ /** * @typedef {(payload: ConnectionPayload) => void} ConnectionCallback * Callback for handling gamepad connection events. */ /** * A callback function that is triggered when a registered input sequence is fully activated. * * @callback InputSequenceCallback * @param {number} timestamp - The moment in milliseconds when the sequence was successfully detected. */ /** * A callback function that is triggered when a registered key sequence is fully activated. * * @callback KeySequenceCallback * @param {number} timestamp - The moment in milliseconds when the sequence was successfully detected. */ /** * Represents any valid callback type used in the TinyGamepad event system. * This is a union of all supported callback signatures. * @typedef {ConnectionCallback | PayloadCallback | MappedInputCallback | MappedKeyCallback} CallbackList */ /** * Represents a specific input source from a gamepad. * - 'gamepad-analog' refers to analog sticks or analog triggers. * - 'gamepad-button' refers to digital buttons on the gamepad. * @typedef {'gamepad-analog'|'gamepad-button'} GamepadDeviceSource */ /** * Represents any possible physical device source that can be used for input. * - 'mouse': Input from a mouse. * - 'keyboard': Input from a keyboard. * - 'gamepad-analog': Analog input from a gamepad (e.g., sticks, triggers). * - 'gamepad-button': Digital button input from a gamepad. * @typedef {'mouse'|'keyboard'|GamepadDeviceSource} DeviceSource */ /** * Represents the type of input interaction detected. * - 'up': Input was released. * - 'down': Input was pressed. * - 'hold': Input is being held. * - 'change': Input value changed (e.g., pressure or axis). * - 'move': Analog movement detected (e.g., joystick motion). * @typedef {'up'|'down'|'hold'|'change'|'move'} DeviceInputType */ /** * @typedef {Object} InputPayload * Structure for digital button input payload. * @property {string} id - Unique identifier for the input. * @property {Event} [event] - Optional DOM event reference. * @property {DeviceInputType} type - Type of input event (down, up, hold). * @property {DeviceSource} source - Input source (keyboard, mouse, gamepad). * @property {string} key - Physical input identifier. * @property {boolean} isPressure - Whether the input is pressure sensitive. * @property {string} logicalName - Logical name associated with the input. * @property {number} value - Primary analog value. * @property {number} value2 - Secondary analog value. * @property {boolean} pressed - Current pressed status. * @property {boolean|null} [prevPressed] - Previous pressed status. * @property {number} timestamp - Timestamp of the event. * @property {Gamepad} [gp] - Reference to the originating Gamepad. */ /** * @typedef {Object} InputAnalogPayload * Structure for analog input payload. * @property {string} id - Unique identifier for the input. * @property {Event} [event] - Optional DOM event reference. * @property {DeviceInputType} type - Type of input event (change). * @property {DeviceSource} source - Input source. * @property {string} key - Physical input identifier. * @property {string} logicalName - Logical name associated with the input. * @property {number} value - Analog value. * @property {number} value2 - Secondary analog value. * @property {number} timestamp - Timestamp of the event. * @property {Gamepad} [gp] - Reference to the originating Gamepad. */ /** * @typedef {Object} InputEvents * Internal structure for digital input events. * @property {string} id - Unique identifier for the input. * @property {Event} [event] - Optional DOM event reference. * @property {string} key - Input key identifier. * @property {DeviceSource} source - Source of the input. * @property {number} value - Value of the input. * @property {number} value2 - Secondary value. * @property {DeviceInputType} type - Type of input event. * @property {boolean} isPressure - Whether it is pressure-sensitive. * @property {boolean} pressed - Pressed status. * @property {boolean|null} [prevPressed] - Previous pressed status. * @property {number} timestamp - Timestamp of the event. * @property {Gamepad} [gp] - Reference to the gamepad. */ /** * @typedef {Object} InputAnalogEvents * Internal structure for analog input events. * @property {string} id - Unique identifier for the input. * @property {Event} [event] - Optional DOM event reference. * @property {string} key - Analog key. * @property {DeviceSource} source - Source of input. * @property {number} value - Main analog value. * @property {number} value2 - Secondary analog value. * @property {DeviceInputType} type - Type of event. * @property {number} timestamp - Timestamp. * @property {Gamepad} [gp] - Gamepad reference. */ /** * @typedef {Object} ConnectionPayload * Payload for connection-related events. * @property {string} id - ID of the gamepad. * @property {number} timestamp - Timestamp of the event. * @property {Gamepad} gp - Gamepad instance. */ /** * @typedef {Object} ExportedConfig * @property {string | null} expectedId - The expected identifier for a specific gamepad device, or null if not set. * @property {string[]} ignoreIds - Array of device IDs to be ignored (excluded from input detection). * @property {number} deadZone - The threshold value below which analog stick inputs are ignored (dead zone). * @property {number} timeoutComboKeys - Time in milliseconds allowed between consecutive inputs in a combo sequence before it resets. * @property {number} axisActiveSensitivity - Sensitivity threshold for detecting significant analog axis movement (0 = most sensitive, 1 = least sensitive). * @property {[string, string | string[]][]} inputMap - Array of key-value pairs representing the mapping between logical input names and physical input(s). */ /** * TinyGamepad is a high-level and extensible input management system * designed for professional-level control schemes in games or applications. * * It supports input from gamepads, keyboards, and optionally mouse devices. * Key features include: * - Input mapping from physical controls to logical action names * - Pressure-sensitive trigger and button support * - Dead zone configuration for analog inputs * - Event-based input handling (start, hold, end) * - Multiple logical names per input binding * - Unified control model for both digital and analog inputs * * TinyGamepad allows seamless integration of fallback control types, * detailed input feedback, and JSON-based profile serialization for saving and restoring mappings. */ declare class TinyGamepad { /** @type {Record<string, string>} */ static "__#private@#specialMap": Record<string, string>; /** * Add or update a special key mapping. * @param {string} char - The character to map. * @param {string} code - The corresponding key code. */ static addSpecialKey(char: string, code: string): void; /** * Remove a special key mapping. * @param {string} char - The character to remove from mapping. */ static removeSpecialKey(char: string): void; /** * Get the mapped code for a special character. * @param {string} char - The character to look up. * @returns {string | undefined} - The mapped code or undefined if not found. */ static getSpecialKey(char: string): string | undefined; /** * Get all current special key mappings. * @returns {Record<string, string>} */ static getAllSpecialKeys(): Record<string, string>; /** * Converts a string into an array of TinyGamepad-style key codes. * Example: "pudding" → ['KeyP', 'KeyU', 'KeyD', 'KeyD', 'KeyI', 'KeyN', 'KeyG'] * @param {string} text - Input text. * @returns {string[]} Array of key codes. */ static stringToKeys(text: string): string[]; /** * Initializes a new instance of TinyGamepad with customizable input behavior. * * This constructor allows configuring the expected device ID, the type of inputs to listen for * (keyboard, gamepad, or both), analog dead zone sensitivity, and whether to allow mouse input. * It also supports filtering out specific devices by ID. * * @param {Object} options - Configuration object for TinyGamepad behavior. * @param {string | null} [options.expectedId=null] - Specific controller ID to expect. * @param {InputMode} [options.inputMode='both'] - Mode of input to use. * @param {string[]} [options.ignoreIds=[]] - List of device IDs to ignore. * @param {number} [options.deadZone=0.1] - Analog stick dead zone threshold. * @param {boolean} [options.allowMouse=false] - Whether mouse events should be treated as input triggers. * @param {number} [options.timeoutComboKeys=500] - Maximum time (in milliseconds) allowed between inputs in a key sequence before the reset time. * @param {number} [options.axisActiveSensitivity=0.3] - Threshold to detect meaningful axis movement (0 = most sensitive, 1 = least sensitive). * @param {Window|Element} [options.elementBase=window] - The DOM element or window to bind keyboard and mouse events to. */ constructor({ expectedId, inputMode, ignoreIds, deadZone, axisActiveSensitivity, timeoutComboKeys, allowMouse, elementBase, }?: { expectedId?: string | null | undefined; inputMode?: InputMode | undefined; ignoreIds?: string[] | undefined; deadZone?: number | undefined; allowMouse?: boolean | undefined; timeoutComboKeys?: number | undefined; axisActiveSensitivity?: number | undefined; elementBase?: Element | Window | undefined; }); /** * Waits for a single input event from the user and resolves with detailed input information. * This is typically used in control configuration screens to allow the user to choose an input * (keyboard, mouse, or gamepad) that will be mapped to a logical action. * * The function listens for the first eligible input (ignores 'MouseMove') and returns the key, * input source, and the Gamepad object if applicable. If no input is received before the timeout, * the promise resolves with a `null` key and source. * * @param {object} [options] - Optional configuration for input capture behavior. * @param {number} [options.timeout=10000] - Timeout in milliseconds before the promise resolves automatically with null values. * @param {string} [options.eventName='MappingInput'] - The temporary logical event name used internally to listen for input. * @param {boolean} [options.canMove=false] - Whether movement-based inputs (e.g., mouse movement) are allowed. * @returns {Promise<{ key: string | null, source: DeviceSource | null, gp?: Gamepad }>} * A promise that resolves with an object containing: * - `key`: the identifier of the pressed input (e.g., "KeyW", "Button0", "LeftClick"), * - `source`: the origin of the input ("keyboard", "mouse", "gamepad-button", or "gamepad-analog"), * - `gp`: the Gamepad object (only if the input source is a gamepad). */ awaitInputMapping({ timeout, eventName, canMove }?: { timeout?: number | undefined; eventName?: string | undefined; canMove?: boolean | undefined; }): Promise<{ key: string | null; source: DeviceSource | null; gp?: Gamepad; }>; /** * Assigns a physical input to a logical name (e.g., "Jump" => "Button1") * @param {string} logicalName * @param {string|string[]} physicalInput */ mapInput(logicalName: string, physicalInput: string | string[]): void; /** * Removes a logical input mapping * @param {string} logicalName */ unmapInput(logicalName: string): void; /** * Checks if a logical name is mapped to any physical input. * @param {string} logicalName * @returns {boolean} */ hasMappedInput(logicalName: string): boolean; /** * Returns the physical input(s) mapped to a given logical name. * @param {string} logicalName * @returns {string | string[]} */ getMappedInput(logicalName: string): string | string[]; /** * Clears all mappings for all logical inputs. */ clearMapInputs(): void; /** * Registers a sequence of logical inputs that triggers a specific callback. * @param {string[]} sequence - Ordered list of logical input names (e.g., ['Jump', 'Action']) * @param {InputSequenceCallback} callback - Function to invoke when the sequence is fully held */ registerInputSequence(sequence: string[], callback: InputSequenceCallback): void; /** * Unregisters a previously registered input sequence. * @param {string[]} sequence - The sequence to remove from detection */ unregisterInputSequence(sequence: string[]): void; /** * Removes all registered input sequences. */ unregisterAllInputSequences(): void; /** * Checks whether a given input sequence is currently registered. * @param {string[]} sequence - The sequence to check * @returns {boolean} */ hasInputSequence(sequence: string[]): boolean; /** * Registers a sequence of logical inputs that triggers a specific callback. * @param {string[]} sequence - Ordered list of logical input names (e.g., ['Jump', 'Action']) * @param {InputSequenceCallback} callback - Function to invoke when the sequence is fully held */ registerKeySequence(sequence: string[], callback: InputSequenceCallback): void; /** * Unregisters a previously registered input sequence. * @param {string[]} sequence - The sequence to remove from detection */ unregisterKeySequence(sequence: string[]): void; /** * Removes all registered input sequences. */ unregisterAllKeySequences(): void; /** * Checks whether a given input sequence is currently registered. * @param {string[]} sequence - The sequence to check * @returns {boolean} */ hasKeySequence(sequence: string[]): boolean; /** * Renew the currently held combo logical keys. */ renewComboMapped(): void; /** * Resets the currently held key combo logical inputs. */ resetComboMapped(): void; /** * Registers a callback for when a mapped key is activated (pressed down) * @param {MappedInputCallback} callback */ onMappedKeyStart(callback: MappedInputCallback): void; /** * Registers a one-time callback for the "mapped-key-start" event. * The callback will be automatically removed after it runs once. * @param {MappedInputCallback} callback */ onceMappedKeyStart(callback: MappedInputCallback): void; /** * Prepends a callback to the "mapped-key-start" event. * @param {MappedInputCallback} callback */ prependMappedKeyStart(callback: MappedInputCallback): void; /** * Removes a callback from the "mapped-key-start" event. * @param {MappedInputCallback} callback */ offMappedKeyStart(callback: MappedInputCallback): void; /** * Removes all callbacks from the "mapped-key-start" event. */ offAllMappedKeyStart(): void; /** * Registers a callback for when a mapped key is deactivated (released) * @param {MappedInputCallback} callback */ onMappedKeyEnd(callback: MappedInputCallback): void; /** * Registers a one-time callback for the "mapped-key-end" event. * The callback will be automatically removed after it runs once. * @param {MappedInputCallback} callback */ onceMappedKeyEnd(callback: MappedInputCallback): void; /** * Prepends a callback to the "mapped-key-end" event. * @param {MappedInputCallback} callback */ prependMappedKeyEnd(callback: MappedInputCallback): void; /** * Removes a callback from the "mapped-key-end" event. * @param {MappedInputCallback} callback */ offMappedKeyEnd(callback: MappedInputCallback): void; /** * Removes all callbacks from the "mapped-key-end" event. */ offAllMappedKeyEnd(): void; /** * Registers a callback for when a mapped input is activated (pressed down) * @param {MappedInputCallback} callback */ onMappedInputStart(callback: MappedInputCallback): void; /** * Registers a one-time callback for the "mapped-input-start" event. * The callback will be automatically removed after it runs once. * @param {MappedInputCallback} callback */ onceMappedInputStart(callback: MappedInputCallback): void; /** * Prepends a callback to the "mapped-input-start" event. * @param {MappedInputCallback} callback */ prependMappedInputStart(callback: MappedInputCallback): void; /** * Removes a callback from the "mapped-input-start" event. * @param {MappedInputCallback} callback */ offMappedInputStart(callback: MappedInputCallback): void; /** * Removes all callbacks from the "mapped-input-start" event. */ offAllMappedInputStart(): void; /** * Registers a callback for when a mapped input is deactivated (released) * @param {MappedInputCallback} callback */ onMappedInputEnd(callback: MappedInputCallback): void; /** * Registers a one-time callback for the "mapped-input-end" event. * The callback will be automatically removed after it runs once. * @param {MappedInputCallback} callback */ onceMappedInputEnd(callback: MappedInputCallback): void; /** * Prepends a callback to the "mapped-input-end" event. * @param {MappedInputCallback} callback */ prependMappedInputEnd(callback: MappedInputCallback): void; /** * Removes a callback from the "mapped-input-end" event. * @param {MappedInputCallback} callback */ offMappedInputEnd(callback: MappedInputCallback): void; /** * Removes all callbacks from the "mapped-input-end" event. */ offAllMappedInputEnd(): void; /** * Registers a callback for a logical input * @param {string} logicalName * @param {PayloadCallback} callback */ onInput(logicalName: string, callback: PayloadCallback): void; /** * Registers a one-time callback for a logical input. * The callback is removed after the first invocation. * @param {string} logicalName * @param {PayloadCallback} callback */ onceInput(logicalName: string, callback: PayloadCallback): void; /** * Prepends a callback to the input event list. * @param {string} logicalName * @param {PayloadCallback} callback */ prependInput(logicalName: string, callback: PayloadCallback): void; /** * Removes a callback from a specific logical input event. * @param {string} logicalName * @param {PayloadCallback} callback */ offInput(logicalName: string, callback: PayloadCallback): void; /** * Registers a callback for the "input-start" event of a logical name * @param {string} logicalName * @param {PayloadCallback} callback */ onInputStart(logicalName: string, callback: PayloadCallback): void; /** * Registers a one-time callback for the "input-start" event. * @param {string} logicalName * @param {PayloadCallback} callback */ onceInputStart(logicalName: string, callback: PayloadCallback): void; /** * Prepends a callback to the "input-start" event list. * @param {string} logicalName * @param {PayloadCallback} callback */ prependInputStart(logicalName: string, callback: PayloadCallback): void; /** * Removes a callback from a specific logical "input-start" event. * @param {string} logicalName * @param {PayloadCallback} callback */ offInputStart(logicalName: string, callback: PayloadCallback): void; /** * Registers a callback for the "input-end" event of a logical name * @param {string} logicalName * @param {PayloadCallback} callback */ onInputEnd(logicalName: string, callback: PayloadCallback): void; /** * Registers a one-time callback for the "input-end" event. * @param {string} logicalName * @param {PayloadCallback} callback */ onceInputEnd(logicalName: string, callback: PayloadCallback): void; /** * Prepends a callback to the "input-end" event list. * @param {string} logicalName * @param {PayloadCallback} callback */ prependInputEnd(logicalName: string, callback: PayloadCallback): void; /** * Removes a callback from a specific logical "input-end" event. * @param {string} logicalName * @param {PayloadCallback} callback */ offInputEnd(logicalName: string, callback: PayloadCallback): void; /** * Registers a callback for the "input-hold" event of a logical name * @param {string} logicalName * @param {PayloadCallback} callback */ onInputHold(logicalName: string, callback: PayloadCallback): void; /** * Registers a one-time callback for the "input-hold" event. * @param {string} logicalName * @param {PayloadCallback} callback */ onceInputHold(logicalName: string, callback: PayloadCallback): void; /** * Prepends a callback to the "input-hold" event list. * @param {string} logicalName * @param {PayloadCallback} callback */ prependInputHold(logicalName: string, callback: PayloadCallback): void; /** * Removes a callback from a specific logical "input-hold" event. * @param {string} logicalName * @param {PayloadCallback} callback */ offInputHold(logicalName: string, callback: PayloadCallback): void; /** * Registers a callback for the "input-change" event of a logical name * @param {string} logicalName * @param {PayloadCallback} callback */ onInputChange(logicalName: string, callback: PayloadCallback): void; /** * Registers a one-time callback for the "input-change" event. * @param {string} logicalName * @param {PayloadCallback} callback */ onceInputChange(logicalName: string, callback: PayloadCallback): void; /** * Prepends a callback to the "input-change" event list. * @param {string} logicalName * @param {PayloadCallback} callback */ prependInputChange(logicalName: string, callback: PayloadCallback): void; /** * Removes a callback from a specific logical "input-change" event. * @param {string} logicalName * @param {PayloadCallback} callback */ offInputChange(logicalName: string, callback: PayloadCallback): void; /** * Registers a callback for the "input-move" event of a logical name * @param {string} logicalName * @param {PayloadCallback} callback */ onInputMove(logicalName: string, callback: PayloadCallback): void; /** * Registers a one-time callback for the "input-move" event. * @param {string} logicalName * @param {PayloadCallback} callback */ onceInputMove(logicalName: string, callback: PayloadCallback): void; /** * Prepends a callback to the "input-move" event list. * @param {string} logicalName * @param {PayloadCallback} callback */ prependInputMove(logicalName: string, callback: PayloadCallback): void; /** * Removes a callback from a specific logical "input-move" event. * @param {string} logicalName * @param {PayloadCallback} callback */ offInputMove(logicalName: string, callback: PayloadCallback): void; /** * Returns a shallow clone of the callback list for a given logical input and event type. * @param {string} logicalName * @param {'all' | 'start' | 'end' | 'hold' | 'change' | 'move'} [type='all'] * @returns {Function[]} */ getCalls(logicalName: string, type?: "all" | "start" | "end" | "hold" | "change" | "move"): Function[]; /** * Removes all callbacks for a specific logical input event. * @param {string} logicalName * @param {'all'| 'start' | 'end' | 'hold' | 'change' | 'move'} [type='all'] */ offAllInputs(logicalName: string, type?: "all" | "start" | "end" | "hold" | "change" | "move"): void; /** * Returns the number of registered callbacks for a specific logical input and type. * @param {string} logicalName * @param {'all' | 'start' | 'end' | 'hold' | 'change' | 'move'} [type='all'] * @returns {number} */ getCallSize(logicalName: string, type?: "all" | "start" | "end" | "hold" | "change" | "move"): number; /** * Returns the current default haptic feedback effect configuration. * * This includes the effect type (e.g., 'dual-rumble') and its parameters * such as duration, delay, and vibration intensities (weak and strong). * * The returned object is a shallow clone to prevent external mutation. * * @returns {{ type: GamepadHapticEffectType; params: GamepadEffectParameters; }} The default haptic effect settings. */ get defaultHapticEffect(): { type: GamepadHapticEffectType; params: GamepadEffectParameters; }; /** * Sets the default haptic feedback effect configuration. * @param {GamepadHapticEffectType} type - Type of effect. * @param {GamepadEffectParameters} params - Effect parameters. */ setDefaultHapticEffect(type: GamepadHapticEffectType, params: GamepadEffectParameters): void; /** * Checks if the connected gamepad supports haptic feedback. * @returns {boolean} */ hasHapticEffect(): boolean; /** * Triggers a haptic feedback vibration using provided or default settings. * @param {GamepadEffectParameters} [params] - Custom parameters. * @param {GamepadHapticEffectType} [type] - Custom type. * @returns {Promise<GamepadHapticsResult>} */ vibrate(params?: GamepadEffectParameters, type?: GamepadHapticEffectType): Promise<GamepadHapticsResult>; /** * Adds an ID to the ignored list * @param {string} id */ ignoreId(id: string): void; /** * Removes an ID from the ignored list * @param {string} id */ unignoreId(id: string): void; /** * Registers a callback for the "connected" event * @param {ConnectionCallback} callback */ onConnected(callback: ConnectionCallback): void; /** * Registers a one-time callback for the "connected" event. * The callback will be automatically removed after it runs once. * @param {ConnectionCallback} callback */ onceConnected(callback: ConnectionCallback): void; /** * Prepends a callback to the "connected" event. * @param {ConnectionCallback} callback */ prependConnected(callback: ConnectionCallback): void; /** * Removes a callback from the "connected" event. * @param {ConnectionCallback} callback */ offConnected(callback: ConnectionCallback): void; /** * Removes all callbacks from the "connected" event. */ offAllConnected(): void; /** * Registers a callback for the "disconnected" event * @param {ConnectionCallback} callback */ onDisconnected(callback: ConnectionCallback): void; /** * Registers a one-time callback for the "disconnected" event. * The callback will be automatically removed after it runs once. * @param {ConnectionCallback} callback */ onceDisconnected(callback: ConnectionCallback): void; /** * Prepends a callback to the "disconnected" event. * @param {ConnectionCallback} callback */ prependDisconnected(callback: ConnectionCallback): void; /** * Removes a callback from the "disconnected" event. * @param {ConnectionCallback} callback */ offDisconnected(callback: ConnectionCallback): void; /** * Removes all callbacks from the "disconnected" event. */ offAllDisconnected(): void; /** * Returns whether a gamepad is currently connected. * @returns {boolean} */ hasGamepad(): boolean; /** * Returns the currently connected Gamepad instance. * Throws if no gamepad is connected. * @returns {Gamepad} */ getGamepad(): Gamepad; /** * Checks if there is a recorded last state for a given button index. * @param {number} index - The index of the button to check. * @returns {boolean} True if a last state exists for the specified button index; otherwise, false. */ hasLastButtonState(index: number): boolean; /** * Retrieves the last recorded state for a given button index. * Throws an error if the button index has no recorded state. * @param {number} index - The index of the button to retrieve. * @returns {KeyStatus} A copy of the last known state for the specified button index. * @throws {Error} If no last state exists for the specified button index. */ getLastButtonState(index: number): KeyStatus; /** * Exports the current TinyGamepad configuration as a plain object suitable for serialization. * Includes device filters, input mappings, and various sensitivity settings. * @returns {ExportedConfig} The current configuration snapshot. */ exportConfig(): ExportedConfig; /** * Imports and applies a configuration object or JSON string to update TinyGamepad settings. * Validates the input to ensure proper types and throws if invalid. * @param {string | ExportedConfig} json - JSON string or configuration object to apply. * @throws {TypeError} Throws if the provided argument is not a valid JSON string or configuration object, * or if any property has an incorrect type. */ importConfig(json: string | ExportedConfig): void; /** * Returns a cloned list of the "mapped-key-start" event callbacks. * @returns {MappedInputCallback[]} */ get mappedKeyStartCalls(): MappedInputCallback[]; /** * Returns a cloned list of the "mapped-key-end" event callbacks. * @returns {MappedInputCallback[]} */ get mappedKeyEndCalls(): MappedInputCallback[]; /** * Returns a clone of currently held key combo logical inputs. * @returns {string[]} */ get comboMappedKeys(): string[]; /** * Returns a clone of currently held combo logical inputs. * @returns {string[]} */ get comboMappedInputs(): string[]; /** * Returns the number of input sequences currently registered. * @returns {number} */ get keySequenceSize(): number; /** * Returns a shallow clone of all input sequences and their associated data. * @returns {InputSequenceCallback[]} */ get keySequences(): InputSequenceCallback[]; /** * Returns a clone of currently held mapped logical keys. * @returns {string[]} */ get activeMappedKeys(): string[]; /** * Returns a cloned list of the "mapped-input-start" event callbacks. * @returns {MappedInputCallback[]} */ get mappedInputStartCalls(): MappedInputCallback[]; /** * Returns a cloned list of the "mapped-input-end" event callbacks. * @returns {MappedInputCallback[]} */ get mappedInputEndCalls(): MappedInputCallback[]; /** * Returns the number of input sequences currently registered. * @returns {number} */ get inputSequenceSize(): number; /** * Returns a shallow clone of all input sequences and their associated data. * @returns {InputSequenceCallback[]} */ get inputSequences(): InputSequenceCallback[]; /** * Returns a clone of currently held mapped logical inputs. * @returns {string[]} */ get activeMappedInputs(): string[]; /** * Returns a shallow clone of all logical-to-physical input mappings as a plain object. * @returns {{ [logicalName: string]: string | string[] }} */ get mappedInputs(): { [logicalName: string]: string | string[]; }; /** * Returns the number of logical inputs currently mapped. * @returns {number} */ get mappedInputSize(): number; /** * Returns a cloned list of the "connected" event callbacks. * @returns {ConnectionCallback[]} */ get connectedCalls(): ConnectionCallback[]; /** * Returns a cloned list of the "disconnected" event callbacks. * @returns {ConnectionCallback[]} */ get disconnectedCalls(): ConnectionCallback[]; /** * Returns a shallow clone of the set of ignored device IDs. * @returns {string[]} */ get ignoredDeviceIds(): string[]; /** * Returns a shallow clone of the currently held keys. * @returns {string[]} */ get heldKeys(): string[]; /** * Returns the total number of sub event keys inside all events currently registered. * @returns {number} */ get eventsSize(): number; /** * Returns the total number of unique event keys currently registered. * @returns {number} */ get callSize(): number; /** * Returns the number of callbacks registered for the "connected" event. * @returns {number} */ get connectedCallSize(): number; /** * Returns the number of callbacks registered for the "disconnected" event. * @returns {number} */ get disconnectedCallSize(): number; /** * Returns the number of callbacks registered for the "mapped-key-start" event. * @returns {number} */ get mappedKeyStartCallSize(): number; /** * Returns the number of callbacks registered for the "mapped-key-end" event. * @returns {number} */ get mappedKeyEndCallSize(): number; /** * Returns the number of callbacks registered for the "mapped-input-start" event. * @returns {number} */ get mappedInputStartCallSize(): number; /** * Returns the number of callbacks registered for the "mapped-input-end" event. * @returns {number} */ get mappedInputEndCallSize(): number; /** * Retrieves a snapshot of the most recent button states. * Each element represents the status of a specific button at the last update cycle. * @returns {KeyStatus[]} An array of button states from the last poll. */ get lastButtonStates(): KeyStatus[]; /** * Retrieves a snapshot of the most recent axis values. * Each element is a numeric value representing the position or tilt of a specific axis at the last update cycle. * @returns {number[]} An array of axis values from the last poll. */ get lastAxes(): number[]; /** * Returns the current input mode (e.g., 'keyboard-only', 'gamepad-only', or 'both'). * @returns {InputMode} */ get inputMode(): InputMode; /** * Returns the base element or window used for event binding. * @returns {Window|Element} */ get elementBase(): Window | Element; /** * Returns the timestamp of the last mapped input combo. * @returns {number} */ get timeComboInputs(): number; /** * Returns the timestamp of the last raw key combo. * @returns {number} */ get timeComboKeys(): number; /** * Returns the timestamp of the last mapped input activity. * @returns {number} */ get timeMappedInputs(): number; /** * Sets the timeout duration (in milliseconds) before raw key combos are reset. * Must be a positive number. * @param {number} value */ set timeoutComboKeys(value: number); /** * Returns the timeout duration (in milliseconds) before raw key combos are reset. * @returns {number} */ get timeoutComboKeys(): number; /** * Sets the sensitivity threshold used to detect active axis input. * This value defines how far an analog axis (such as a thumbstick or trigger) must move from its rest position * before being considered as a valid input. * * Values must be between 0 and 1. An error is thrown if the value is outside this range or not a number. * * @param {number} value - The new sensitivity value between 0 and 1. */ set axisActiveSensitivity(value: number); /** * Gets the sensitivity threshold used to detect when an axis (like a thumbstick or trigger) * is considered "actively moved". This helps distinguish minor noise from intentional input. * * A value of 0 means even the slightest movement is detected; a value closer to 1 means only strong input is accepted. * * @returns {number} The current axis activity sensitivity (between 0 and 1). */ get axisActiveSensitivity(): number; /** * Sets a new dead zone threshold for analog inputs. * Must be a number between 0 and 1. * @param {number} value */ set deadZone(value: number); /** * Returns the current dead zone threshold for analog inputs. * @returns {number} */ get deadZone(): number; /** * Sets the expected device ID. * @returns {string} */ set expectedId(value: string | null); /** * Returns the expected device ID, or null if any is accepted. * @returns {string|null} */ get expectedId(): string | null; /** * Returns whether the instance has been destroyed * @returns {boolean} */ get isDestroyed(): boolean; /** * Clears internal state and stops any ongoing input monitoring or loops. * Marks the instance as destroyed. */ destroy(): void; #private; } //# sourceMappingURL=TinyGamepad.d.mts.map