Created
August 8, 2019 03:23
-
-
Save littletsu/d1c1b512d6843071144b7b89109a8de2 to your computer and use it in GitHub Desktop.
SendInput with Rust WinAPI crate (not for actual use in a serious rust program, just an example)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "sendinputexample" | |
version = "0.1.0" | |
authors = ["Dayaa <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
winapi = { version = "0.3.7", features = ["winuser"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[cfg(windows)] extern crate winapi; | |
use std::io::Error; | |
#[cfg(windows)] | |
fn press_key(key: u16, flags: u32) -> Result<(), Error> { | |
use winapi::um::winuser::{INPUT_u, INPUT, INPUT_KEYBOARD, KEYBDINPUT, SendInput}; | |
let mut input_u: INPUT_u = unsafe { std::mem::zeroed() }; | |
unsafe { | |
*input_u.ki_mut() = KEYBDINPUT { | |
wVk: key, | |
dwExtraInfo: 0, | |
wScan: 0, | |
time: 0, | |
dwFlags: flags | |
} | |
} | |
let mut input = INPUT { | |
type_: INPUT_KEYBOARD, | |
u: input_u | |
}; | |
let ipsize = std::mem::size_of::<INPUT>() as i32; | |
unsafe { | |
SendInput(1, &mut input, ipsize); | |
}; | |
Ok(()) | |
} | |
#[cfg(not(windows))] | |
fn press_key(key: u16) -> Result<(), Error> { | |
Ok(()) | |
} | |
fn main() { | |
const KEYUP: u32 = 0x0002; | |
press_key(0x91, 0); | |
println!("dw"); | |
std::thread::sleep(std::time::Duration::from_millis(30)); | |
println!("up"); | |
press_key(0x91, KEYUP); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment