consume keypress events

R

rege

Hi,

I have two applications developed in C# say A and B. When user presses
key "L" with applicaition A active , it causes a keypress event which
causes application B to run. Application A then waits for application B
to exit. On exit from Application B the focus shifts back to
application A.

The problem is if user presses L more than once, say thrice, then three
keypress events are generated. Therefore application A starts
application B and waits for it to exit. After we exit from B, the
application A processes the second keypress (2nd one of the three) and
application B starts again (which is undesirable!) .

Is there some way by which after the wait for exit we can get rid of
all the queued keypress events??

See the sample code snippet below:

private void ListBox_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar.ToString() == "l")
{
proc = Process.Start(procInfo); //starts application B
proc.WaitForExit(); //waits for B to exit.

//I want to consume all queued up keypress events here.
}



Please help!

Regards
Kshitij
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I think a better approach is to have a flag indicating the status of app B,
IMO it's not a good idea to mess with the msg. queue.

You could use either a bool variable or an enum for that.

cheers,
 
A

Alan Pretre

Is there some way by which after the wait for exit we can get rid of
all the queued keypress events??

Have a look at the virtual function ProcessCmdKey() that all WinForms
inherit. You can get at the keyboard keys at a lower level than the
keypress event. You might be able to make that do what you want.

-- Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top