By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,715 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

KeyPress not fired in some cases when Form.KeyPreview is on

P: n/a
Hi,

I am running is some problems with the KeyPreview and KeyPress events.

The KeyPress event is only triggered when there this an focusable control on
the form. When all controls are disabled the Form.KeyPress event does not
trigger anymore.

A way to 'solve' this is to set the focus to the form, but during testing it
happens ofter the keypress event is not getting triggered on keys like
ENTER. Clicking a few times on the main form will get the even triggered
again.

Is there a good solution to solve this, or is there a way to convert the
keys in ProcessCmdKey() to keydata?

TIA,

Rene
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Rene,

It seems like the keyPress event doesn't trigger sometimes for the Enter key
because KeyDown doesn't. I don't know why or where the event goes. Have you
considered using only the KeyUp event?

Christiaan.
....
this.KeyDown += new KeyEventHandler(MyKeyDownEventHandler);
this.KeyPress += new KeyPressEventHandler(MyKeyPressEventHandler);
this.KeyUp += new KeyEventHandler(MyKeyUpEventHandler);
this.Click+=new EventHandler(MyClickEventHandler);

}

public void MyKeyDownEventHandler(object sender,KeyEventArgs e)
{
if (e.KeyCode==Keys.Enter) System.Console.Write("ENTER");
System.Console.WriteLine("down");
e.Handled=true;
}

public void MyKeyUpEventHandler(object sender,KeyEventArgs e)
{
if (e.KeyCode==Keys.Enter) System.Console.Write("ENTER");
System.Console.WriteLine("up");
e.Handled=true;
}

public void MyKeyPressEventHandler(object sender,KeyPressEventArgs e)
{
if (e.KeyChar==13) System.Console.Write("ENTER");
System.Console.WriteLine("press");
e.Handled=true;
}

public void MyClickEventHandler(object sender,EventArgs e)
{
button1.Enabled=!button1.Enabled;
}

....
this.KeyPreview = true;

"Rene" <re**@nomail.inval> wrote in message
news:uX**************@tk2msftngp13.phx.gbl...
Hi,

I am running is some problems with the KeyPreview and KeyPress events.

The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled the Form.KeyPress event does not
trigger anymore.

A way to 'solve' this is to set the focus to the form, but during testing it happens ofter the keypress event is not getting triggered on keys like
ENTER. Clicking a few times on the main form will get the even triggered
again.

Is there a good solution to solve this, or is there a way to convert the
keys in ProcessCmdKey() to keydata?

TIA,

Rene

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.