Using action scripts you can bind a complex sequence of actions to a trigger.
Most actions that you can configure in Key2Joy are available through scripting. To get started check out the examples, or browse the full API Reference.
When writing action scripts you can use Lua 5.2.3.
If you're an advanced user and curious about the Lua implementation: this project uses NLua
Goal: When the users presses "F" on the keyboard, simulate pressing → ↓ ← B on the GamePad.
-
Save this script to your desktop as
test.lua:SetDelayedFunctions( 250, -- The following functions run with 250ms delay between them function () -- You can press and hold... GamePad.Simulate(GamePadControl.DPadRight, PressState.Press) end, function () -- ... to later release a button. GamePad.Simulate(GamePadControl.DPadRight, PressState.Release) end, function () GamePad.Simulate(GamePadControl.DPadDown, PressState.Press) end, function () GamePad.Simulate(GamePadControl.DPadDown, PressState.Release) end, function () GamePad.Simulate(GamePadControl.DPadLeft, PressState.Press) end, function () GamePad.Simulate(GamePadControl.DPadLeft, PressState.Release) end, function () GamePad.Simulate(GamePadControl.B, PressState.Press) end, function () GamePad.Simulate(GamePadControl.B, PressState.Release) end )
-
In Key2Joy click Create New Mapping (Button marked A in the screenshot)
-
Choose the trigger Keyboard Event (Section B in the screenshot)
-
Click the marked area and press the "F"-key on your keyboard
-
Select Release from the dropdown. This ensures the script will only run once when the F-key is released.
-
For the action we'll choose: Lua Script Action (Section C in the screenshot)
-
Uncheck Direct Input so we can select the
test.luascript we created earlier. -
Click Browse, navigate to the
test.luafile and select it. -
Save the mapping.
Now when you enable the mappings (Check the Arm Mappings checkbox in the top right of
Key2Joy) you can run that Lua script by pressing and releasing the F-key
on your keyboard.
You can find more examples in the 📃 Scripting API Reference.
Lua:
Print("test")
GamePad.Simulate(GamePadControl.A, PressState.Press)
SetTimeout(function ()
GamePad.Simulate(GamePadControl.A, PressState.Release)
App.Command("abort")
end, 500)
Print("end test")All input triggers have their 'InputBag' stored in a global INPUT variable. You can use this to get information about the trigger event.
For example if we Print(INPUT) for a 'GamePad Trigger Pull Event', we'll see in the logs that we get an instance of this object: Core/Key2Joy.Core/Mapping/Triggers/GamePad/GamePadTriggerInputBag.cs. With that information we can find out that to get the amount of the trigger pull we can use either INPUT.LeftTriggerDelta or INPUT.RightTriggerDelta:
You can see the output of scripts in several places. In Key2Joy go to View > View Script Output and choose where you want to see the logs. In Window Event Viewer it looks like this:


