如何在 Win32 中操作键盘输入?
或者为了澄清这个问题,我怎样才能让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所描述的伪造输入是由 SendInput 函数。输入进入具有输入焦点的线程。
What you describe, faking input, is implemented by the SendInput function. Input goes to the thread which has input focus.
您可以
SendMessage
到任何您想要的窗口,甚至在其他进程上。您甚至可以使用HWND_BROADCAST
将其发送到系统上的每个 to-level 窗口。但这是你真正想要的吗?如果您只对特定程序感兴趣,可以使用 FindWindow,然后仅将消息发送到该窗口。请注意,如果您只想将简单的击键注入到另一个进程中,那么
SendInput
确实是正确的选择。如果您想发送一些全局键盘快捷键,那么谁拥有焦点并不重要。如果您想使用SendInput
将相同的输入发送到多个窗口,则必须循环遍历窗口列表,并且对于每个窗口,首先设置焦点,然后发送输入。You can
SendMessage
to whatever window you want, even on other processes. You can even useHWND_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 usingSendInput
, you'll have to loop over the list of windows, and for each window first set the focus and then send the input.