0

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. 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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1715925
  • 607
  • 9
  • 26
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Nov 06 '12 at 01:04
  • Surely it doesn't work because the Alt key is still down. Putting the Ctrl key down again wouldn't make a difference. So send the input to fake releasing Alt first. And put it back down afterwards. – Hans Passant Nov 06 '12 at 01:09
  • Thanks Hans, I already know the problem and have a solution like you mentioned, but was just wondering if it's a recommended approach or if it would lead to other subtle issues. – user1715925 Nov 07 '12 at 20:08

1 Answers1

1

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++. 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.

A likely easier, more efective and reliable solution usually is to use the WM_GETTEXT and WM_SETTEXT messages. And anticipating future questions, to push a button in another application send a BN_CLICKED notification to the parent of the button. You can use Spy++ to get many more answers to questions of messages.

Using SendInput might be the best solution for you, but there is not enough of your requirements provided to say for sure. In my opinion, SendInput should be the last resort and used only when the other possibilities are not possible. Please spend some time reading some of the numerous articles and previous answers relevant to the question.

Sam Hobbs
  • 83
  • 1
  • 6
  • Just because the question touches on something very often asked doesn't mean the author didn't invest time in researching related answers. A single tweak could change the whole dynamics of a problem. In my case, which you haven't provided an answer to, I already said to have a solution and only asking if it is the recommended approach. There are many articles and none have the answer I need which is why I made a post here. Your suggestion of sending messages is irrelevant since I asked about simulating keyboard inputs, not about how to get the selected text like most other questions are. – user1715925 Nov 07 '12 at 20:06
  • Sure, using Spy++ would help me know what's going on with the windows, but it doesn't tell me the best practice of solving an issue. In my opinion, if you decided to post an answer and lecture someone (which is fine), at least please read the question carefully and also try to point them towards some useful resources instead of making faulty assumptions and undocumented claims. – user1715925 Nov 07 '12 at 20:06
  • Yes, "A single tweak could change the whole dynamics of a problem.". Exactly. The information you provide in a questionb is critical. If you had tweaked your question differently, it is very possible you would have received better help, either from me or from someone else. Criticize me if you wish but if you are serious about getting help then please read the guideleines for asking questions. – Sam Hobbs Nov 08 '12 at 00:42