如何在 Win32 中操作键盘输入?

发布于 2024-12-10 00:51:32 字数 132 浏览 1 评论 0原文

或者为了澄清这个问题,我怎样才能让 Windows 认为我按下了某个键,而实际上我没有按下?我知道我可以使用 SendMessage 并在那里指定输入,但是这样不是只有我的应用程序会收到它吗?我希望控制所有应用程序接收“假”输入的程度。有什么建议吗?

Or to clarify this question, how can I make Windows think I hit a key, when I really didn't? I know I could possibly use SendMessage and specify the input there, but then wouldn't only my application receive it? I'd like control to the extent of all applications receiving the "fake" input. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猛虎独行 2024-12-17 00:51:32

您所描述的伪造输入是由 SendInput 函数。输入进入具有输入焦点的线程。

What you describe, faking input, is implemented by the SendInput function. Input goes to the thread which has input focus.

终难愈 2024-12-17 00:51:32

您可以SendMessage到任何您想要的窗口,甚至在其他进程上。您甚至可以使用 HWND_BROADCAST 将其发送到系统上的每个 to-level 窗口。但这是你真正想要的吗?如果您只对特定程序感兴趣,可以使用 FindWindow,然后仅将消息发送到该窗口。

请注意,如果您只想将简单的击键注入到另一个进程中,那么 SendInput 确实是正确的选择。如果您想发送一些全局键盘快捷键,那么谁拥有焦点并不重要。如果您想使用 SendInput 将相同的输入发送到多个窗口,则必须循环遍历窗口列表,并且对于每个窗口,首先设置焦点,然后发送输入。

You can SendMessage to whatever window you want, even on other processes. You can even use HWND_BROADCAST to send it to every to-level window on the system. But is that what you really want? If you're only interested in a specific program, you can get its window's handle using FindWindow, and then send the message only to that window.

Note that if all you want to do is a simple keystrokes injection into another process, then SendInput is indeed the way to go. If you'd like to send some global keyboard shortcut it doesn't matter who has the focus. If you'd like to send the same input to more than one window using SendInput, you'll have to loop over the list of windows, and for each window first set the focus and then send the input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文