Solved

keybd_event || SendInput

Posted on 2001-08-10
8
1,315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 31

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 31

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 
LVL 31

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 31

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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