Inputs are buttons events that handle both key press and release states. They must be registered using registerInput() at root level, outside of any event, in the init.lua file.
The button state (press/release) is defined in the first argument passed to the callback.
Definition
registerInput(slug, label, callback)
---- registerInput()---- @param string slug The internal slug (must be unique in your mod scope)-- @param string label The label displayed in CET Bindings-- @param function callback The callback function--registerInput('slug', 'label', function(keypress)if keypress then-- key is pressedelse-- key is releasedendend)
Alternative Usage
You can register an Input and make it behave like a Hotkey. This method is more reactive as it triggers on key press, when a Hotkey is triggered on release.
registerInput('slug', 'label', function(keypress)-- bail early on key releaseifnot keypress thenreturnend-- key is pressedend)
Usage Note
It is important to check the keypress argument inside the callback. Otherwise the code will be executed twice:
One time when the key is pressed
A second time when released
registerInput('slug', 'label', function(keypress)-- this will be called 2 times!end)
Usage Example
Activate slow motion effect as long as the input key is pressed:
init.lua
-- register inputregisterInput('slow_motion', 'Slow Motion', function(keypress)-- get time systemlocal timeSystem = Game.GetTimeSystem()-- bail early if time system doesn't existsifnot timeSystem thenreturnend-- key is pressedif keypress then timeSystem:SetTimeDilation('MySlowMo', 0.3)-- key is releasedelse timeSystem:UnsetTimeDilation('MySlowMo')endend)
Advanced Example
Continuously give money as long as the input key is pressed:
This example use the onUpdate event, which is triggered continuously. Make sure to check the documentation before any modification.
init.lua
-- set initial switch statekeep_giving =false-- register inputregisterInput('give_continuous_money', 'Give Continuous Money', function(keypress)-- input pressedif keypress then keep_giving =true-- switch on-- input releasedelse keep_giving =false-- switch offendend)-- onUpdate-- this event is triggered continuouslyregisterForEvent('onUpdate', function()-- check switch stateif keep_giving then Game.AddToInventory('Items.money', 20)endend)