Input

LittleJS Input System

  • Keyboard input with key down, pressed, and released states
  • Mouse input with position (world and screen space), buttons, and wheel
  • Gamepad support for multiple controllers with analog sticks and buttons
  • Touch input mapped to mouse position and buttons
  • Virtual on-screen gamepad for mobile devices
  • Automatic gamepad vs keyboard/mouse detection
  • Input event prevention for canvas focus
  • Clipboard copy/paste support

Members

(static) gamepadPrimary :number

Primary gamepad index, automatically set to first gamepad with input

Type:
  • number

(static) inputMouseMoveThreshold :number

Screen-pixel mouse movement per frame that counts as "using the mouse" (so sub-pixel hand jitter doesn't steal focus from the keyboard/gamepad).

Type:
  • number
Default Value
  • 6

(static) inputPreventDefault :boolean

Prevents input continuing to the default browser handling (true by default)

Type:
  • boolean

(static, constant) isTouchDevice

True if a touch device has been detected

(static) isUsingGamepad :boolean

True if a gamepad is the most recently used input device. Equivalent to usingGamepadInput(); derived from lastInputDevice each frame.

Type:
  • boolean

(static) lastInputDevice :'mouse'|'keyboard'|'gamepad'

The most recently used input device: 'mouse' | 'keyboard' | 'gamepad'. Sticky: it holds its value while every device is idle, so a mouse-follow control (e.g. paddle = mousePos) won't snap back the instant the stick/keys are released. With several devices in play at once (e.g. keyboard to move + mouse to aim) it tracks whichever was touched last each frame, so it may alternate — that's intended; use it to pick which control drives a shared action. Updated every frame by inputUpdate().

Type:
  • 'mouse' | 'keyboard' | 'gamepad'

(static) mouseDelta :Vector2

Mouse movement delta in world space

Type:
  • Vector2

(static) mouseDeltaScreen :Vector2

Mouse movement delta in screen space

Type:
  • Vector2

(static) mouseInWindow :boolean

True if mouse was inside the document window, set to false when mouse leaves

Type:
  • boolean

(static) mousePos :Vector2

Mouse pos in world space

Type:
  • Vector2

(static) mousePosScreen :Vector2

Mouse pos in screen space

Type:
  • Vector2

(static) mouseWheel :number

Mouse wheel delta this frame

Type:
  • number

Methods

(static) gamepadConnected(gamepadopt) → {boolean}

Returns true if passed in gamepad is connected

Parameters:
NameTypeAttributesDescription
gamepadnumber<optional>
Returns:
Type: 
boolean

(static) gamepadDpad(gamepadopt) → {Vector2}

Returns gamepad dpad value

Parameters:
NameTypeAttributesDescription
gamepadnumber<optional>
Returns:
Type: 
Vector2

(static) gamepadIsDown(button, gamepadopt) → {boolean}

Returns true if gamepad button is down

Parameters:
NameTypeAttributesDescription
buttonnumber
gamepadnumber<optional>
Returns:
Type: 
boolean

(static) gamepadStick(stick, gamepadopt) → {Vector2}

Returns gamepad stick value

Parameters:
NameTypeAttributesDescription
sticknumber
gamepadnumber<optional>
Returns:
  • Where the stick points, y up, length 0 to 1 past the dead zone, zero when there is no such stick
Type: 
Vector2

(static) gamepadStickCount(gamepadopt) → {number}

Returns how many control sticks the passed in gamepad has

Parameters:
NameTypeAttributesDescription
gamepadnumber<optional>
Returns:
Type: 
number

(static) gamepadVibrate(gamepadopt, durationopt, strongMagnitudeopt, weakMagnitudeopt, startDelayopt)

Pulse a gamepad's vibration hardware using the dual-rumble effect if it exists Strong magnitude is usually the left side motor, weak magnitude is usually the right side motor

Parameters:
NameTypeAttributesDefaultDescription
gamepadnumber<optional>

gamepad index

durationnumber<optional>
200

effect duration in ms, browsers limit it and the delay to 5 seconds together

strongMagnitudenumber<optional>
1

strong (left) motor intensity, 0 to 1

weakMagnitudenumber<optional>
1

weak (right) motor intensity, 0 to 1

startDelaynumber<optional>
0

delay in ms before the effect starts

(static) gamepadVibrateStop(gamepadopt)

Stop vibration on a gamepad

Parameters:
NameTypeAttributesDescription
gamepadnumber<optional>

gamepad index

(static) gamepadWasPressed(button, gamepadopt) → {boolean}

Returns true if gamepad button was pressed

Parameters:
NameTypeAttributesDescription
buttonnumber
gamepadnumber<optional>
Returns:
Type: 
boolean

(static) gamepadWasReleased(button, gamepadopt) → {boolean}

Returns true if gamepad button was released

Parameters:
NameTypeAttributesDescription
buttonnumber
gamepadnumber<optional>
Returns:
Type: 
boolean

(static) inputClear()

Clears all input

(static) inputClearKey(key, deviceopt, clearDownopt, clearPressedopt, clearReleasedopt)

Clear a key's state, so it stops reading as down, pressed or released this frame; a key still held is not pressed again until it is let go

Parameters:
NameTypeAttributesDefaultDescription
keystring | number

A key code like 'Space', or a mouse or gamepad button number

devicenumber<optional>
0

0 for the keyboard and mouse, gamepad n is device n + 1

clearDownboolean<optional>
true

Clear the held state

clearPressedboolean<optional>
true

Clear the pressed this frame state

clearReleasedboolean<optional>
true

Clear the released this frame state

(static) keyDirection(upopt, downopt, leftopt, rightopt) → {Vector2}

Returns input vector from arrow keys or WASD if enabled

Parameters:
NameTypeAttributesDefaultDescription
upstring<optional>
ArrowUp
downstring<optional>
ArrowDown
leftstring<optional>
ArrowLeft
rightstring<optional>
ArrowRight
Returns:
Type: 
Vector2

(static) keyIsDown(key, deviceopt) → {boolean}

Returns true if device key is down

Parameters:
NameTypeAttributesDefaultDescription
keystring | number
devicenumber<optional>
0

0 for the keyboard and mouse, gamepad n is device n + 1

Returns:
Type: 
boolean

(static) keyWasPressed(key, deviceopt) → {boolean}

Returns true if device key was pressed this frame

Parameters:
NameTypeAttributesDefaultDescription
keystring | number
devicenumber<optional>
0

0 for the keyboard and mouse, gamepad n is device n + 1

Returns:
Type: 
boolean

(static) keyWasReleased(key, deviceopt) → {boolean}

Returns true if device key was released this frame

Parameters:
NameTypeAttributesDefaultDescription
keystring | number
devicenumber<optional>
0

0 for the keyboard and mouse, gamepad n is device n + 1

Returns:
Type: 
boolean

(static) mouseIsDown(button) → {boolean}

Returns true if mouse button is down

Parameters:
NameTypeDescription
buttonnumber
Returns:
Type: 
boolean

(static) mouseWasPressed(button) → {boolean}

Returns true if mouse button was pressed

Parameters:
NameTypeDescription
buttonnumber
Returns:
Type: 
boolean

(static) mouseWasReleased(button) → {boolean}

Returns true if mouse button was released

Parameters:
NameTypeDescription
buttonnumber
Returns:
Type: 
boolean

(static) pointerLockExit()

Request to unlock the pointer

(static) pointerLockIsActive() → {boolean}

Check if pointer is locked (true if locked)

Returns:
Type: 
boolean

(static) pointerLockRequest()

Request to lock the pointer, for a mouse; a device with only touch refuses it

(static) setInputMouseMoveThreshold(threshold)

Set the screen-pixel mouse movement per frame that counts as using the mouse

Parameters:
NameTypeDescription
thresholdnumber

(static) setInputPreventDefault(preventDefaultopt)

Prevents input continuing to the default browser handling This is useful to disable for html menus so the browser can handle input normally, the right click menu included; over an html text field that menu always shows

  • While on, the mouse's back and forward buttons don't leave the page, a game can read them as mouse 3 and 4
Parameters:
NameTypeAttributesDefaultDescription
preventDefaultboolean<optional>
true

(static) usingGamepadInput() → {boolean}

Is a gamepad the most recently used input device?

Returns:
Type: 
boolean

(static) usingKeyboardInput() → {boolean}

Is the keyboard the most recently used input device?

Returns:
Type: 
boolean

(static) usingMouseInput() → {boolean}

Is the mouse the most recently used input device?

Returns:
Type: 
boolean

(static) vibrate(patternopt)

Pulse the vibration hardware if it exists

Parameters:
NameTypeAttributesDefaultDescription
patternnumber | Array<optional>
100

single value in ms or vibration interval array

(static) vibrateStop()

Cancel any ongoing vibration