LittleJS 1.28.0

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

Functions

Members

Constants

Functions

gamepadConnected(gamepad) → booleanengineInput.js:348

Returns true if passed in gamepad is connected

gamepad number optional
gamepadDpad(gamepad) → Vector2engineInput.js:338

Returns gamepad dpad value

gamepad number optional
gamepadIsDown(button, gamepad) → booleanengineInput.js:291

Returns true if gamepad button is down

button number
gamepad number optional
gamepadStick(stick, gamepad) → Vector2engineInput.js:327

Returns gamepad stick value

stick number
gamepad number optional
Returns
  • Where the stick points, y up, length 0 to 1 past the dead zone, zero when there is no such stick
gamepadStickCount(gamepad) → numberengineInput.js:358

Returns how many control sticks the passed in gamepad has

gamepad number optional
gamepadVibrate(gamepad, duration, strongMagnitude, weakMagnitude, startDelay)engineInput.js:372

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

gamepad number optional

gamepad index

duration number optional, default 200

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

strongMagnitude number optional, default 1

strong (left) motor intensity, 0 to 1

weakMagnitude number optional, default 1

weak (right) motor intensity, 0 to 1

startDelay number optional, default 0

delay in ms before the effect starts

gamepadVibrateStop(gamepad)engineInput.js:389

Stop vibration on a gamepad

gamepad number optional

gamepad index

gamepadWasPressed(button, gamepad) → booleanengineInput.js:303

Returns true if gamepad button was pressed

button number
gamepad number optional
gamepadWasReleased(button, gamepad) → booleanengineInput.js:315

Returns true if gamepad button was released

button number
gamepad number optional
inputClear()engineInput.js:171

Clears all input

inputClearKey(key, device, clearDown, clearPressed, clearReleased)engineInput.js:125

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

key string|number

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

device number optional, default 0

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

clearDown boolean optional, default true

Clear the held state

clearPressed boolean optional, default true

Clear the pressed this frame state

clearReleased boolean optional, default true

Clear the released this frame state

keyDirection(up, down, left, right) → Vector2engineInput.js:246

Returns input vector from arrow keys or WASD if enabled

up string optional, default ArrowUp
down string optional, default ArrowDown
left string optional, default ArrowLeft
right string optional, default ArrowRight
keyIsDown(key, device) → booleanengineInput.js:199

Returns true if device key is down

key string|number
device number optional, default 0

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

keyWasPressed(key, device) → booleanengineInput.js:214

Returns true if device key was pressed this frame

key string|number
device number optional, default 0

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

keyWasReleased(key, device) → booleanengineInput.js:229

Returns true if device key was released this frame

key string|number
device number optional, default 0

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

mouseIsDown(button) → booleanengineInput.js:260

Returns true if mouse button is down

button number
mouseWasPressed(button) → booleanengineInput.js:270

Returns true if mouse button was pressed

button number
mouseWasReleased(button) → booleanengineInput.js:280

Returns true if mouse button was released

button number
pointerLockExit()engineInput.js:435

Request to unlock the pointer

pointerLockIsActive() → booleanengineInput.js:444

Check if pointer is locked (true if locked)

pointerLockRequest()engineInput.js:425

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

setInputMouseMoveThreshold(threshold)engineInput.js:102

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

threshold number
setInputPreventDefault(preventDefault)engineInput.js:97

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
preventDefault boolean optional, default true
usingGamepadInput() → booleanengineInput.js:115

Is a gamepad the most recently used input device?

usingKeyboardInput() → booleanengineInput.js:111

Is the keyboard the most recently used input device?

usingMouseInput() → booleanengineInput.js:107

Is the mouse the most recently used input device?

vibrate(pattern)engineInput.js:410

Pulse the vibration hardware if it exists

pattern number|Array optional, default 100

single value in ms or vibration interval array

vibrateStop()engineInput.js:418

Cancel any ongoing vibration

Members

gamepadPrimary : numberengineInput.js:85

Primary gamepad index, automatically set to first gamepad with input

inputMouseMoveThreshold : number default 6engineInput.js:75

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

inputPreventDefault : booleanengineInput.js:80

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

isUsingGamepad : booleanengineInput.js:57

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

lastInputDevice : 'mouse'|'keyboard'|'gamepad'engineInput.js:68

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

Mouse movement delta in world space

mouseDeltaScreen : Vector2engineInput.js:34

Mouse movement delta in screen space

mouseInWindow : booleanengineInput.js:51

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

Mouse pos in world space

mousePosScreen : Vector2engineInput.js:24

Mouse pos in screen space

mouseWheel : numberengineInput.js:39

Mouse wheel delta this frame

touchPinch : numberengineInput.js:46

Touch pinch delta this frame, in the mouse wheel's units: two fingers moving apart are negative, as the wheel turned up is, and moving together positive, 1 for every 50 pixels; added to mouseWheel while touchPinchWheel is on, so a game that zooms with the wheel zooms with a pinch

Constants

isTouchDeviceengineInput.js:89

True if a touch device has been detected