How to Trap Keystrokes in the Form Instead of Form's ControlsID: Q99688
|
To trap most keystrokes (see NOTE below) at the form level instead of
passing them to the form's controls, set the form's KeyPreview property to
True and use KeyAscii=0 in the Form_KeyPress event. This prevents
keystrokes from going to the form's controls.
NOTE: the technique described in this article will not intercept the ENTER
key on command buttons. Command buttons are subclassed Windows push button
controls and the ENTER key is an accelerator key that is passed to the
superclass; Visual Basic never receives it.
Also note that KeyCode=0 in the Form_KeyDown event won't prevent keystrokes
going to the form's controls. This behavior is by design.
A form's KeyPreview property determines whether form keyboard events are
invoked before control keyboard events. The keyboard events are KeyDown,
KeyUp, and KeyPress.
You can use the KeyPreview property to create a keyboard-handling procedure
for a form. For example, when an application uses function keys, it's
likely that you'll want to process the keystrokes at the form level rather
than writing code for each control that might receive keystroke events. If
a form has no visible and enabled controls, it automatically receives all
keyboard events.
To handle keyboard events only at the form level and not allow controls
to receive keyboard events, set KeyAscii to 0 in the form's KeyPress event.
KeyCode = 0
Additional query words: 2.00 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 1, 1999