OnKeyDown
The OnKeyDown event occurs when the user presses a key on a physical or virtual keyboard while the control (i.e. TextBox) has focus. It occurs before the actual text is inserted into the control . The OnKeyDown event enables you to intercept and handle the keyboard input before its is processed by the control. By being able to intercept the input before its processed by the control, you can use this event to validate the input, restrict certain keys, or interpret the combination (i.e. Shift + keyboard key) to call other functions.
In other words, this event is typically used to capture incoming keystrokes and perform some action based upon user input.
For legacy programs, the OnKeyDown event replaces OnChar event.
Group: Events
Applies to: Application, Prompts
Syntax: OnKeyDown(KeyCode, Shift, Handled)
KeyCode (Long) is the value of the key pressed.
Shift (Boolean) True means the user had the Shift key was also pressed; False it was not.
Handled (Boolean) True means the event was handled, False means the event was not handled.
Example
Private Sub TextBox1_OnKeyDown(ByVal KeyCode As Long, ByVal Shift As Boolean, ByRef Handled As Boolean)
'On Error Resume Next
If KeyCode = 65 And Shift = True Then
Handled = True ' Indicate that the event is handled
MsgBox "The letter A was entered."
Else
MsgBox "The letter A was not entered."
End If
End Sub
Version Supported: