How to simulate a keypress

Viewed 1485

I know about AutoHotKey but I want to make my own program for e.g. make it press F5 every 10 seconds. I searched the internet and Stack Overflow but did not find a solution

Is there a way to do it in C or not? I am using and targeting Windows 8.1

2 Answers

If you need the key modular, check out my implementation (without event loop)

#include <windows.h>

const int key_a = 0x41;
const int key_b = 0x42;
const int key_c = 0x43;
const int key_d = 0x44;

write(int key) {

    INPUT inputs[2] = { 0 };

    inputs[0].type = INPUT_KEYBOARD;
    inputs[0].ki.wVk = key;

    inputs[1].type = INPUT_KEYBOARD;
    inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;

    SendInput(2, inputs, sizeof(INPUT));
}

main() {
    // Some time to select another window here (e.g. Notepad)
    Sleep(3000);

    write(key_a);
    write(key_b);
    write(key_c);
    write(key_d);
}
Related