简体   繁体   English

您如何将 PostMessage 与 windows-rs 板条箱一起使用?

[英]How do you use PostMessage with the windows-rs crate?

I'm new to rust and the windows API and I'm trying to use the PostMessage function using the windows-rs crate.我是Z72812E308734555DCEE2D1EE26E4ABZ和windows ZDB5C42Z ZDB974238714CA8DE14CAY1717171D727141414514514F. However, I'm not sure what data type WPARAM expects.但是,我不确定 WPARAM 期望什么数据类型。 I've tried guessing since the windows-rs documentation doesn't seem to say.我试过猜测,因为 windows-rs 文档似乎没有说。 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. C++ 的官方微软文档似乎期望下面找到的常量.. 但是当我尝试将它们与 rust 板条箱一起使用时出现错误。

https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/UI/WindowsAndMessaging/fn.PostMessageA.html https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/UI/WindowsAndMessaging/fn.PostMessageA.ZFC35FDC70D5FC69D2698Z83A822C

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagea https://docs.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".我收到一条错误消息,告诉我 WPARAM 没有“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.我也尝试过以各种方式对 VK_ADD 使用十六进制。但每个人都会抛出关于 WPARAM 没有实现我尝试使用的类型的错误。

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?有没有人有任何想法,或者对 rust 有更好理解的人可以查看 crate 的源代码吗?

https://github.com/microsoft/windows-rs https://github.com/microsoft/windows-rs

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正如@Jmb 在他的评论中所说,使用WPARAM(VK_ADD.0 as _)应该可以工作,但是您还需要按照WM_KEYDOWN消息的要求传递LPARAM参数,但是如果您只是想发送键盘输入,那么它会更好并且更容易使用SendInput

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM