开发者

Simulating keypresses in Visual C++

开发者 https://www.devze.com 2023-01-09 14:31 出处:网络
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.

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.


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); 
0

精彩评论

暂无评论...
验证码 换一张
取 消