_Application.OnKey(String, Object) Method

Definition

Runs a specified procedure when a particular key or key combination is pressed.

public void OnKey (string Key, object Procedure);
Public Sub OnKey (Key As String, Optional Procedure As Object)

Parameters

Key
String

Required String. A string indicating the key to be pressed.

Procedure
Object

Optional Object. A string indicating the name of the procedure to be run. If Procedure is "" (empty text), nothing happens when Key is pressed. This form of OnKey changes the normal result of keystrokes in Microsoft Excel. If Procedure is omitted, Key reverts to its normal result in Microsoft Excel, and any special key assignments made with previous OnKey methods are cleared.

Remarks

The Key argument can specify any single key combined with ALT, CTRL, or SHIFT, or any combination of these keys. Each key is represented by one or more characters, such as "a" for the character a, or "{ENTER}" for the ENTER key.

To specify characters that aren't displayed when you press the corresponding key (ENTER or TAB, for example), use the codes listed in the following table. Each code in the table represents one key on the keyboard.

BACKSPACE{BACKSPACE} or {BS}
BREAK{BREAK}
CAPS LOCK{CAPSLOCK}
CLEAR{CLEAR}
DELETE or DEL{DELETE} or {DEL}
DOWN ARROW{DOWN}
END{END}
ENTER~ (tilde)
ENTER (numeric keypad){ENTER}
ESC{ESCAPE} or {ESC}
F1 through F15{F1} through {F15}
HELP{HELP}
HOME{HOME}
INS{INSERT}
LEFT ARROW{LEFT}
NUM LOCK{NUMLOCK}
PAGE DOWN{PGDN}
PAGE UP{PGUP}
RETURN{RETURN}
RIGHT ARROW{RIGHT}
SCROLL LOCK{SCROLLLOCK}
TAB{TAB}
UP ARROW{UP}

You can also specify keys combined with SHIFT and/or CTRL and/or ALT. To specify a key combined with another key or keys, use the following table.

SHIFT+ (plus sign)
CTRL^ (caret)
ALT% (percent sign)

To assign a procedure to one of the special characters (+, ^, %, and so on), enclose the character in braces.

Applies to