Tested with: KSA Version 2026.10.19.5638
This section will cover how to get keyboard input when modding KSA.
This guide uses harmony to intercept the Vehicle.OnKey function calls so we will need that setup first.
This guide continues from Getting started with harmony
for this guide you will need:
To do actions when a key is pressed we will be hooking into the Vehicle.OnKey function. So lets set that up
In KSA Key events are handled using a combination of Glfw and some extra wrappers. So we will need some extra dependencies.
Add the following dependencies to your visual studio project:
Now lets write the key input code
Patcher.cs fileusing Brutal.GlfwApi;
using KSA;
Patcher class add the Harmony Patch[HarmonyPatch(typeof(Vehicle), nameof(Vehicle.OnKey))]
[HarmonyPrefix]
public static bool onKey(Vehicle __instance, RenderCore.Input.GlfwKeyEvent keyEvent)
{
Console.WriteLine("A key was pressed!");
return true;
}
The Patcher.OnKey function will automatically be called every time a key event happens (pressed/released/...) in game. If you want to add extra logic besides the print it should go in there.
Now that we can detect any key event, We can start filtering for the specific event we want.
For this tutorial we will try to print something every time the space bar is pressed.
Inside OnKey:
They GlfwKeyEvent contains 2 parts so we will first split those into GlfwKey and GlfwKeyAction.
We want to get the space bar so we will use an if statement to check for GlfwKey.Space
We only want to do something when it is pressed (Key goes down) not when released or repeated. So we will check for GlfwKeyAction.Press
Replace the contents of onKey with the following code.
GlfwKey key = keyEvent.Key;
GlfwKeyAction action = keyEvent.Action;
if (key == GlfwKey.Space)
{
if (action == GlfwKeyAction.Press)
{
Console.WriteLine("Space Pressed");
}
}
return true;
We return true at the end to tell Harmony that the original method should still run.
In a Harmony prefix, returning:
true → "continue and execute the original Vehicle.OnKey"
false → "skip the original method completely"
Since this patch only wants to log the space key and not block or replace the game’s input handling, it finishes by returning true, allowing the game’s normal key-handling behavior to proceed.
That is it you now have a functioning onKey function. If you want to add custom behavior just make sure you do it inside the onKey function and all will go well
Players can rebind the game's controls, including to mouse buttons. If your mod reacts to one of the game's own actions, ask the game whether the event matches that action instead of hard-coding a key:
if (Input.MatchPressed(in keyEvent, InputAction.ToggleLights))
{
Console.WriteLine("The player pressed their lights key");
}
Input.Matches checks any action, MatchPressed only the press and MatchReleased only the release. The InputAction enum lists every bindable action. Mods cannot add their own actions to it, so for a mod's own controls, checking GlfwKey as above is still the way.
Before 2026.10,
Input.Matchestook aGlfwKeyandGlfwModifierseparately. That overload is gone. See Code Changes Since 2026.8.