364,033 Members | 4752 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

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

Rene
P: n/a
Rene
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


Christiaan Nijdam
P: n/a
Christiaan Nijdam
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" <rene@nomail.inval> wrote in message
news:uXmo3jRvFHA.3688@tk2msftngp13.phx.gbl...[color=blue]
> 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[/color]
on[color=blue]
> 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[/color]
it[color=blue]
> 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
>
>[/color]


Nov 17 '05 #2

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp