简体   繁体   English

激活热键组合后如何正确模拟键盘事件

[英]how to properly simulate keyboard events once a hotkey combination is activated

I have a program that registers a certain hotkey combination for activation. 我有一个注册用于激活的特定热键组合的程序。 For the purpose of this question, let's say this hotkey is Ctrl + Alt + D. Once the combination is pressed, my program will then simulate some other keyboard events to the active window using SendInput, for example, it could simulate Ctrl + A. My issue is that the simulation of Ctrl + A doesn't work properly since (I think) the Ctrl key is still pressed from the hotkey combination. 就此问题而言,假设此热键为Ctrl + Alt +D。一旦按下组合键,我的程序便会使用SendInput将其他一些键盘事件模拟到活动窗口,例如,它可以模拟Ctrl +A。我的问题是Ctrl + A的模拟无法正常工作,因为(我认为)仍然从热键组合中按下了Ctrl键。 One of the solutions I'm thinking to do is to send Key Up events for every key in my hotkey combination and then simulate Ctrl + A, however I wanted to check if this is the proper way of handling it? 我想做的解决方案之一是为我的热键组合中的每个键发送Key Up事件,然后模拟Ctrl + A,但是我想检查这是否是正确的处理方法?

First, understand that questions asking how to send keyboard messages are asked very often. 首先,要理解经常问有关如何发送键盘消息的问题。 I assume you have not invested much time looking for previous answers. 我认为您没有花太多时间寻找以前的答案。 Second, understand that beginners often mistakenly think that sending keyboard messages is the easiest and most effective solution. 其次,要了解初学者通常会错误地认为发送键盘消息是最简单,最有效的解决方案。 The truth is that it is usualy not the easiest and is not the most effective and not the most reliable. 事实是,这通常不是最容易的,也不是最有效的,也不是最可靠的。

If you are going to work with Windows messages like that, then learn to use Spy++. 如果要使用类似Windows的消息,请学习使用Spy ++。 If you do not know what that is, then please invest a minute by familiarizing yourself with the tools available in the VS Tools menu. 如果您不知道这是什么,请花一点时间来熟悉VS Tools菜单中提供的工具。

A likely easier, more efective and reliable solution usually is to use the WM_GETTEXT and WM_SETTEXT messages. 一个可能更简单,更有效和更可靠的解决方案通常是使用WM_GETTEXT和WM_SETTEXT消息。 And anticipating future questions, to push a button in another application send a BN_CLICKED notification to the parent of the button. 并预见将来的问题,若要在另一个应用程序中按一个按钮,则向该按钮的父级发送BN_CLICKED通知。 You can use Spy++ to get many more answers to questions of messages. 您可以使用Spy ++获得更多有关消息问题的答案。

Using SendInput might be the best solution for you, but there is not enough of your requirements provided to say for sure. 使用SendInput可能是最适合您的解决方案,但是没有足够的需求可以肯定地说。 In my opinion, SendInput should be the last resort and used only when the other possibilities are not possible. 我认为,SendInput应该是最后的选择,并且仅在其他可能性不可行时才使用。 Please spend some time reading some of the numerous articles and previous answers relevant to the question. 请花一些时间阅读大量与该问题相关的文章和先前的答案。

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

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