Nightfall's KCD2 Keyboard API is a lightweight integration tool for Kingdom Come: Deliverance 2. It bridges the gap between the game's internal scripting engine and the Windows Operating System. By monitoring the game's output log (kcd.log) in real-time, it intercepts specific strings and translates them into simulated hardware-level keyboard and mouse inputs.
Built as a proxy DLL, it seamlessly loads with the game while maintaining original functionality, allowing modders and Lua script developers to trigger physical OS keystrokes and mouse clicks directly from in-game events.
- Download or compile the mod. You should have a file named
KCD2-KeyboardSim.dll. - Rename the compiled
KCD2-KeyboardSim.dllfile toWhGdk.dll. - Navigate to your game's binary folder:
...\KingdomComeDeliverance2\Bin\Win64MasterMasterSteamPGO - Locate the original
WhGdk.dlland rename it toWhGdk_orig.dll. - Drag and drop your newly renamed modded
WhGdk.dllinto that same folder. - Launch the game. A console window will initialize automatically confirming the simulator has loaded.
- Proxy Loading — Safely hooks into the game's boot process by forwarding
GetWhGdkWrapperto the original DLL. The game runs normally without missing critical exports. - Real-Time Log Tailing — Continuously monitors and reads updates from
kcd.log(looking backwards up to 16 characters for new changes) without locking the file. - Hardware-Level Input Simulation — Utilizes Windows
SendInputto generate authentic keyboard and mouse events with built-in timing delays (50ms press, 100ms release) to ensure the game engine registers the inputs accurately. - Console Debugging — Opens an external console window alongside the game to display target paths, initial file content, and debug messaging. (If build in Debug Mode)
Once installed, any mod, Lua script, or game event that prints specific trigger phrases to kcd.log will cause the mod to simulate that key press.
If your custom Lua script executes System.LogAlways("Press MLeft"), the mod will detect the update in kcd.log and simulate a physical Left Mouse Button click.
If you want to make bigger scripts, you can use my lua executor to write larger multiline scripts for the game (https://www.nexusmods.com/kingdomcomedeliverance2/mods/3111).
Here is an example, which will press the left mouse buttom, as soon as an entity is within 1 meter of the player:
proximityLoopRunning = true
function CheckNearby()
if not proximityLoopRunning then return end
local pos = player:GetPos()
local radius = 2
for _, e in pairs(System.GetEntities(pos, radius) or {}) do
if e.id ~= player.id and e.soul then
local ep = e:GetPos()
local d2 = (ep.x - pos.x)^2 + (ep.y - pos.y)^2 + (ep.z - pos.z)^2
if d2 <= radius * radius then
System.LogAlways("Press MLeft")
break
end
end
end
Script.SetTimer(200, CheckNearby)
end
CheckNearby()Supported Command Strings:
Simply print the exact string via System.LogAlways('') to trigger the corresponding key/mouse action:
- Mouse:
Press MLeft,Press MRight,Press MMiddle,Press MX1,Press MX2 - Alphabet:
Press AthroughPress Z - Numbers:
Press 0throughPress 9 - Special Keys:
Press SPACE,Press ENTER,Press SHIFT,Press TAB,Press CTRL,Press ALT,Press ESC - Arrows:
Press UP,Press DOWN,Press LEFT,Press RIGHT
- System-Wide Inputs: Because this mod uses Windows
SendInput, the key presses and mouse clicks are sent to the OS itself, not just the game window. If youALT+TABout of KCD2 while a script is spamming log commands, it will click and type on your desktop or other applications. Use with caution. - File I/O Performance: The mod reads
kcd.logon a 10ms loop. While extremely fast, generating excessively massive log files via other mods may impact the tailing performance. - Compatibility Issues: Ensure no other mods are trying to proxy the exact same
WhGdk.dll. If you are using other proxy mods, you may need to chain your DLLs.
If you encounter bugs, have feature suggestions, or need help integrating this into your own Lua scripts, message me on Discord: NightfallCT