Simulating keystrokes does not always work

93 Views Asked by At

I am making a streaming bot and I'm trying to have the bot be able to simulate keystrokes/presses, this function works currently in my tests, but for some reason I'm getting complaints that it won't always work or won't work in some games/applications.

Do I need to somehow get the PID of the currently working application and send it the events somehow? Or is there something I'm missing?

use windows::Win32::UI::Input::KeyboardAndMouse::{
    SendInput,
    INPUT,
    INPUT_0,
    KEYBD_EVENT_FLAGS,
    VIRTUAL_KEY,
    INPUT_KEYBOARD,
    KEYBDINPUT,
};

fn keybd_event(flags: KEYBD_EVENT_FLAGS, vk: VIRTUAL_KEY, scan: ScanCode) {
    let input = INPUT {
        r#type: INPUT_KEYBOARD,
        Anonymous: INPUT_0 {
            ki: KEYBDINPUT {
                wVk: vk,
                wScan: scan,
                dwFlags: flags,
                time: 0,
                dwExtraInfo: 0,
            },
        },
    };
    unsafe {
        SendInput(
            &[input as INPUT],
            size_of::<INPUT>()
                .try_into()
                .expect("Could not convert the size of INPUT to i32"),
        )
    };
}

The reason I'm not using Enigo is it uses the standard convention of simulating keystrokes and that was even worse when it came to applications responding to the keystrokes.

0

There are 0 best solutions below