Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

keybd_event || SendInput

Posted on 2001-08-10
8
Medium Priority
?
1,335 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 450 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

730 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