I'm new to rust and the windows API and I'm trying to use the PostMessage function using the windows-rs crate. However, I'm not sure what data type WPARAM expects. I've tried guessing since the windows-rs documentation doesn't seem to say. The official microsoft docs for C++ seem to expect the constants found below.. but I get an error when I try using them with the rust crate.
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagea
use windows::{
Win32::System::Threading::*, Win32::UI::WindowsAndMessaging::*, Win32::UI::Input::KeyboardAndMouse::*
};
fn main() {
unsafe {
Sleep(1000);
let wh = GetActiveWindow();
PostMessageA(wh, WM_KEYDOWN, VK_ADD, None);
}
}
I get an error telling me that WPARAM doesn't have an implementation for "VIRTUAL_KEY". I've also tried using the hexdecimal for VK_ADD in various ways.. but each one throws errors about WPARAM not implementing the type I try to use.
PostMessageA(wh, WM_KEYDOWN, 0x6B, None);
PostMessageA(wh, WM_KEYDOWN, "0x6B", None);
let hex: u32 = 0x6B;
PostMessageA(wh, WM_KEYDOWN, hex, None);
I'm also not sure how to send the hexadecimal properly formatted either, so I'm probably doing that incorrectly. Does anyone have any ideas, or could anyone with a better understanding of rust check out the source code of the crate?
As @Jmb said in his comment, using WPARAM(VK_ADD.0 as _)
should work, but you would also need to pass the LPARAM
argument as required by the WM_KEYDOWN
message, but if you simply want to send keyboard input then its better and easier to use SendInput