Input manager. Used to recieve input from keyboard and mouse.
To access the Input manager use Shaku.input
.
Kind: global class
- Input
- new Input()
- .preventDefaults :
Boolean
- .disableMouseWheelAutomaticScrolling :
Boolean
- .disableContextMenu :
Boolean
- .delegateTouchInputToMouse :
Boolean
- .delegateGamepadInputToKeys :
Boolean
- .resetOnFocusLoss :
Boolean
- .defaultDoublePressInterval :
Number
- .MouseButtons
- .KeyboardKeys
- .TouchKeyCode ⇒
String
- .touchPosition ⇒
Vector2
- .touching ⇒
Boolean
- .touchStarted ⇒
Boolean
- .touchEnded ⇒
Boolean
- .mousePosition ⇒
Vector2
- .prevMousePosition ⇒
Vector2
- .mouseDelta ⇒
Vector2
- .mouseMoving ⇒
Boolean
- .shiftDown ⇒
Boolean
- .ctrlDown ⇒
Boolean
- .altDown ⇒
Boolean
- .anyKeyPressed ⇒
Boolean
- .anyKeyDown ⇒
Boolean
- .anyMouseButtonPressed ⇒
Boolean
- .anyMouseButtonDown ⇒
Boolean
- .mouseWheelSign ⇒
Number
- .mouseWheel ⇒
Number
- .setTargetElement(element)
- .gamepad([index]) ⇒
Gamepad
- .gamepadId([index]) ⇒
- .gamepadIds() ⇒
Array.<String>
- .setCustomState(code, value)
- .mousePressed(button) ⇒
Boolean
- .mouseDown(button) ⇒
Boolean
- .mouseUp(button) ⇒
Boolean
- .mouseReleased(button) ⇒
Boolean
- .keyDown(key) ⇒
boolean
- .keyUp(key) ⇒
Boolean
- .keyReleased(button) ⇒
Boolean
- .keyPressed(key) ⇒
Boolean
- .down(code) ⇒
Boolean
- .released(code) ⇒
Boolean
- .pressed(code) ⇒
Boolean
- .lastReleaseTime(code) ⇒
Number
- .lastPressTime(code) ⇒
Number
- .doublePressed(code, maxInterval) ⇒
Boolean
- .doubleReleased(code, maxInterval) ⇒
Boolean
Create the manager.
If true, will prevent default input events by calling preventDefault().
Kind: instance property of Input
By 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
If true (default), will disable the context menu (what typically opens when you right click the page).
Kind: instance property of Input
If 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
If 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):
- gamepadX_top: state of arrow keys top key (left buttons).
- gamepadX_bottom: state of arrow keys bottom key (left buttons).
- gamepadX_left: state of arrow keys left key (left buttons).
- gamepadX_right: state of arrow keys right key (left buttons).
- gamepadX_leftStickUp: true if left stick points directly up.
- gamepadX_leftStickDown: true if left stick points directly down.
- gamepadX_leftStickLeft: true if left stick points directly left.
- gamepadX_leftStickRight: true if left stick points directly right.
- gamepadX_rightStickUp: true if right stick points directly up.
- gamepadX_rightStickDown: true if right stick points directly down.
- gamepadX_rightStickLeft: true if right stick points directly left.
- gamepadX_rightStickRight: true if right stick points directly right.
- gamepadX_a: state of A key (from right buttons).
- gamepadX_b: state of B key (from right buttons).
- gamepadX_x: state of X key (from right buttons).
- gamepadX_y: state of Y key (from right buttons).
- gamepadX_frontTopLeft: state of the front top-left button.
- gamepadX_frontTopRight: state of the front top-right button.
- gamepadX_frontBottomLeft: state of the front bottom-left button.
- gamepadX_frontBottomRight: state of the front bottom-right button.
Where X in
gamepad
is the gamepad index: gamepad0, gamepad1, gamepad2..
Kind: instance property of Input
If true (default), will reset all states if the window loses focus.
Kind: instance property of Input
Default 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
Return 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.
Get touch screen touching position. Note: if not currently touching, will return last known position.
Kind: instance property of Input
Returns: Vector2
- Touch position.
Get if currently touching a touch screen.
Kind: instance property of Input
Returns: Boolean
- True if currently touching the screen.
Get if started touching a touch screen in current frame.
Kind: instance property of Input
Returns: Boolean
- True if started touching the screen now.
Get if stopped touching a touch screen in current frame.
Kind: instance property of Input
Returns: Boolean
- True if stopped touching the screen now.
Get mouse position.
Kind: instance property of Input
Returns: Vector2
- Mouse position.
Get mouse previous position (before the last endFrame() call).
Kind: instance property of Input
Returns: Vector2
- Mouse position in previous frame.
Get mouse movement since last endFrame() call.
Kind: instance property of Input
Returns: Vector2
- Mouse change since last frame.
Get if mouse is currently moving.
Kind: instance property of Input
Returns: Boolean
- True if mouse moved since last frame, false otherwise.
Get 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.
Get 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.
Get 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.
Get if any keyboard key was pressed this frame.
Kind: instance property of Input
Returns: Boolean
- True if any key was pressed down this frame.
Get if any keyboard key is currently down.
Kind: instance property of Input
Returns: Boolean
- True if there's a key pressed down.
Get 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.
Get if any mouse button is down.
Kind: instance property of Input
Returns: Boolean
- True if any of the mouse buttons are pressed.
Get 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.
Get 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);
Get 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. |
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. |
Get 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). |
Get 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). |
Get 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). |
Get 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). |
Get 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. |
Get 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. |
Get 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. |
Get 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. |
Return 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!'); }
Return 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!'); }
Return 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!'); }
Return 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');
Return 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');
Return 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']);
Return 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']);