Skip to content

bkmashiro/KeyMouseSim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KeyMouseSim

MIT License Platform

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.


Classes

KeyboardSim — Send key events

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 character

MouseSim — Send mouse events

var 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 ticks

KeyboardHook — Listen to global key events

using 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()

⚠️ KeyboardHook must be created on a thread with a message loop (e.g. the WinForms UI thread). Failing to call Stop() / Dispose() leaks a system hook.


Use Cases

// 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);

Notes

  • 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: MouseSim maps screen pixel coordinates to the 0–65535 range Win32 expects. Coordinates are relative to the primary screen.
  • Windows only — uses user32.dll / kernel32.dll.

License

MIT © bkmashiro

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages