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 event is triggered, input events can be handled by registering global input event listeners.

Class Object: InputManager Class.

Usage:

local InputManager = require("InputManager")
local inputManager = InputManager.CreateManager({
context1 = {
action1 = Trigger.KeyDown("W"),
},
})
-- activate context1
inputManager:pushContext("context1")
-- add prefix "Input." to the listened action name
node:gslot("Input.action1", function()
print("action1 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.

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)