Link to home
Start Free TrialLog in
Avatar of chandas
chandas

asked on

keybd_event || SendInput

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





Avatar of Zoppo
Zoppo
Flag of Germany image

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
Avatar of chandas
chandas

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chandas

ASKER

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
ok, no problem ... if it helps      :)               (well, I'm nearly sure it works coz I tested it)

BTW, nice story ...

regards,

ZOPPO

Avatar of chandas

ASKER

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

>.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
Hello chandas, how to fix ?

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