Table of contents
TOC
折叠目录
展开目录

WebBrowserControl.KeyDown 事件 (访问)WebBrowserControl.KeyDown Event (Access)

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

当用户按下某个键时窗体或控件具有焦点时,将发生KeyDown事件。如果您在窗体或控件使用 SendKeys 操作的宏或 Visual Basic 中的SendKeys语句发送击键,也会发生此事件。The KeyDown event occurs when the user presses a key while a form or control has the focus. This event also occurs if you send a keystroke to a form or control by using the SendKeys action in a macro or the SendKeys statement in Visual Basic.

语法Syntax

表达式KeyDown(* 键控代码班次)expression. **KeyDown( ** KeyCode, ** Shift* )

表达式_一个表示WebBrowserControl对象的变量。_expression A variable that represents a WebBrowserControl object.

参数Parameters

名称Name必需/可选Required/Optional数据类型Data Type说明Description
键控代码KeyCode必需RequiredIntegerInteger
ShiftShift必需RequiredIntegerInteger

返回值Return Value

nothingnothing

注解Remarks

若要在这些事件发生时运行宏或事件过程,请将OnKeyDown属性设置为宏的名称或 [事件过程]。To run a macro or event procedure when these events occur, set the OnKeyDown property to the name of the macro or to [Event Procedure].

对于这两个事件,获得焦点的对象将接收到所有的键击事件。窗体只有在它没有控件或它所有可见的控件都失效时才能获得焦点。For both events, the object with the focus receives all keystrokes. A form can have the focus only if it has no controls or all its visible controls are disabled.

窗体也能接收所有的键盘事件,甚至包括那些对控件发生,如果将窗体的KeyPreview属性设置为是。通过设置此属性值,所有键盘事件首先都发生该窗体,然后该控件具有焦点。您可以对无论哪个控件获得焦点,窗体中按下的特定键作出响应。例如,您可能希望组合键 CTRL + X 总在窗体上执行相同的操作。A form will also receive all keyboard events, even those that occur for controls, if you set the KeyPreview property of the form to Yes. With this property setting, all keyboard events occur first for the form, and then for the control that has the focus. You can respond to specific keys pressed in the form, regardless of which control has the focus. For example, you may want the key combination CTRL+X to always perform the same action on a form.

如果按下并按住某个键, KeyDownKeyPress事件交替反复 ( KeyDown按键KeyDown按键,等等) 直到松开键,则KeyUp事件发生。If you press and hold down a key, the KeyDown and KeyPress events alternate repeatedly ( KeyDown, KeyPress, KeyDown, KeyPress, and so on) until you release the key, then the KeyUp event occurs.

尽管在大部分键被按下时,将发生KeyDown事件,但它通常用于识别或区别:Although the KeyDown event occurs when most keys are pressed, it is typically used to recognize or distinguish between:

  • 扩展字符键,如功能键。Extended character keys, such as function keys.

  • 定位键,如 Home、End、Page Up、Page Down、向上键、向下键、向右键、向左键及 Tab。Navigation keys, such as HOME, END, PAGE UP, PAGE DOWN, UP ARROW, DOWN ARROW, RIGHT ARROW, LEFT ARROW, and TAB.

  • 键的组合和标准的键盘更改键(Shift、Ctrl 或 Alt 键)。Combinations of keys and standard keyboard modifiers (SHIFT, CTRL, or ALT keys).

  • 数字小键盘和键盘数字键。The numeric keypad and keyboard number keys.

当您按下时,就不会发生KeyDown事件:The KeyDown event does not occur when you press:

  • 如果窗体的命令按钮的默认值属性设置为是 ENTER 键。The ENTER key if the form has a command button for which the Default property is set to Yes.

  • 如果窗体的命令按钮的Cancel属性设置为是 ESC 键。The ESC key if the form has a command button for which the Cancel property is set to Yes.

当您按下或发送一个 ANSI 键,将发生KeyDown事件。任何事件之后, KeyUp事件发生按下或发送键引起控件。如果键击导致焦点从一个控件移动到另一个控件,将发生KeyDown事件有关的第一个控件的按键KeyUp事件发生的第二个控件。The KeyDown event occurs when you press or send an ANSI key. The KeyUp event occurs after any event for a control caused by pressing or sending the key. If a keystroke causes the focus to move from one control to another control, the KeyDown event occurs for the first control, while the KeyPress and KeyUp events occur for the second control.

若要找出对应于按下的键的 ANSI 字符,使用KeyPress事件。To find out the ANSI character corresponding to the key pressed, use the KeyPress event.

如果模式对话框显示由于按下或发送键,则KeyDownKeyPress事件发生,但不会发生KeyUp事件。If a modal dialog box is displayed as a result of pressing or sending a key, the KeyDown and KeyPress events occur, but the KeyUp event doesn't occur.

示例Example

下面的示例确定是否已经按下 Shift、Ctrl 或 Alt 键。The following example determines whether you have pressed the SHIFT, CTRL, or ALT key.

若要尝试运行该示例,请将以下事件过程添加到含有名为 KeyHandler 的文本框的窗体中。To try the example, add the following event procedure to a form containing a text box named KeyHandler.

Private Sub KeyHandler_KeyDown(KeyCode As Integer, _ 
 Shift As Integer) 
 Dim intShiftDown As Integer, intAltDown As Integer 
 Dim intCtrlDown As Integer 

 ' Use bit masks to determine which key was pressed. 
 intShiftDown = (Shift And acShiftMask) > 0 
 intAltDown = (Shift And acAltMask) > 0 
 intCtrlDown = (Shift And acCtrlMask) > 0 
 ' Display message telling user which key was pressed. 
 If intShiftDown Then MsgBox "You pressed the SHIFT key." 
 If intAltDown Then MsgBox "You pressed the ALT key." 
 If intCtrlDown Then MsgBox "You pressed the CTRL key." 
End Sub

另请参阅See also

概念Concepts

WebBrowserControl 对象WebBrowserControl Object

© 2018 Microsoft