Skip to main content

InputManager

Description:

  InputManager is a class for managing input contexts and actions. Input events can be listened for and handled by creating input contexts and actions, and then adding them to the input manager. Specific combinations of input contexts can be activated and deactivated by calling the pushContext and popContext methods. When an action changes state, input events can be handled with on(), once(), or onCompleted().

Class Object: InputManager Class.

Usage:

local InputManager = require("InputManager")
local Trigger = InputManager.Trigger
local inputManager = InputManager.CreateManager({
context1 = {
action1 = Trigger.Down({
{key = "Space"},
{button = "a"}
}),
},
})
-- activate context1
inputManager:pushContext("context1")
-- listen for the completed action
inputManager:onCompleted("action1", function(event)
print(event.action .. " triggered")
end)
-- remove context1 from the context stack
inputManager:popContext()
inputManager:destroy()

getNode

Type: Function.

Description:

  Gets the current input node. The input node is used to receive input events. It will be added to Director.entry automatically.

Signature:

getNode: function(self: InputManager): Node.Type

Returns:

Return TypeDescription
NodeThe input node.

getEventName

Type: Function.

Description:

  Gets the global event name for an action.

Signature:

getEventName: function(self: InputManager, actionName: string): string

Parameters:

ParameterTypeDescription
actionNamestringThe action name.

Returns:

Return TypeDescription
stringThe global event name.

on

Type: Function.

Description:

  Listens for all state changes of an action.

Signature:

on: function(self: InputManager, actionName: string, handler: InputHandler)

Parameters:

ParameterTypeDescription
actionNamestringThe action name.
handlerInputHandlerThe handler to call.

once

Type: Function.

Description:

  Listens for the next state change of an action, then disables the listener.

Signature:

once: function(self: InputManager, actionName: string, handler: InputHandler)

Parameters:

ParameterTypeDescription
actionNamestringThe action name.
handlerInputHandlerThe handler to call.

off

Type: Function.

Description:

  Disables handlers registered by on(), once(), or onCompleted().

Signature:

off: function(self: InputManager, actionName: string, handler: InputHandler)

Parameters:

ParameterTypeDescription
actionNamestringThe action name.
handlerInputHandlerThe handler to disable.

onCompleted

Type: Function.

Description:

  Listens for the completed state of an action.

Signature:

onCompleted: function(self: InputManager, actionName: string, handler: InputHandler)

Parameters:

ParameterTypeDescription
actionNamestringThe action name.
handlerInputHandlerThe handler to call.

pushContext

Type: Function.

Description:

  Adds an input context to the context stack. Temporarily disables the previous context, then activates the actions in the new context.

Signature:

pushContext: function(self: InputManager, contextNames: string | {string}): boolean

Parameters:

ParameterTypeDescription
contextNamesstring{string}

Returns:

Return TypeDescription
booleanWhether the context is successfully pushed.

popContext

Type: Function.

Description:

  Removes the current input context from the context stack. Activates the previous context.

Signature:

popContext: function(self: InputManager, count?: number): boolean

Parameters:

ParameterTypeDescription
countnumber[optional] The number of contexts to be popped. Default is 1.

Returns:

Return TypeDescription
booleanWhether the context is successfully popped.

emitKeyDown

Type: Function.

Description:

  Emits a key down event for input simulation.

Signature:

emitKeyDown: function(self: InputManager, keyName: KeyName)

Parameters:

ParameterTypeDescription
keyNameKeyNameThe name of the key.

emitKeyUp

Type: Function.

Description:

  Emits a key up event for input simulation.

Signature:

emitKeyUp: function(self: InputManager, keyName: KeyName)

Parameters:

ParameterTypeDescription
keyNameKeyNameThe name of the key.

emitButtonDown

Type: Function.

Description:

  Emits a button down event for input simulation.

Signature:

emitButtonDown: function(self: InputManager, buttonName: ButtonName, controllerId?: number)

Parameters:

ParameterTypeDescription
buttonNameButtonNameThe name of the button.
controllerIdnumber[optional] The ID of the gamepad controller. Default is 0.

emitButtonUp

Type: Function.

Description:

  Emits a button up event for input simulation.

Signature:

emitButtonUp: function(self: InputManager, buttonName: ButtonName, controllerId?: number)

Parameters:

ParameterTypeDescription
buttonNameButtonNameThe name of the button.
controllerIdnumber[optional] The ID of the gamepad controller. Default is 0.

emitAxis

Type: Function.

Description:

  Emits an axis event for input simulation.

Signature:

emitAxis: function(self: InputManager, axisName: AxisName, value: number, controllerId?: number)

Parameters:

ParameterTypeDescription
axisNameAxisNameThe name of the axis.
valuenumberThe value of the axis, ranging from -1 to 1.
controllerIdnumber[optional] The ID of the gamepad controller. Default is 0.

destroy

Type: Function.

Description:

  Destroys the input manager and clears input event listeners.

Signature:

destroy: function(self: InputManager)