Windows keyboard & mouse simulation via SendInput Win32 API.
Sends both virtual key codes and hardware scan codes, so games that ignore VK-only input (Minecraft, many DirectInput games) respond correctly.
var kb = new KeyboardSim();
kb.Press(Keys.Space); // press & release
kb.Press(Keys.W, holdMs: 500); // hold W for 500ms
kb.PressAsync(Keys.Enter); // fire-and-forget via thread pool
kb.TypeUnicode("Hello, 世界!"); // any Unicode charactervar mouse = new MouseSim();
mouse.MoveTo(960, 540); // move cursor (absolute screen coords)
mouse.Click(); // left click
mouse.Click(MouseButton.Right); // right click
mouse.ClickAt(100, 200); // move then click
mouse.Scroll(3); // scroll up 3 detents
mouse.Scroll(-5, delayMs: 30); // scroll down, 30ms between ticksusing var hook = new KeyboardHook();
hook.KeyDown += (_, e) => Console.WriteLine($"↓ {e.KeyCode}");
hook.KeyUp += (_, e) => Console.WriteLine($"↑ {e.KeyCode}");
hook.Start();
// ... run message loop ...
hook.Stop(); // or Dispose()
⚠️ KeyboardHookmust be created on a thread with a message loop (e.g. the WinForms UI thread). Failing to callStop()/Dispose()leaks a system hook.
// Auto-clicker with random jitter (avoid detection)
var mouse = new MouseSim();
var rng = new Random();
while (running) {
mouse.Click();
Thread.Sleep(rng.Next(80, 150)); // human-like interval
}
// Hotkey listener → macro
using var hook = new KeyboardHook();
hook.KeyDown += (_, e) => {
if (e.KeyCode == Keys.F9) {
// trigger sequence when F9 pressed
kb.Press(Keys.D1); // select slot 1
Thread.Sleep(50);
mouse.Click();
}
};
hook.Start();
Application.Run(); // message loop
// Type text into any window (game chat, form, terminal)
var kb = new KeyboardSim();
kb.TypeUnicode("gg wp!这局打得不错");
// Scroll + click workflow (e.g. browser automation)
var mouse = new MouseSim();
mouse.MoveTo(960, 540);
mouse.Scroll(-3); // scroll down
Thread.Sleep(300);
mouse.ClickAt(760, 400); // click link
// Hold W to walk in a game for 2 seconds
var kb = new KeyboardSim();
kb.KeyDown(Keys.W);
Thread.Sleep(2000);
kb.KeyUp(Keys.W);- Why scan codes matter: Some games (Minecraft, many DirectInput titles) ignore virtual key codes and only respond to hardware scan codes. This library always sends both — the same behaviour as a real keyboard.
- Absolute mouse coordinates:
MouseSimmaps screen pixel coordinates to the0–65535range Win32 expects. Coordinates are relative to the primary screen. - Windows only — uses
user32.dll/kernel32.dll.
MIT © bkmashiro