KeyDown, KeyUp Events

See AlsoF0EZ6Z              ExampleAJAK4T4>Low

Apply To

Form14TJ2LN, check box9P3BU5, combo box1YZXFF6, command buttonXJSPC0, directory list boxO9U5A0, drive list box5WJO0PW, file list box1M6S8UX, grid2VGT0PT, horizontal scroll bar1JSJOS7, list boxG11UCK, OLE control2HQDVVU, option buttonJYBO08, picture box31MYIWX, text boxYPYZDG, vertical scroll bar1JSJOS7.


Occur when the user presses (KeyDown) or releases (KeyUp) a key while an object has the focus1L3L8ZY.  (To interpret ANSI5221FB characters, use the KeyPressPFFDUA event.)


Sub Form_KeyDown (KeyCode As Integer, Shift As Integer)

Sub ctlname_KeyDown ([Index As Integer,]KeyCode As Integer, Shift As Integer)

Sub Form_KeyUp (KeyCode As Integer, Shift As Integer)

Sub ctlname_KeyUp ([Index As Integer,]KeyCode As Integer, Shift As Integer)


The KeyDown and KeyUp events use these arguments:

Argument     Description


Index             Uniquely identifies a control if it is in a control array8G7Y0UU.

KeyCode        A key code, such as, KEY_F1 (F1 key) and KEY_HOME (Home key).  To specify key codes, use the constantsTD9WSV in CONSTANT.TXT, a Visual Basic file that specifies system defaults.

Shift              The state of the Shift, Ctrl, and Alt keys at the time of the event.  The Shift argument is a bit field3JH793R, with the least-significant bits corresponding to the Shift key (bit 0), the Ctrl key (bit 1), and the Alt key (bit 2 ).  These bits correspond to the values 1, 2, and 4, respectively.  Some, all, or none of the bits can be set, indicating that some, all, or none of the keys is pressed.  For example, if both Ctrl and Alt are pressed, the value of Shift is 6.


For both events, the object with the focus receives all keystrokes.  A form can have the focus only if it has no visible and enabled controls.  Although KeyDown and KeyUp can apply to most keys, they are most often used for:

         Extended character keys such as function keys58I6XR.

        Navigation keys3ZZ1RT5.

         Combinations of keys with standard keyboard modifiers198ZVV6.

         Distinguishing between the numeric keypad and regular number keys.


Use KeyDown and KeyUp event procedures for keyboard handlers if you need to respond to both the pressing and releasing of a key.

KeyDown and KeyUp are not invoked for:

         The Enter key if the form has a command button with the Default31OHP7N property set to True.

         The Esc key if the form has a command button with the CancelKQ58YS property set to True.

         The Tab key.


If you need to test for the Shift argument, you can declare constantsTD9WSV that define the bits within the argument by loading the CONSTANT.TXT file into a module.  The Shift constants have the following values:

Constant          Value



CTRL_MASK      2

ALT_MASK         4


The constants act as bit masks17EJ9RH that you can use to test for any combination of buttons.  You can place the constants at the procedure level or in the Declarations section of a module where you can use this syntax:

Global Const Constantname = expression

You test for a condition by first assigning each result to a temporary integer variable and then comparing Shift to a bit mask.  Use the AndLANAND operator with the Shift argument to test whether the condition is greater than 0, indicating that the modifier was pressedfor example:

ShiftDown = (Shift And SHIFT_MASK) > 0

In a procedure, you can test for any combination of conditionsfor example:

If ShiftDown And CtrlDown Then


Note   If the KeyPreview18F9O8A property is True, a form receives these events before controls on the form receive the events.  Use the KeyPreview property to create global keyboard-handling routines.


See Also



KeyPreview Property18F9O8A

Operator Precedence23082A9

SendKeys Statement41LDB48


Programmer's Guide:

Chapter 3, "Creating and Using Controls"

KeyDown, KeyUp Events Example

The example demonstrates a generic keyboard handler that responds to the F2 function key and to all the associated Alt, Shift, and Ctrl combinations.  To find the value of the key constants, look at the CONSTANT.TXT file or load it into a module.  To try this example, paste the code into the Declarations section of a form that contains a text box.  Then press F5 and press F2 with the Alt  Shift, and Ctrl keys.

Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)

   Dim ShiftDown, AltDown, CtrlDown, Txt

   Const KEY_F2 = &H71             ' Define constants.

   Const SHIFT_MASK = 1

   Const CTRL_MASK = 2

   Const ALT_MASK = 4

   ShiftDown = (Shift And SHIFT_MASK) > 0

   AltDown = (Shift And ALT_MASK) > 0

   CtrlDown = (Shift And CTRL_MASK) > 0

   If KeyCode = KEY_F2 Then        ' Display key combinations.

   If ShiftDown And CtrlDown And AltDown Then

      Txt = "Shift + Ctrl + Alt F2."

   ElseIf ShiftDown And AltDown Then

      Txt = "Shift + Alt F2."

   ElseIf ShiftDown And CtrlDown Then

      Txt = "Shift + Ctrl + F2."

   ElseIf CtrlDown And AltDown Then

      Txt = "Ctl + Alt + F2."

   ElseIf ShiftDown Then

      Txt = "Shift F2."

   ElseIf CtrlDown Then

   Txt = "Ctrl F2."

   ElseIf AltDown Then

      Txt = "Alt F2."

   ElseIf Shift = 0 Then

      Txt = "F2."

   End If

   Text1.Text = "You pressed " & Txt

   End If

End Sub