inpctrl is a simple, cross-platform, header-only C++ library to handle keyboard and mouse input for macros and automation tools. It supports Windows and Linux (macOS is not supported).
- Check if a key is currently pressed
- Press, hold, and release keys programmatically
- Move the mouse relative to its current position
- Map between human-readable key names and system key codes
- Thread-safe key state tracking
- Pure header-only, no compilation required
- Uses GetAsyncKeyState to detect key states
- Installs a low-level keyboard hook for accurate key tracking
- Sends input events via SendInput for simulating key presses/releases
- Mouse movement is simulated with SendInput relative motion
- Reads
/dev/input/event*devices for real-time key states - Sends synthetic key/mouse events via uinput
- Maps Windows virtual key codes to evdev key codes for consistency
- Uses a dedicated thread to listen for input events and update key states
Simply include the header in your project:
#include "inpctrl.hpp"No compilation or linking is required.
#include "inpctrl.hpp"
#include <iostream>
int main() {
inpctrl::CrossInput input;
if (!input.init()) {
std::cerr << "Failed to initialize input system!" << std::endl;
return 1;
}
// Press and release the 'A' key
input.pressKey(inpctrl::CrossInput::Key::A);
// Move mouse 100 pixels right and 50 pixels down
input.moveMouse(100, 50);
input.cleanup();
return 0;
}You can also hold and release keys manually:
input.holdKey(inpctrl::CrossInput::Key::Space);
// do something while holding Space
input.releaseKey(inpctrl::CrossInput::Key::Space);-
bool initWindows()
Initializes Windows input system with hook and async key tracking. -
void cleanupWindows()
Cleans up hook and input resources. -
void holdKeyWindows(unsigned int vkCode)
Simulates pressing a key. -
void releaseKeyWindows(unsigned int vkCode)
Simulates releasing a key. -
void moveMouseWindows(int dx, int dy)
Moves the mouse relative to current position.
-
bool initLinux()
Initializes Linux input system via/dev/inputanduinput. -
void cleanupLinux()
Cleans up file descriptors and virtual device. -
void holdKeyLinux(unsigned int evdevCode)
Simulates pressing a key. -
void releaseKeyLinux(unsigned int evdevCode)
Simulates releasing a key. -
void moveMouseLinux(int dx, int dy)
Moves the mouse relative to current position. -
unsigned int toEvdevCode(unsigned int vkCode)
Converts Windows virtual key code to Linux evdev code. -
unsigned int fromEvdevCode(unsigned int evdevCode)
Converts Linux evdev code back to Windows virtual key code.
-
bool init()
Initializes the input system (Windows or Linux). -
void cleanup()
Cleans up resources. -
bool isKeyPressed(Key key)
Returns true if the key is currently pressed. -
void holdKey(Key key)
Press and hold a key. -
void releaseKey(Key key)
Release a key. -
void pressKey(Key key, int delayMs = 50)
Press and release a key with optional delay. -
void moveMouse(int dx, int dy)
Move the mouse relative to its current position. -
std::string getKeyName(Key key)
Returns a human-readable name for a key.
MIT License. See LICENSE file.
- @quuuut on github.
- https://github.com/Spencer0187/Spencer-Macro-Utilities for the examples.