
Input manager. Used to recieve input from keyboard and mouse.
To access the Input manager use Shaku.input.
Kind: global class
BooleanBooleanBooleanBooleanBooleanBooleanNumberStringVector2BooleanBooleanBooleanVector2Vector2Vector2BooleanBooleanBooleanBooleanBooleanBooleanBooleanBooleanNumberNumberGamepadArray.<String>BooleanBooleanBooleanBooleanbooleanBooleanBooleanBooleanBooleanBooleanBooleanNumberNumberBooleanBooleanCreate the manager.
BooleanIf true, will prevent default input events by calling preventDefault().
Kind: instance property of Input
BooleanBy default, when holding wheel button down browsers will turn into special page scroll mode and will not emit mouse move events. if this property is set to true (default), the Input manager will prevent this behavior, so we could still get mouse delta while mouse wheel is held down.
Kind: instance property of Input
BooleanIf true (default), will disable the context menu (what typically opens when you right click the page).
Kind: instance property of Input
BooleanIf true (default), will treat touch events (touch start / touch end / touch move) as if the user clicked and moved a mouse.
Kind: instance property of Input
BooleanIf true (default), will delegate events from mapped gamepads to custom keys. This will add the following codes to all basic query methods (down, pressed, released, doublePressed, doubleReleased):
gamepad is the gamepad index: gamepad0, gamepad1, gamepad2..Kind: instance property of Input
BooleanIf true (default), will reset all states if the window loses focus.
Kind: instance property of Input
NumberDefault time, in milliseconds, to consider two consecutive key presses as a double-press.
Kind: instance property of Input
Get the Mouse Buttons enum.
Kind: instance property of Input
See: MouseButtons
Get the Keyboard Buttons enum.
Kind: instance property of Input
See: KeyboardKeys
StringReturn the string code to use in order to get touch events.
Kind: instance property of Input
Returns: String - Key code to use for touch events.
Vector2Get touch screen touching position. Note: if not currently touching, will return last known position.
Kind: instance property of Input
Returns: Vector2 - Touch position.
BooleanGet if currently touching a touch screen.
Kind: instance property of Input
Returns: Boolean - True if currently touching the screen.
BooleanGet if started touching a touch screen in current frame.
Kind: instance property of Input
Returns: Boolean - True if started touching the screen now.
BooleanGet if stopped touching a touch screen in current frame.
Kind: instance property of Input
Returns: Boolean - True if stopped touching the screen now.
Vector2Get mouse position.
Kind: instance property of Input
Returns: Vector2 - Mouse position.
Vector2Get mouse previous position (before the last endFrame() call).
Kind: instance property of Input
Returns: Vector2 - Mouse position in previous frame.
Vector2Get mouse movement since last endFrame() call.
Kind: instance property of Input
Returns: Vector2 - Mouse change since last frame.
BooleanGet if mouse is currently moving.
Kind: instance property of Input
Returns: Boolean - True if mouse moved since last frame, false otherwise.
BooleanGet if any of the shift keys are currently down.
Kind: instance property of Input
Returns: Boolean - True if there’s a shift key pressed down.
BooleanGet if any of the Ctrl keys are currently down.
Kind: instance property of Input
Returns: Boolean - True if there’s a Ctrl key pressed down.
BooleanGet if any of the Alt keys are currently down.
Kind: instance property of Input
Returns: Boolean - True if there’s an Alt key pressed down.
BooleanGet if any keyboard key was pressed this frame.
Kind: instance property of Input
Returns: Boolean - True if any key was pressed down this frame.
BooleanGet if any keyboard key is currently down.
Kind: instance property of Input
Returns: Boolean - True if there’s a key pressed down.
BooleanGet if any mouse button was pressed this frame.
Kind: instance property of Input
Returns: Boolean - True if any of the mouse buttons were pressed this frame.
BooleanGet if any mouse button is down.
Kind: instance property of Input
Returns: Boolean - True if any of the mouse buttons are pressed.
NumberGet mouse wheel sign.
Kind: instance property of Input
Returns: Number - Mouse wheel sign (-1 or 1) for wheel scrolling that happened during this frame.
Will return 0 if mouse wheel is not currently being used.
NumberGet mouse wheel value.
Kind: instance property of Input
Returns: Number - Mouse wheel value.
Set the target element to attach input to. If not called, will just use the entire document. Must be called before initializing Shaku. This can also be a method to invoke while initializing.
Kind: instance method of Input
| Param | Type | Description |
|---|---|---|
| element | Element | elementCallback |
Element to attach input to. |
Example
// the following will use whatever canvas the gfx manager uses as input element.
// this means mouse offset will also be relative to this element.
Shaku.input.setTargetElement(() => Shaku.gfx.canvas);
GamepadGet Gamepad current states, or null if not connected. Note: this object does not update itself, you’ll need to query it again every frame.
Kind: instance method of Input
Returns: Gamepad - Gamepad current state.
| Param | Type | Description |
|---|---|---|
| [index] | Number |
Gamepad index or undefined for first connected device. |
Get gamepad id, or null if not connected to this slot.
Kind: instance method of Input
Returns: Gamepad id or null.
| Param | Type | Description |
|---|---|---|
| [index] | Number |
Gamepad index or undefined for first connected device. |
Array.<String>Return a list with connected devices ids.
Kind: instance method of Input
Returns: Array.<String> - List of connected devices ids.
Set a custom key code state you can later use with all the built in methods (down / pressed / released / doublePressed, etc.)
For example, lets say you want to implement a simulated keyboard and use it alongside the real keyboard.
When your simulated keyboard space key is pressed, you can call setCustomState('sim_space', true). When released, call setCustomState('sim_space', false).
Now you can use Shaku.input.down(['space', 'sim_space']) to check if either a real space or simulated space is pressed down.
Kind: instance method of Input
| Param | Type | Description |
|---|---|---|
| code | String |
Code to set state for. |
| value | Boolean | null |
Current value to set, or null to remove custom key. |
BooleanGet if mouse button was pressed this frame.
Kind: instance method of Input
Returns: Boolean - True if mouse button is currently down, but was up in previous frame.
| Param | Type | Default | Description |
|---|---|---|---|
| button | MouseButton |
0 |
Button code (defults to MouseButtons.left). |
BooleanGet if mouse button is currently pressed.
Kind: instance method of Input
Returns: Boolean - true if mouse button is currently down, false otherwise.
| Param | Type | Default | Description |
|---|---|---|---|
| button | MouseButton |
0 |
Button code (defults to MouseButtons.left). |
BooleanGet if mouse button is currently not down.
Kind: instance method of Input
Returns: Boolean - true if mouse button is currently up, false otherwise.
| Param | Type | Default | Description |
|---|---|---|---|
| button | MouseButton |
0 |
Button code (defults to MouseButtons.left). |
BooleanGet if mouse button was released in current frame.
Kind: instance method of Input
Returns: Boolean - True if mouse was down last frame, but released in current frame.
| Param | Type | Default | Description |
|---|---|---|---|
| button | MouseButton |
0 |
Button code (defults to MouseButtons.left). |
booleanGet if keyboard key is currently pressed down.
Kind: instance method of Input
Returns: boolean - True if keyboard key is currently down, false otherwise.
| Param | Type | Description |
|---|---|---|
| key | KeyboardKey |
Keyboard key code. |
BooleanGet if keyboard key is currently not down.
Kind: instance method of Input
Returns: Boolean - True if keyboard key is currently up, false otherwise.
| Param | Type | Description |
|---|---|---|
| key | KeyboardKey |
Keyboard key code. |
BooleanGet if a keyboard button was released in current frame.
Kind: instance method of Input
Returns: Boolean - True if key was down last frame, but released in current frame.
| Param | Type | Description |
|---|---|---|
| button | KeyboardKey |
Keyboard key code. |
BooleanGet if keyboard key was pressed this frame.
Kind: instance method of Input
Returns: Boolean - True if key is currently down, but was up in previous frame.
| Param | Type | Description |
|---|---|---|
| key | KeyboardKey |
Keyboard key code. |
BooleanReturn if a mouse or keyboard button is currently down.
Kind: instance method of Input
Returns: Boolean - True if key or mouse button are down.
| Param | Type | Description |
|---|---|---|
| code | string | Array.<String> |
Keyboard, touch or mouse code. Can be array of codes to test any of them. For mouse buttons: set code to ‘mouse_left’, ‘mouse_right’ or ‘mouse_middle’. For keyboard buttons: use one of the keys of KeyboardKeys (for example ‘a’, ‘alt’, ‘up_arrow’, etc..). For touch screen: set code to ‘touch’. For numbers (0-9): you can use the number itself. Note: if you inject any custom state via setCustomState(), you can use its code here too. |
Example
if (Shaku.input.down(['mouse_left', 'touch', 'space'])) { alert('mouse, touch screen or space are pressed!'); }
BooleanReturn if a mouse or keyboard button was released in this frame.
Kind: instance method of Input
Returns: Boolean - True if key or mouse button were down in previous frame, and released this frame.
| Param | Type | Description |
|---|---|---|
| code | string | Array.<String> |
Keyboard, touch, gamepad or mouse button code. Can be array of codes to test any of them. For mouse buttons: set code to ‘mouse_left’, ‘mouse_right’ or ‘mouse_middle’. For keyboard buttons: use one of the keys of KeyboardKeys (for example ‘a’, ‘alt’, ‘up_arrow’, etc..). For touch screen: set code to ‘touch’. For numbers (0-9): you can use the number itself. Note: if you inject any custom state via setCustomState(), you can use its code here too. |
Example
if (Shaku.input.released(['mouse_left', 'touch', 'space'])) { alert('mouse, touch screen or space were released!'); }
BooleanReturn if a mouse or keyboard button was pressed in this frame.
Kind: instance method of Input
Returns: Boolean - True if key or mouse button where up in previous frame, and pressed this frame.
| Param | Type | Description |
|---|---|---|
| code | string | Array.<String> |
Keyboard, touch, gamepad or mouse button code. Can be array of codes to test any of them. For mouse buttons: set code to ‘mouse_left’, ‘mouse_right’ or ‘mouse_middle’. For keyboard buttons: use one of the keys of KeyboardKeys (for example ‘a’, ‘alt’, ‘up_arrow’, etc..). For touch screen: set code to ‘touch’. For numbers (0-9): you can use the number itself. Note: if you inject any custom state via setCustomState(), you can use its code here too. |
Example
if (Shaku.input.pressed(['mouse_left', 'touch', 'space'])) { alert('mouse, touch screen or space were pressed!'); }
NumberReturn timestamp, in milliseconds, of the last time this key code was released.
Kind: instance method of Input
Returns: Number - Timestamp of last key release, or 0 if was never released.
| Param | Type | Description |
|---|---|---|
| code | string |
Keyboard, touch, gamepad or mouse button code. For mouse buttons: set code to ‘mouse_left’, ‘mouse_right’ or ‘mouse_middle’. For keyboard buttons: use one of the keys of KeyboardKeys (for example ‘a’, ‘alt’, ‘up_arrow’, etc..). For touch screen: set code to ‘touch’. For numbers (0-9): you can use the number itself. Note: if you inject any custom state via setCustomState(), you can use its code here too. |
Example
let lastReleaseTime = Shaku.input.lastReleaseTime('mouse_left');
NumberReturn timestamp, in milliseconds, of the last time this key code was pressed.
Kind: instance method of Input
Returns: Number - Timestamp of last key press, or 0 if was never pressed.
| Param | Type | Description |
|---|---|---|
| code | string |
Keyboard, touch, gamepad or mouse button code. For mouse buttons: set code to ‘mouse_left’, ‘mouse_right’ or ‘mouse_middle’. For keyboard buttons: use one of the keys of KeyboardKeys (for example ‘a’, ‘alt’, ‘up_arrow’, etc..). For touch screen: set code to ‘touch’. For numbers (0-9): you can use the number itself. Note: if you inject any custom state via setCustomState(), you can use its code here too. |
Example
let lastPressTime = Shaku.input.lastPressTime('mouse_left');
BooleanReturn if a key was double-pressed.
Kind: instance method of Input
Returns: Boolean - True if one or more key codes double-pressed, false otherwise.
| Param | Type | Description |
|---|---|---|
| code | string | Array.<string> |
Keyboard, touch, gamepad or mouse button code. Can be array of codes to test any of them. For mouse buttons: set code to ‘mouse_left’, ‘mouse_right’ or ‘mouse_middle’. For keyboard buttons: use one of the keys of KeyboardKeys (for example ‘a’, ‘alt’, ‘up_arrow’, etc..). For touch screen: set code to ‘touch’. For numbers (0-9): you can use the number itself. Note: if you inject any custom state via setCustomState(), you can use its code here too. |
| maxInterval | Number |
Max interval time, in milliseconds, to consider it a double-press. Defaults to defaultDoublePressInterval. |
Example
let doublePressed = Shaku.input.doublePressed(['mouse_left', 'touch', 'space']);
BooleanReturn if a key was double-released.
Kind: instance method of Input
Returns: Boolean - True if one or more key codes double-released, false otherwise.
| Param | Type | Description |
|---|---|---|
| code | string | Array.<string> |
Keyboard, touch, gamepad or mouse button code. Can be array of codes to test any of them. For mouse buttons: set code to ‘mouse_left’, ‘mouse_right’ or ‘mouse_middle’. For keyboard buttons: use one of the keys of KeyboardKeys (for example ‘a’, ‘alt’, ‘up_arrow’, etc..). For touch screen: set code to ‘touch’. For numbers (0-9): you can use the number itself. Note: if you inject any custom state via setCustomState(), you can use its code here too. |
| maxInterval | Number |
Max interval time, in milliseconds, to consider it a double-release. Defaults to defaultDoublePressInterval. |
Example
let doubleReleased = Shaku.input.doubleReleased(['mouse_left', 'touch', 'space']);