Link to home
Start Free TrialLog in
Avatar of devfredde
devfredde

asked on

Use WM_CHAR instead of keybd_event ???

I use keybd_event but if I call it to many time my program will exit with kernel32 error.

I think this happend because of leybd_event is to slow or something..

My program is in the background and I want it send a character from it to the activated window.

It's possible to use SendMessage and WM_CHAR and
use FoundWindow or something to find the
the activated windows and send a character to it from my program in the background ?


Please help me.
Avatar of devfredde
devfredde

ASKER

Edited text of question
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

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
>>Did you try using a short sleep (Sleep()) after each event?

No I haven't.
But I don't think it would help in this case.

You see, I send a chatacter over TCP/IP and use a server to read the incomming char.. and use Keybd_event to display it.

I have a text box, and every time I hit a key it will send the char.. over to the server and use Keybd_event to display it.
I use keybd_event twice, but the last one with the message key up.

keybd_event( 0x41,nCode,0,0 )
keybd_event( 0x41,nCode,KEYEVENTF_KEYUP,0,0 )

When I typed slowly it display all the char.. on the servers display but if I typed rapidly, some of the char.. will not be displayed or change place.

Exampel:

I typed "Hello Fredrik" rapidly
and the server displayed "Hell Ferdi"

To see if keybd_event is to slow. I use a loop on the client who send over 1000 of char... ( only A ). I only see one A and then it stoped and return a Kernel32 error.

Do you know what is wrong ?

 
I suspect what is wrong is that you aren't given the process that receives the keyboard event time to process it.  keyboard and mouse events are placed in a single system queue until the applications with the focus can remove them.  If you are using all the processor time to post to that queue, and are not allowing the application with the focus to remove the events from the queue, then the events will get lost and/or the queue will fill.

Make sure your process is not using too much processor time.  It should be frequently calling GetMessage(), Sleep() or other that allows it to yield CPU time.  
Is it working now?