Jacob
Jacob

Reputation: 1553

How to send a key using winapi's SendInput?

I am trying to convert this example to Rust 1.3 with winapi-rs 0.2.4.

I have:

fn send_key_event(vk: u16, flags: u32) {
    let mut input = winapi::INPUT {
        type_: winapi::INPUT_KEYBOARD,
        union_: winapi::KEYBDINPUT {
            wVk: vk,
            wScan: 0,
            dwFlags: flags,
            time: 0,
            dwExtraInfo: 0,
        }
    };
    unsafe {
        user32::SendInput(1, &mut input, mem::size_of::<winapi::INPUT>() as i32);
    }
}

which does not compile with:

error: mismatched types:
 expected `winapi::winuser::MOUSEINPUT`,
    found `winapi::winuser::KEYBDINPUT`
(expected struct `winapi::winuser::MOUSEINPUT`,
    found struct `winapi::winuser::KEYBDINPUT`) [E0308]

Haw do I send keystrokes to the active window?

Upvotes: 2

Views: 2401

Answers (1)

Francis Gagn&#233;
Francis Gagn&#233;

Reputation: 65742

The definition of winapi::INPUT in the version of winapi-rs you use is incorrect. It appears to have been fixed today (or yesterday, depending on where you are).

Upvotes: 3

Related Questions