JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Handling Keyboard Events

You can handle keyboard events in forms and many controls with these events:

  • KeyDown— Happens when a key is pressed down while the control has focus.

  • KeyPress— Happens when a key is pressed while the control has focus.

  • KeyUp— Happens when a key is released while the control has focus.

For KeyDown and KeyUp events, the event handler receives an argument of type KeyEventArgs containing data related to this event, with these properties:

  • Alt— Holds a value indicating whether the Alt key was pressed.

  • Control— Holds a value indicating whether the Ctrl key was pressed.

  • Handled— Holds or sets a value indicating whether the event was handled.

  • KeyCode— Holds the keyboard code for a KeyDown or KeyUp event.

  • KeyData— Holds the keyboard data for a KeyDown or KeyUp event.

  • KeyValue— Holds the keyboard value for a KeyDown or KeyUp event.

  • Modifiers— Holds the modifier flags for a KeyDown or KeyUp event. This indicates which modifier keys (Ctrl, Shift, and/or Alt) were pressed. These values can be ORed together (for more on the OR operator and how it works, see "Using Visual Basic Operators" in Chapter 2)—using the Control, Shift, and Alt properties is usually easier.

  • Shift— Holds a value indicating whether the Shift key was pressed.

For KeyPress events, you get an argument of type KeyPressEventArgs containing the following KeyPressEventArgs properties:

  • Handled— Gets or sets a value indicating whether the KeyPress event was handled. If you set this value to True, Visual Basic will not handle this key (so if you want to delete it, set Handled to True and do no further processing on it).

  • KeyChar— Holds the character corresponding to the key pressed.

In the KeyDown and KeyUp events, you're responsible for determining which modifier key—Ctrl, Shift, or Alt—is down. Letter characters are passed to you as character codes as though they were in upper case, whether or not they should be. Here's an example where I check if Shift was actually on, and decipher the key code passed to us, displaying it in a message box in a KeyDown event handler (I'm only handling letter keys in this example):

Dim strText As String

Private Sub Form1_KeyDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode >= Keys.A And e.KeyCode <= Keys.Z Then
        strText += Switch(e.Shift, Chr(e.KeyCode), Not e.Shift, _
            Char.ToLower(Chr(e.KeyCode)))
        MsgBox(strText)
    End If
End Sub

If you want to get the actual character typed without this kind of checking, use the KeyPress event, which passes you the character as a Char object directly, making handling that character much easier (KeyPress events occur after Visual Basic has had a chance to process and decipher the key itself):

Dim strText As String

Private Sub Form1_KeyPress(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    Handles MyBase.KeyPress
    strText += e.KeyChar
    MsgBox(strText)
End Sub

For more on KeyPress, including an example showing how to discard typed input, see "Controlling Input in a Text Box" in Chapter 5.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor