0

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.

Jab
  • 26,853
  • 21
  • 75
  • 114
  • `SendInput` can be blocked by UIPI. I'm also not sure whether input injected by `SendInput` will be observed by low-level input APIs typically used in games (XInput, Raw Input, etc.). That's something you would need to verify. – IInspectable Jun 25 '23 at 07:27
  • Also see [Can an application block calls to SendInput?](https://stackoverflow.com/q/19382846/1889329) The short of it is that programs can easily identify injected input, and just as easily block injected input from getting processed by the system. – IInspectable Jun 26 '23 at 07:16

0 Answers0