Microsoft Office Tutorials and References
In Depth Information
16.1.4 Event-Related Members
16.1.4 Event-Related Members
It is possible to assign macros to certain events. (These are special events—not the events that we
discussed in Chapter 11 .) For instance, we can assign a macro to play whenever a particular key is
pressed. This is done by invoking the OnKey method for the Application object. Let us describe
two of the more useful events that can be assigned a macro.
16.1.4.1 OnKey method
The syntax for the OnKey method is:
Application.OnKey( Key , Procedure )
where Key is the key or key combination (written as a string) that will execute the macro and
Procedure is the name of that macro.
Note that we can alter the normal behavior of Excel by assigning a key combination to the Key
parameter that has a normal Excel response (such as Ctrl-S for save). If we assign an empty string
to the Procedure parameter, then Excel will omit its normal response (so nothing will happen).
If we omit the Procedure parameter, then Excel will return the key combination to its normal
function.
To illustrate, the following code will disable the Ctrl-o key combination, which normally displays
the Open dialog box:
Application.OnKey "^o",""
The following code returns the Ctrl-o key combination to its normal Excel function:
Application.OnKey "^o"
The Key argument can specify a single key or any key combined with one or more of Alt, Ctrl, or
Shift. Normal alphanumeric keys are denoted by themselves, as in "a," "A," "1." Table 16-3 s hows
how to enter special keys. For instance, the F2 key is denoted by "{F2}", and the Enter key is
denoted either by "{ENTER}" or "~".
Table 16-3. Special Keys for the Key Parameter
Key
Code
Backspace
{BACKSPACE} or {BS}
Break
{BREAK}
Caps Lock
{CAPSLOCK}
Clear
{CLEAR}
Delete or Del
{DELETE} or {DEL}
Down Arrow
{DOWN}
End
{END}
Enter (numeric keypad)
{ENTER}
Enter
~ (tilde)
Esc
{ESCAPE} or {ESC}
Help
{HELP}
Home
{HOME}
Ins
{INSERT}
 
Search JabSto ::




Custom Search