Share via


Visual Basic Reference

KeyPreview Property Example

This example creates a form keyboard handler in the KeyDown event. Each of the first four function keys displays a different message. To try this example, paste the code into the Declarations section of a form, and then press F5. Once the program is running, press any one of the first four (F1 F4) function keys.

  Private Sub Form_Load ()
   KeyPreview = True
End Sub

Private Sub Form_KeyDown (KeyCode As Integer, Shift As Integer)
   Select Case KeyCode
      Case vbKeyF1: MsgBox "F1 is your friend."
      Case vbKeyF2: MsgBox "F2 could copy text."
      Case vbKeyF3: MsgBox "F3 could paste text."
      Case vbKeyF4: MsgBox "F4 could format text."
   End Select
End Sub