Application.OnKey 方法 (Excel)Application.OnKey method (Excel)

当按特定键或特定的组合键时运行指定的过程。Runs a specified procedure when a particular key or key combination is pressed.

语法Syntax

expressionexpression. OnKey( _Key_ , _Procedure_ )

expression:表示 Application 对象的变量。expression A variable that represents an Application object.

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
KeyKey 必需Required StringString 表示要按的键的字符串。A string indicating the key to be pressed.
ProcedureProcedure 可选Optional VariantVariant 指示要运行的过程名称的字符串。A string indicating the name of the procedure to be run. 如果_过程_""(空文本),没有任何反应_键_被按下时。If Procedure is "" (empty text), nothing happens when Key is pressed. 这种形式的OnKey更改普通键击,Microsoft Excel 中的结果。This form of OnKey changes the normal result of keystrokes in Microsoft Excel. 如果省略_过程_,则_键_恢复为在 Microsoft Excel 中,其正常结果,并清除以前OnKey方法所做任何特殊按键分配方案。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

_Key_参数可以指定与 ALT、 CTRL 或 SHIFT 或这些键的任意组合结合任意单个键。The Key argument can specify any single key combined with ALT, CTRL, or SHIFT, or any combination of these keys. 每个键由一个或多个字符,如"a"的字符,或"{ENTER}"ENTER 键。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 (for example: ENTER or TAB), use the codes listed in the following table. 每个表中的代码表示键盘上的一个键。Each code in the table represents one key on the keyboard.

按键Key 代码Code
BACKSPACEBACKSPACE {BACKSPACE}{BS}{BACKSPACE} or {BS}
BreakBREAK {BREAK}
Caps LockCAPS LOCK {CAPSLOCK}
ClearCLEAR {CLEAR}
Delete 或 DelDELETE or DEL {DELETE}{DEL}{DELETE} or {DEL}
向下键DOWN ARROW {DOWN}
EndEND {END}
Enter(数字小键盘)ENTER (numeric keypad) {ENTER}
EnterENTER ~(波形符)~ (tilde)
EscESC {ESCAPE}{ESC}{ ESCAPE} or {ESC}
HelpHELP {HELP}
HomeHOME {HOME}
InsINS {INSERT}
向左键LEFT ARROW {LEFT}
Num LockNUM LOCK {NUMLOCK}
PageDownPAGE DOWN {PGDN}
PageUpPAGE UP {PGUP}
ReturnRETURN {RETURN}
向右键RIGHT ARROW {RIGHT}
Scroll LockSCROLL LOCK {SCROLLLOCK}
TabTAB {TAB}
向上键UP ARROW {UP}
F1 到 F15F1 through F15 {F1}{F15}{F1} through {F15}

您还可以指定与 SHIFT 和/或 CTRL 和/或 ALT 组合的键。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.

要组合的键To combine keys with 在键代码之前添加Precede the key code by
ShiftSHIFT +(加号)+ (plus sign)
CtrlCTRL ^(插入符号)^ (caret)
AltALT %(百分号)% (percent sign)

要分配给某个特殊字符的过程 (+、 ^,%,依此类推) 的字符括在括号中。To assign a procedure to one of the special characters (+, ^, %, and so on), enclose the character in braces. 有关详细信息,请参阅示例。For details, see the example.

示例Example

本示例为键序列 Ctrl+加号分配“InsertProc”过程,并为键序列 Shift+Ctrl+向右键分配“SpecialPrintProc”过程。This example assigns "InsertProc" to the key sequence CTRL+PLUS SIGN and assigns "SpecialPrintProc" to the key sequence SHIFT+CTRL+RIGHT ARROW.

Application.OnKey "^{+}", "InsertProc" 
Application.OnKey "+^{RIGHT}", "SpecialPrintProc"

本示例将 Shift+Ctrl+向右键重新设为正常的含义。This example returns SHIFT+CTRL+RIGHT ARROW to its normal meaning.

Application.OnKey "+^{RIGHT}"

本示例将 Shift+Ctrl+向右键键序列设为不发生任何操作。This example disables the SHIFT+CTRL+RIGHT ARROW key sequence.

Application.OnKey "+^{RIGHT}", ""

另请参阅See also

Application 对象Application Object