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

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

语法Syntax

表达式OnKey(KeyProcedure)expression.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. 如果 Procedure 为空文本 (""),则按 Key 时不发生任何操作。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 and/or Command. 若要指定与其他键组合使用的键,可使用下表。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)
命令Command *号仅适用于 Mac;只能在 Excel 2011 for Mac 和更高版本中使用。* (asterisk) Only applies to Mac; may only work on Excel 2011 for Mac and not later versions.

若为特定字符指定处理过程(如 +、^、% 等等),可将此字符用圆括号括起。To assign a procedure to one of the special characters (+, ^, %, and so on), enclose the character in braces. 有关详细信息,请参阅示例。For details, see the example.

备注

目前, 在 Office VBA 的最新版本中无法检测命令项。There is no way to currently detect the Command key in recent versions of Office VBA. Microsoft 意识到这一点, 正在寻找它。Microsoft is aware of this and is looking into it.

示例Example

此示例将 Specialprintproc 分配给键序列 Ctrl + 加号, 并将 Insertproc 分配给键序列 Shift + Ctrl + 向右键。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}", ""

支持和反馈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.