Skip to main content

Input

API for handling user input in a way that's compatible with all supported input devices (keyboards, mice, gamepads, and more!).

Inputs are defined as a set of Actions, collectively called an Action Set. Each Action is bound to one or more physical buttons and/or analogue sensors, and carries a semantic meaning that should be adhered to. This ensures players are free to configure their inputs how they want, on a range of devices, while still having a consistent experience across games and addons.

Action Sets can be selected on the fly to change how input behaves for players. For example, switching to menu navigation when they open chat.

Functions

Input.switchToBasicActionSet

Switches the active action set to "Basic", which should support most 2D and 3D player characters.

If the player is using a mouse, their cursor is locked to the screen when switching to this action set.

Input.switchToBasicActionSet()

Input.switchToMenuActionSet

Switches the active action set to "Menu", enabling the navigation of UIs. Pass the optional MenuType enum to control which input will trigger the dismissMenu action.

If the player is using a mouse, their cursor is unlocked from the screen when switching to this action set.

Input.switchToMenuActionSet(menu?: MenuType)

Parameters

NameTypeDescription
menuMenuType?The (optional) menu type being switched to.

Input.addEventListener

Adds an event listener. A list of events can be found here.

Input.addEventListener(eventType: string, callback: function): function

Parameters

NameTypeDescription
eventTypestringThe event to listen for.
callbackfunctionThe callback to use.

Returns

NameTypeDescription
callbackfunctionThe callback that was registered.

Input.removeEventListener

Removes an event listener.

Input.removeEventListener(eventType: string, callback: function)

Parameters

NameTypeDescription
eventTypestringThe event to remove the listener from.
callbackfunctionThe callback to remove.

Types

DigitalAction

A digital action, i.e. a button press (but not necessarily bound to a physical button).

Note that none of these properties are mutually exclusive. It is valid for a button to be pressed and released during, and held at the end of, a given input sample.

type DigitalAction = { .wasPressedThisFrame: boolean, .wasReleasedThisFrame: boolean, .isHeld: boolean }

Properties

FieldTypeDescription
wasPressedThisFramebooleanTrue if the button transitioned from not pressed to pressed during this input sample
wasReleasedThisFramebooleanTrue if the button transitioned from pressed to not pressed during this input sample
isHeldbooleanTrue if the button was held at the end of this input sample

A collection of actions available when the "Menu" action set is active.

type MenuActionSet = { .cursor: number[], .scroll: number[], .up: DigitalAction, .down: DigitalAction, .left: DigitalAction, .right: DigitalAction, .previousTab: DigitalAction, .nextTab: DigitalAction, .activateCursor: DigitalAction, .affirm: DigitalAction, .cancel: DigitalAction, .openContextMenu: DigitalAction, .hotkey1: DigitalAction, .hotkey2: DigitalAction, .hotkey3: DigitalAction, .hotkey4: DigitalAction, .dismissMenu: DigitalAction }

Properties

FieldTypeDescription
cursornumber[]The delta (in pixels) of the physical or virtual mouse cursor since the last time input was processed.
scrollnumber[]The delta of the scroll action since the last time input was processed.
upDigitalActionSelects a focusable UI element immediately above the currently focused element
downDigitalActionSelects a focusable UI element immediately below the currently focused element
leftDigitalActionSelects a focusable UI element immediately left of the currently focused element
rightDigitalActionSelects a focusable UI element immediately right of the currently focused element
previousTabDigitalActionSelects the previous tab or page in the currently active window or modal
nextTabDigitalActionSelects the next tab or page in the currently active window or modal
activateCursorDigitalActionClicks/presses an interactive UI element underneath the current cursor position
affirmDigitalActionAffirms confirmation modals or clicks/presses the currently focused element
cancelDigitalActionCancels/closes modals or returns to the previous screen
openContextMenuDigitalActionOpens a context menu or displays more information for the currently hovered/focused UI element
hotkey1DigitalActionGeneric action which can be used by developers for interactions not covered by the above actions
hotkey2DigitalActionGeneric action which can be used by developers for interactions not covered by the above actions
hotkey3DigitalActionGeneric action which can be used by developers for interactions not covered by the above actions
hotkey4DigitalActionGeneric action which can be used by developers for interactions not covered by the above actions
dismissMenuDigitalActionDismisses the currently open menu. Bound dynamically depending on the active menu type

