blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
51 lines (50 loc) • 1.7 kB
TypeScript
/**
* A callback to be executed on key events.
*/
export declare type KeyCallback = (pressed: boolean) => void;
/**
* Handles keyboard events for an {@link HTMLElement}.
*/
export default class KeyboardHandler {
readonly element: HTMLElement;
keys: {
[index: string]: boolean;
};
listeners: {
[index: string]: KeyCallback[];
};
constructor(element: HTMLElement);
private addListeners;
/**
* Determines wether or not a key is currently pressed.
*
* @see [Key Codes](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code)
*
* @param code The key code to check
* @returns Wether or not the key corresponding to the given code is pressed.
*/
isPressed(code: string): boolean;
/**
* Emulates a keydown/keyup event on the pages body for a given key code.
*
* @see [Key Codes](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code)
*
* @param code The key code to emulate
* @param pressed When true a `keydown` event is emulated otherwise `keyup` is emulated
*/
setKey(code: string, pressed: boolean): void;
/**
* Attaches a listener to a given key code that is called whenever the state of the key changes.
*
* @param code The key code to attach the listener to
* @param cb The callback to execute on a keydown/keyup event
*/
addListener(code: string, cb: KeyCallback): void;
/**
* Removes a listener from a given key code.
*
* @param code The key code the listener is attached to
* @param cb The attached listener
*/
removeListener(code: string, cb: KeyCallback): void;
}