KeyPress ....
-
Hi all, I'm trying to respond to the letter 'a' being pressed . But some how when i ran the form, this KeyPress function was not activated at all. Is there something else I need to do ? I have
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.frmMain_KeyPress);
inserted automatically already. Please help. Does this mean that the form is no focus??? I already clicked on the form and tried typing 'a' but no messagebox shown or was did debug mode enter the source code.private void frmMain_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { switch ( e.KeyChar) { case 'a': MessageBox.Show("a"); break; } }
-
Hi all, I'm trying to respond to the letter 'a' being pressed . But some how when i ran the form, this KeyPress function was not activated at all. Is there something else I need to do ? I have
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.frmMain_KeyPress);
inserted automatically already. Please help. Does this mean that the form is no focus??? I already clicked on the form and tried typing 'a' but no messagebox shown or was did debug mode enter the source code.private void frmMain_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { switch ( e.KeyChar) { case 'a': MessageBox.Show("a"); break; } }
Normally a form automatically receives all keyboard events, if it has no visible or enabled controls. But if it has controls, I think the focus is never on the form itself but always on a contained control. Try setting the
KeyPreview
property of your form to true, so your form receives key events before the event is passed to the control that has focus.