Shaku

Shaku JS

Back To Table of Content

Input

Input

Input manager. Used to recieve input from keyboard and mouse.

To access the Input manager use Shaku.input.

Kind: global class

new Input()

Create the manager.

input.preventDefaults : Boolean

If true, will prevent default input events by calling preventDefault().

Kind: instance property of Input

input.disableMouseWheelAutomaticScrolling : Boolean

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

input.disableContextMenu : Boolean

If true (default), will disable the context menu (what typically opens when you right click the page).

Kind: instance property of Input

input.delegateTouchInputToMouse : Boolean

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

input.delegateGamepadInputToKeys : Boolean

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):

Kind: instance property of Input

input.resetOnFocusLoss : Boolean

If true (default), will reset all states if the window loses focus.

Kind: instance property of Input

input.defaultDoublePressInterval : Number

Default time, in milliseconds, to consider two consecutive key presses as a double-press.

Kind: instance property of Input

input.MouseButtons

Get the Mouse Buttons enum.

Kind: instance property of Input
See: MouseButtons

input.KeyboardKeys

Get the Keyboard Buttons enum.

Kind: instance property of Input
See: KeyboardKeys

input.TouchKeyCode ⇒ String

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.

input.touchPosition ⇒ Vector2

Get touch screen touching position. Note: if not currently touching, will return last known position.

Kind: instance property of Input
Returns: Vector2 - Touch position.

input.touching ⇒ Boolean

Get if currently touching a touch screen.

Kind: instance property of Input
Returns: Boolean - True if currently touching the screen.

input.touchStarted ⇒ Boolean

Get if started touching a touch screen in current frame.

Kind: instance property of Input
Returns: Boolean - True if started touching the screen now.

input.touchEnded ⇒ Boolean

Get if stopped touching a touch screen in current frame.

Kind: instance property of Input
Returns: Boolean - True if stopped touching the screen now.

input.mousePosition ⇒ Vector2

Get mouse position.

Kind: instance property of Input
Returns: Vector2 - Mouse position.

input.prevMousePosition ⇒ Vector2

Get mouse previous position (before the last endFrame() call).

Kind: instance property of Input
Returns: Vector2 - Mouse position in previous frame.

input.mouseDelta ⇒ Vector2

Get mouse movement since last endFrame() call.

Kind: instance property of Input
Returns: Vector2 - Mouse change since last frame.

input.mouseMoving ⇒ Boolean

Get if mouse is currently moving.

Kind: instance property of Input
Returns: Boolean - True if mouse moved since last frame, false otherwise.

input.shiftDown ⇒ Boolean

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.

input.ctrlDown ⇒ Boolean

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.

input.altDown ⇒ Boolean

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.

input.anyKeyPressed ⇒ Boolean

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.

input.anyKeyDown ⇒ Boolean

Get if any keyboard key is currently down.

Kind: instance property of Input
Returns: Boolean - True if there’s a key pressed down.

input.anyMouseButtonPressed ⇒ Boolean

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.

input.anyMouseButtonDown ⇒ Boolean

Get if any mouse button is down.

Kind: instance property of Input
Returns: Boolean - True if any of the mouse buttons are pressed.

input.mouseWheelSign ⇒ Number

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.

input.mouseWheel ⇒ Number

Get mouse wheel value.

Kind: instance property of Input
Returns: Number - Mouse wheel value.

input.setTargetElement(element)

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);

input.gamepad([index]) ⇒ Gamepad

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.

input.gamepadId([index]) ⇒

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.

input.gamepadIds() ⇒ Array.<String>

Return a list with connected devices ids.

Kind: instance method of Input
Returns: Array.<String> - List of connected devices ids.

input.setCustomState(code, value)

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.

input.mousePressed(button) ⇒ Boolean

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).

input.mouseDown(button) ⇒ Boolean

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).

input.mouseUp(button) ⇒ Boolean

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).

input.mouseReleased(button) ⇒ Boolean

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).

input.keyDown(key) ⇒ boolean

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.

input.keyUp(key) ⇒ Boolean

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.

input.keyReleased(button) ⇒ Boolean

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.

input.keyPressed(key) ⇒ Boolean

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.

input.down(code) ⇒ Boolean

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!'); }

input.released(code) ⇒ Boolean

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!'); }

input.pressed(code) ⇒ Boolean

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!'); }

input.lastReleaseTime(code) ⇒ Number

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');

input.lastPressTime(code) ⇒ Number

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');

input.doublePressed(code, maxInterval) ⇒ Boolean

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']);

input.doubleReleased(code, maxInterval) ⇒ Boolean

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']);