myForm.KeyPreview = true; ... protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Escape) { this.Hide(); return true; } return base.ProcessDialogKey(keyData); }
February 2, 2008
Keyboard Handling in Win Forms
C# Key Processing Techniques
Use
Form.KeyPreview = true
Tells form to receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true and the currently selected control is a TextBox, after the keystroke is handled by the event-handling methods of the form the TextBox control will receive the key that was pressed. To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event-handling method to true.
eg.: Hide the form when the Escape key is Pressed
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment