Key Down

The KeyDown event occurs when there are no controls on the form or if the form's KeyPreview property is set to Yes. A form's KeyPreview property determines whether form keyboard events are invoked before control keyboard events.

The KeyDown event takes the following two arguments: KeyCode — Determines which key was pressed. To specify key codes, use members of the KeyCodeConstants class in the VBA Object Library in the Object Browser.

Shift — Determines if the Shift, Ctrl, or Alt key was pressed. Use acShiftMask(1) to test for the Shift key, acCtrlMask(2) to test for the Ctrl key, and acAltMask(4) to test for the Alt key. You can test for any

Part IV

combination of buttons. For example, to specify that Ctrl and Alt were pressed, use the value of 6 (2+4) as the Shift argument.

The event procedure in Hands-On 24-14 displays a message when you press one of the following keys: F1, Home, Tab, Shift, Ctrl, or Alt.

0 0

Post a comment