应用程序. KeyDown 事件 (Visio)Application.KeyDown event (Visio)

按键盘键时,发生此事件。Occurs when a keyboard key is pressed.

语法Syntax

表达式KeyDown(KeyCodeKeyButtonStateCancelDefault)expression.KeyDown (KeyCode, KeyButtonState, CancelDefault)

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

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
KeyCodeKeyCode 必需Required LongLong 按下的键。The key that was pressed. 可能的值在Keycode 常量中声明。Possible values are declared in Keycode constants.
KeyButtonStateKeyButtonState 必需Required LongLong 事件的 Shift 和 Ctrl 键的状态。The state of the Shift and Ctrl keys for the event. 可以是在**VisKeyButtonFlags** 中声明的值的组合。Can be a combination of the values declared in VisKeyButtonFlags. 例如, 如果_KeyButtonState_返回 12, 则表示用户同时按住 Shift 和 Ctrl。For example, if KeyButtonState returns 12, it indicates that the user held down both Shift and Ctrl.
CancelDefaultCancelDefault 必需Required BooleanBoolean 如果 Microsoft Visio 应处理从此事件接收的消息,则为 False;否则为 TrueFalse if Microsoft Visio should process the message it receives from this event; otherwise, True.

说明Remarks

如果将 CancelDefault 设置为 True,Visio 将不对单击鼠标键时收到的消息进行处理。If you set CancelDefault to True, Visio does not process the message received when the mouse button is clicked.

与其他一些 Visio 事件不同, KeyDown不具有前缀查询, 但它仍是查询事件。Unlike some other Visio events, KeyDown does not have the prefix Query, but it is still a query event. 也就是说, 可以通过将_CancelDefault_设置为true, 或通过返回true来使用**VisEventProc** 方法来处理事件, 从而取消处理由KeyDown发送的邮件。That is, you can cancel processing the message sent by KeyDown, either by setting CancelDefault to True, or, if you are using the VisEventProc method to handle the event, by returning True. 有关详细信息, 请参阅VisEventProc方法和任何查询事件的主题 (例如, QueryCancelSuspend事件)。For more information, see the topics for the VisEventProc method and for any of the query events (for example, the QueryCancelSuspend event).

备注

按加速键组合 (例如, Ctrl + C) 不会触发KeyDown事件。Pressing an accelerator key combination, for example, Ctrl+C, does not fire the KeyDown event.

如果您使用 VBA,则此主题中的语法描述的是一种通用而有效的事件处理方法。If you are using VBA, the syntax in this topic describes a common, efficient way to handle events.

如果要创建自己的Event对象, 请使用**Add** 或**AddAdvise** 方法。If you want to create your own Event objects, use the Add or AddAdvise method.

若要创建可运行加载项的 Event 对象,请使用 Add 方法,因为它适用于 EventList 集合。To create an Event object that runs an add-on, use the Add method as it applies to the EventList collection.

若要创建可接收通知的 Event 对象,请使用 AddAdvise 方法。To create an Event object that receives notification, use the AddAdvise method.

若要查找要创建的事件的事件代码,请参阅事件代码To find an event code for the event that you want to create, see Event codes.

示例Example

此类模块演示如何定义一个名为KeyboardListener的 sync 类, 该类侦听活动窗口中的键盘操作触发的事件。This class module shows how to define a sync class called KeyboardListener that listens for events fired by keyboard actions in the active window. 它通过使用 WithEvents 关键字来声明对象变量 vsoWindowIt declares the object variable vsoWindow by using the WithEvents keyword. 该类模块还包含 KeyDownKeyPressKeyUp 事件的事件处理程序。The class module also contains event handlers for the KeyDown, KeyPress, and KeyUp events.

若要运行此示例, 请在 VBA 项目中插入一个新的类模块, 将其命名为KeyboardListener, 并在模块中插入以下代码。To run this example, insert a new class module in your VBA project, name it KeyboardListener, and insert the following code in the module.

Dim WithEvents vsoWindow As Visio.Window 
 
Private Sub Class_Initialize() 
 
 Set vsoWindow = ActiveWindow 
 
End Sub 
 
Private Sub Class_Terminate() 
 
 Set vsoWindow = Nothing 
 
End Sub 
 
Private Sub vsoWindow_KeyDown(ByVal KeyCode As Long, ByVal KeyButtonState As Long, CancelDefault As Boolean) 
 
 Debug.Print "KeyCode is "; KeyCode 
 Debug.Print "KeyButtonState is" ; KeyButtonState 
 
End Sub 
 
Private Sub vsoWindow_KeyPress(ByVal KeyAscii As Long, CancelDefault As Boolean) 
 
 Debug.Print "KeyAscii value is "; KeyAscii 
 
End Sub 
 
Private Sub vsoWindow_KeyUp(ByVal KeyCode As Long, ByVal KeyButtonState As Long, CancelDefault As Boolean) 
 
 Debug.Print "KeyCode is "; KeyCode 
 Debug.Print "KeyButtonState is" ; KeyButtonState 
 
End Sub

然后, 在**ThisDocument** 项目中插入以下代码。Then, insert the following code in the ThisDocument project.

Dim myKeyboardListener As KeyboardListener 
 
Private Sub Document_DocumentSaved(ByVal doc As IVDocument) 
 
 Set myKeyboardListener = New KeyboardListener 
 
End Sub 
 
Private Sub Document_BeforeDocumentClose(ByVal doc As IVDocument) 
 
 Set myKeyboardListener = Nothing 
 
End Sub

保存文档以初始化该类,然后按任意键触发 KeyDown 事件。Save the document to initialize the class, and then press any key to fire a KeyDown event. 在 "即时" 窗口中, 处理程序将打印在触发事件时所按下的用于触发事件的键的代码以及 Shift 和 Ctrl 键的状态。In the Immediate window, the handler prints the code of the key that was pressed to fire the event and the state of the Shift and Ctrl keys at the time the event fired.

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