malwoden
Version:
   
148 lines (147 loc) • 5.94 kB
TypeScript
import { KeyCode } from "./keycode";
export interface KeyboardContextCallback {
(event: KeyboardHandlerEvent): void;
}
export interface KeyboardHandlerEvent {
key: KeyCode;
repeat: boolean;
metaKey: boolean;
shiftKey: boolean;
ctrlKey: boolean;
event: "keydown" | "keyup";
}
/**
* Represents a global keyboard. Will likely only create one per app.
*
* You can bind/switch KeyboardContexts to the Keyboard handler
* to change input 'modes'.
*/
export declare class KeyboardHandler {
private _context?;
private _isDown;
private _stopPropagation;
/** Creates a new KeyboardHandler */
constructor(config?: {
stopPropagation: boolean;
});
private static keyEventFromDomEvent;
private _checkPropagation;
private onKeyDownEvent;
private onKeyUpEvent;
/**
* Sets the active context of the keyboard handler.
* @param context KeyboardContext - The context to use
*/
setContext(context: KeyboardContext): void;
/**
* Clears the active context for the keyboard handler.
*/
clearContext(): KeyboardContext | undefined;
/**
* Returns if a key is currently held down.
* @param keyCode KeyCode | number - The keyCode to check
*/
isKeyDown(keyCode: number): boolean;
/**
* Returns a promise that will resolve the next time a given keyCode is released.
* If no keyCode is given, it will return the next keyCode released.
* @param keyCode - The keyCode to listen to. If not provided, will monitor for any keycode.
* @returns - The pressed keyCode.
*/
waitForKeyUp(keyCode?: number): Promise<number>;
/**
* Returns a promise that will resolve the next time a given keyCode is pressed down. If
* no keyCode is given, it will return the next keyCode pressed.
* @param keyCode
* @returns - The pressed keyCode.
*/
waitForKeyDown(keyCode?: number): Promise<number>;
}
/**
* KeyboardContext represents a single 'mode' of the game's keyboard controls.
* For instance, you might have one context to use for the overworld, another for
* menus, another for inventory, etc.
*
* You can set the active context through a KeyboardHandler's setContext(ctx)
* and clearContext() methods.
*
* Normally the onDown/onUp callbacks can be used to listen for specific keys,
* but for more control onAnyDown/onAnyUp can be used to register callbacks
* that fire on any key press. These 'any' callbacks will always be the first
* to be called during a new key press.
*
* You can register multiple 'any' callbacks or key-specific callbacks for rare
* patterns,for instance registering 2 callbacks to fire when the 'E' key is
* pressed, etc.
*/
export declare class KeyboardContext {
private _onDown;
private _onUp;
private _anyKey;
private _getOnDownFns;
private _getOnUpFns;
private _addOnDown;
private _addOnUp;
/**
* Fires callback on a specific key down. Can call multiple times to register multiple callbacks.
* @param key KeyCode | number - The Key to monitor
* @param callback (KeyHandlerEvent) => void - The callback
* @returns - The KeyboardContext
*/
onDown(key: KeyCode | number, callback: KeyboardContextCallback): KeyboardContext;
/**
* Clear an existing callback for a key. If no callback is provided, will clear
* all callbacks on that key.
* @param key KeyCode - The keycode to clear onDown.
* @param callback If no existing event is provided, will clear all
*/
clearOnDown(key: KeyCode | number, callback?: KeyboardContextCallback): void;
/**
* Fires callback on any key down. Can call multiple times to register multiple callbacks.
* @param callback (KeyHandlerEvent) => void - The callback
* @returns - The KeyboardContext
*/
onAnyDown(callback: KeyboardContextCallback): KeyboardContext;
/**
* Clear a callback listening for any key. If no callback is provided, will clear
* all callbacks on listening for any keys.
* @param callback If no existing event is provided, will clear all
*/
clearOnAnyDown(callback?: KeyboardContextCallback): void;
/**
* Fires callback on a specific key up. Can call multiple times to register multiple callbacks.
* @param key KeyCode | number - The Key to monitor
* @param callback (KeyHandlerEvent) => void - The callback
* @returns - The KeyboardContext
*/
onUp(key: KeyCode | number, callback: KeyboardContextCallback): KeyboardContext;
/**
* Clear an existing callback for a key. If no callback is provided, will clear
* all callbacks on that key.
* @param key KeyCode - The keycode to clear onUp.
* @param callback If no existing event is provided, will clear all
*/
clearOnUp(key: KeyCode | number, callback?: KeyboardContextCallback): void;
/**
* Fires callback on any key up. Can call multiple times to register multiple callbacks.
* @param callback (KeyHandlerEvent) => void - The callback
* @returns - The KeyboardContext
*/
onAnyUp(callback: KeyboardContextCallback): KeyboardContext;
/**
* Clear a callback listening for any key. If no callback is provided, will clear
* all callbacks on listening for any keys.
* @param callback If no existing event is provided, will clear all
*/
clearOnAnyUp(callback?: KeyboardContextCallback): void;
/**
* Programmatically call all callbacks as if a key was pressed.
* @param key KeyHandlerEvent
*/
callOnDown(event: KeyboardHandlerEvent): void;
/**
* Programmatically call all callbacks as if a key was lifted.
* @param key KeyHandlerEvent
*/
callOnUp(event: KeyboardHandlerEvent): void;
}