views:

103

answers:

1

Ok so I might be totally barking up the wrong tree here as I'm not that up to date on Windows programming but here goes.

I'm trying to initiate a simulated keypress in a C++ project in Visual Studio 2010.

Essentially when the program receives a specific code string from another application (which is all being worked via many if statements, messy but works)

I need it to perform the same function as if I pressed control and s on my keyboard.

Now some code I found was this.

keybd_event(VK_LCONTROL,0,0,0); 
  keybd_event(bKey, 0, 0, 0);


   keybd_event(bKey, 0, KEYEVENTF_KEYUP, 0);
  keybd_event(VK_LCONTROL,0,KEYEVENTF_KEYUP,0); 

Whereby bKey is the appropriate ASCII code for S, it escapes me off hand but still, it's mostly irrelevant to this post.

What this should do by my understanding of it is

Push Control down
Push S down
Release S
Release Control.

However it does not seem to do this, it seems to only push down and release S thereby printing S as appropriate or not depending if text entry is available at the time.

Am I barking up the wrong tree altogether here? I know how to do this in Objective C in Xcode but the two seem quite radically different, I know they're different systems but still.

Can anyone propose a solution to this? Basically a way to make the program hit Control + S together (not one after the other, together).

Any help is much appreciated. There seems to be a lot of conflicting information out there.

+1  A: 

Try this instead:

keybd_event(VK_LCONTROL,MapVirtualKey(VK_LCONTROL,0),0,0); 
keybd_event(bKey, MapVirtualKey(bKey,0), 0, 0);


keybd_event(bKey, MapVirtualKey(bKey,0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_LCONTROL,MapVirtualKey(VK_LCONTROL,0),KEYEVENTF_KEYUP,0); 
Blindy
Will give it a go. Taking a short break before my head imploded. Can you explain to me what it is that the new code does differently? Doesn't need to be exacting detail as I rarely work in the language just need some idea.
David26th
Well as far as I know, `keybd_event` needs the actual scan code. You provided a 0 for it, whereas I'm giving it the real scan code.
Blindy
Ok so I've tried that, and it works - kind of, the trouble is this.For sake of this test I subsituted Control with Shift, and bKey with 062 (Numerical key 2)If I put this into notepad, I get " (which is expected behaviour for the program), if I press it in another context where there is not a text input option it simply responds as 2, for example if I were in a game where I had to press Shift + 2 (think a RTS for example) then it responds only as 2. Any more ideas?
David26th
It's probably because games access the keyboard through DirectInput instead of the normal windows message queue. I can't think off the top of my head how to work around that though..
Blindy
Hm, I thought that too however I just tried control S (save) in notepad and that didn't work right either.
David26th
As a follow up - would SendInput() have any likelihood of better results?
David26th
SendInput() is pretty much a wrap around keybd_event and mouse_event, it most likely won't do much better. Try adding a small delay between the key presses and releases, and increasing it until it works. Maybe you'll have some luck with that.
Blindy
Inserting ::sleep(50);between each of the commands did the job beautifully. Thank you so very much for your help. I owe you one :-)
David26th