Form.KeyPress event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am trying to trace down the Enter key in my Form.KeyPress event handler.
The KeyPreview property is set to false, so I'd assume that all key presses
should go through my form's KeyPress event handler, right? Ok. If the focus
is set to any other control than a button, then the above event handler
fires, but if the focus is on a button, then I've no way to know that the
Enter key has been pressed or not, because in that case pressing Enter
doesn't fire KeyPress event of the Form. My question is Why?

Thanks for your help,
 
Hello,

Try setting the Form.KeyPreview to true then add an Eventhandler to the
forms KeyUp event, this should capture the Enter-key.
 
Sorry, I should've said that KeyPreview is set to TRUE. Now, I was able to
capture the Return key using KeyUp event, but the button still gets pressed
even after setting e.Handled to TRUE. Do you know how to prevent this?
 
Hello,

I guess you _COULD_ override the ProcessDialogKey method to disable the
Enter-key for a button (but sill allow it to be clicked):

private void button1_Click(object sender, EventArgs e) {
// button clicked
Debug.WriteLine("BClick");
}

protected override bool ProcessDialogKey(Keys keyData) {
if (keyData == Keys.Enter) {
//TODO: something meaningful
Debug.WriteLine("Something meaningful");

if (this.ActiveControl == button1) {
// don't send the enter to the button
return false;
}
}
return base.ProcessDialogKey (keyData);
}

Hope it helps!
 
Back
Top