BasicActionSet

A collection of actions available when the "Basic" action set is active.

Useful for generic 2D/3D player character controllers and simple/arcade land, sea and air vehicles.

type BasicActionSet = { .look: number[], .move: number[], .jump: DigitalAction, .crouch: DigitalAction, .sprint: DigitalAction, .primary: DigitalAction, .secondary: DigitalAction, .previousItem: DigitalAction, .nextItem: DigitalAction, .reload: DigitalAction, .interact: DigitalAction, .openInventory: DigitalAction, .openMainMenu: DigitalAction, .openSocialMenu: DigitalAction, .hotkey1: DigitalAction, .hotkey2: DigitalAction, .hotkey3: DigitalAction, .hotkey4: DigitalAction }

Properties

FieldTypeDescription
looknumber[]Adjusts the player's viewport on two axes
movenumber[]Moves the player on two axes. Length is clamped to one, but can be shorter
jumpDigitalActionCauses the player to perform an action they would expect to be bound to their jump button
crouchDigitalActionCauses the player to perform an action they would expect to be bound to their crouch button
sprintDigitalActionCauses the player to perform an action they would expect to be bound to their sprint button
primaryDigitalActionCauses the player to perform an action they would expect to be bound to the button usually used for shooting/attacking in FPS/TPS games
secondaryDigitalActionCauses the player to perform an action they would expect to be bound to the button usually used for aiming in FPS/TPS games
previousItemDigitalActionSelects the previous item/weapon held by the player
nextItemDigitalActionSelects the next item/weapon held by the player
reloadDigitalActionCauses the player to reload/recharge their item/weapon.
interactDigitalActionCauses the player to interact with the object they're looking at (e.g. entering a vehicle)
openInventoryDigitalActionOpens the player's inventory (if relevant to the type of game or addon you're building)
openMainMenuDigitalActionShould always open the main in-game menu (usually a pause menu in singleplayer games)
openSocialMenuDigitalActionOpens a menu used for social features in multiplayer (e.g. chat)
hotkey1DigitalActionGeneric action which can be used by developers for interactions not covered by the above actions
hotkey2DigitalActionGeneric action which can be used by developers for interactions not covered by the above actions
hotkey3DigitalActionGeneric action which can be used by developers for interactions not covered by the above actions
hotkey4DigitalActionGeneric action which can be used by developers for interactions not covered by the above actions

Enums

Menu types which are opened by an action, and should be dismissed by the same action.

Value
Main
Inventory
Social

Events

Fired every time input is processed while the "Menu" action set is active.

  • In the menu, this is fired every frame
  • In games on the client, this is fired once per server tick for the local player
  • This action set does not exist on the server
Input.addEventListener("menuActionSet", fn(actions, deltaTime) end)

Parameters

NameTypeDescription
actionsMenuActionSetA snapshot of the menu actions this frame/tick.
deltaTimenumberSeconds passed since the last time input was processed (including for other action sets)

"basicActionSetPredicted"

Fired in the menu and games on the client every time input is processed while the "Basic" action set is active.

  • In the menu, this is fired every frame
  • In games, this is fired once per server tick for each player and when performing local prediction

On the server, the "basicActionSet" event is fired instead.

Input.addEventListener("basicActionSetPredicted", fn(player, actions, isFirstTimePredicted, deltaTime) end)

Parameters

NameTypeDescription
playerPlayerThe player these inputs are for.
actionsBasicActionSetA snapshot of the basic actions this frame/tick.
isFirstTimePredictedbooleanTrue if this is the first time we're processing this input
deltaTimenumberSeconds passed since the last time input was processed (including for other action sets)

"basicActionSet"

Fired on the server every time input is processed while the "Basic" action set is active.

On the client, the "basicActionSetPredicted" event is fired instead.

Input.addEventListener("basicActionSet", fn(player, actions, deltaTime) end)

Parameters

NameTypeDescription
playerPlayerThe player these inputs are for.
actionsBasicActionSetA snapshot of the basic actions this frame/tick.
deltaTimenumberSeconds passed since the last time input was processed (including for other action sets)