Solved

keybd_event || SendInput

Posted on 2001-08-10
8
1,290 Views
Last Modified: 2013-12-03
Hi guys, here's my problem.

I would like to make sure that SendInput() which I'm using to synthesise a particular key stroke, actually carries out that keystroke before moving on to the next line of code. For example

SendInput(...) //where I'm synthesing VK_DELETE

because I'm also handling WM_KEYUP messages, shouldn't SendInput() send a WM_KEYUP message immediately?

Instead, it carries out the lines of code AFTER SendInput(), and therefore seems to actually carry out the key press at the wrong time. It's almost as though it's working in a separate thread.

This is troublesome because the code after SendInput is depenedent on the key press being simulated first.

Has anyone got any ideas on how best to achieve my goal?

Oh, I get the same behaviour with keybd_event(...)

Thanks

Senkwe





0
Comment
Question by:chandas
  • 4
  • 3
8 Comments
 
LVL 30

Expert Comment

by:Zoppo
ID: 6371775
Hi chandas,

in both cases (SendInput or keybd_event) you'll have to explicitely simulate both the
key-press and the key-release. I.e. with keybd_event you can do it like this:


keybd_event( VK_DELETE, 0, 0, 0 ); // simulates key press, so a WM_KEYDOWN message is sent
keybd_event( VK_DELETE, 0, KEYEVENTF_KEYUP, 0); // simulates key release (WM_KEYUP);

BTW, if you want your app to be able to run on Win95 or WinNT 3.x or WinNT 4 with ServicePack lower
than version 3 you'll need to use keybd_event instead of SendInput.

hope that helps,

ZOPPO
0
 

Author Comment

by:chandas
ID: 6371791
Hi Zoopos, thanks for the quick response.

Firstly, yes...I do perform a key release after the key press. With SendInput it's a matter of sending in an array of INPUT structures. In my case this array will be two structures big. The first array cell will hold the "key pressed" input structure and the second the "key up" structure.

Secondly, yes, I'm aware of the fact that SendInput is Win98, NT and 2000 specific only, I don't mind too much for now. I would just like it to work.

Actually, I had the exact same code with keybd_event(...) exactly as you've suggested there and it didn't work, which led me to try SendInput. Truthfully, I'd rather use keybd_event() so that I can target Win95 as well.

So it looks like once the solution is found for one func, it will work for the other.

Thanks so much for the suggestions though, if you have any more please do send them through
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 150 total points
ID: 6371838
ok, think I found the problem ... as it's the default behavior of windows these
messages produced by SendInput and keybd_event are put into the application's
message queue and NOT directly called using SendMessage (which would do
it as you need).And this is correct behavior, because the same would happen
if you really press a key. So, simply implement function like this:

void FlushMessages( HWND hWnd )
{
 MSG rMsg;
 while( ::PeekMessage(&rMsg, hWnd, 0,0, PM_NOREMOVE) ) {
  AfxGetApp()->PumpMessage();
 }
}

and call it like this:
keybd_event( VK_DELETE, 0, 0, 0 ); // simulates key press, so a WM_KEYDOWN message is sent
FlushMessages( m_hWnd ); // you can also pass NULL for hWnd
keybd_event( VK_DELETE, 0, KEYEVENTF_KEYUP, 0); // simulates key release (WM_KEYUP);
FlushMessages( m_hWnd ); // dito


hope that helps,

ZOPPO
0
 

Author Comment

by:chandas
ID: 6372057
Wow, thanks Zoppo...that looks like it just might work. I had actually thought maybe the keyboard messages were treated differently from other messages. So I even tried to reset the keyboard state back to "no virtual keys pressed".

I'll try this out when I get home tonight and will likely update this thread tomorrow morning (I'm in South Africa) by the way.

On an interesting sidenote, I was once debugging some similar piece of code except this time I was using keybd_event to simulate a VK_ENTER. I put a breakpoint just before the keybd_event code and when I stepped through it, the VK_ENTER was processed WITHIN MY DEBUG WINDOW. For a while I was wondering why my source code kept getting edited, I mean why was my code getting shifted down a line when all I was doing was tracing through it?? hehe.

Thanks alot for the info, I'll update this first chance I get.

Senkwe
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Zoppo
ID: 6372171
ok, no problem ... if it helps      :)               (well, I'm nearly sure it works coz I tested it)

BTW, nice story ...

regards,

ZOPPO

0
 

Author Comment

by:chandas
ID: 6378954
Hi Zoppos, sorry for the delay. Well I tried your suggestion and for some reason...

while( ::PeekMessage(&rMsg, hWnd, 0,0, PM_NOREMOVE) )

...hangs. It's always equal to 1. I'm awarding the points because I've learned something new from your suggestion, and also because I've found another, albeit unrelated, fix.

Thanks again Zoppos

0
 
LVL 30

Expert Comment

by:Zoppo
ID: 6391613
>.hangs. It's always equal to 1.
hmm, strange, coz I've tested it here with a small (dialog-)test app ... maybe your message handling differs from mine    :)

glad I could help you...

regards,

ZOPPO
0
 

Expert Comment

by:tong111797
ID: 8090725
Hello chandas, how to fix ?

>because I've found another, albeit unrelated, fix.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now