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 字符串String 表示要按的键的字符串。A string indicating the key to be pressed.
步骤Procedure 可选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. 如果省略 Procedure 参数,则 Key 恢复为 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" 表示字符 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.

若要指定在按下相应的键 (例如: ENTER 或 TAB) 时不显示的字符, 请使用下表中列出的代码。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}
清除CLEAR {CLEAR}
Delete 或 DelDELETE or DEL {DELETE}{DEL}{DELETE} or {DEL}
向下键DOWN ARROW {DOWN}
结束END {END}
Enter(数字小键盘)ENTER (numeric keypad) {ENTER}
EnterENTER ~(波形符)~ (tilde)
EscESC { ESCAPE}{ESC}{ ESCAPE} or {ESC}
帮助HELP {HELP}
主页HOME {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

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.