如何使用 C++ 将击键/消息发送到 Windows 7和Windows API?
例如,程序是否可以向 Windows 7(其运行的操作系统)发送击键或消息来模拟用户在键盘上实际按下 Windows 键的效果?在 C++ 中,使用 Windows API?
Is it possible for a program to send keystrokes or messages to Windows 7 (the operating system it's running on) to simulate the effect of a user physically pressing the Windows key on their keyboard, for example; in C++, using the Windows API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SendInput 函数已经存在很长时间了,应该可以满足您的需要。
The SendInput function has been available for ages, and should do just what you need.
初学者经常尝试将密钥发送到窗口,因为他们认为这是最简单的解决方案。肯定很容易回答说这是可能的。通常,在开发人员投入大量时间后,事情就会变得复杂。它复杂的原因之一是您必须确保控件(文本框或其他)具有焦点。然后你必须做一些事情来处理数据,例如按下按钮。您可能需要阅读该窗口来决定下一步该做什么。
另一种方法是提升一个级别并尝试通过访问控件及其父级(窗口)来控制应用程序。换句话说,您可以将数据作为字符串直接放入文本框中,而不是通过在其中键入按键来实现。您可以向窗口发送 BN_CLICKED 通知消息,而不是向按钮发送 Enter 键来单击按钮。你应该寻找方法来做这类事情。这是完全有可能的。
熟悉 Spy++;它是一个真正可以帮助您探索控件和窗口等的工具。
Beginners often try to send keys to windows because they think it is the easiest solution. It is sure easy to reply by saying it is possible. Often, after the developer invests a significant amount of time, it becomes complicated. One reason it is complicated is because you must ensure that the control (the textbox or whatever) has the focus. Then you have to do something to get the data processed, such as push a button. You might need to read the window to decide what to do next.
An alternative is to go up a level and try to control anapplication by accessing the controls and their parent (the window). So in other words you can put data into a textbox directly as a string, not by typing keys into it. You can send a BN_CLICKED notification message to the wndow instead of sending an enter key to the button to click the button. You should look for ways to do that type of thing. It is totally possible.
Become familiar with Spy++; it is a tool that can really help you to explore the controls and windows and such.
您可以使用 进行任何模拟输入SendInput,但是,您受到应用程序完整性级别的约束(也就是说,您不能将输入注入到比您的级别更高的应用程序中)。
You can do any simulated input using SendInput, however, you are bound by the application integrity level (that is, you cannot inject input into applications that have a higher level than yours).