Application.KeyDown 事件 (Visio)Application.KeyDown Event (Visio)

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

语法Syntax

Private Sub_表达式__ 'KeyDown (ByVal KeyCode As LongByVal KeyButtonState As LongByVal CancelDefault As BooleanPrivate Sub expression _'KeyDown'(ByVal KeyCode As Long , ByVal KeyButtonState As Long , ByVal CancelDefault As Boolean)

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

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
KeyCodeKeyCode 必需Required LongLong 已按下的键。The key that was pressed. 有关可能值,请参阅"说明"。See Remarks for possible values.
KeyButtonStateKeyButtonState 必需Required LongLong SHIFT 和 CTRL 键的事件的状态。The state of the SHIFT and CTRL keys for the event. 有关可能值,请参阅"说明"。See Remarks for possible values.
CancelDefaultCancelDefault 必需Required BooleanBoolean 如果 Microsoft Visio 应处理从此事件接收的消息,则为 *False*;否则为 TrueFalse if Microsoft Visio should process the message it receives from this event; otherwise, True.

备注Remarks

_KeyCode_的可能值 for Applications (VBA) 库中的KeyCodeConstants的 Microsoft Visual Basic 中声明。Possible values for KeyCode are declared in KeyCodeConstants in the Microsoft Visual Basic for Applications (VBA) library.

_KeyButtonState_的可能值可以是下表中中, 显示的值在 Visio 类型库中的VisKeyButtonFlags中声明的组合。Possible values for KeyButtonState can be a combination of the values shown in the following table, which are declared in VisKeyButtonFlags in the Visio type library. 例如,如果_KeyButtonState_返回 12,它指示用户按下 SHIFT 和 CTRL。For example, if KeyButtonState returns 12, it indicates that the user held down both SHIFT and CTRL.

常量Constant Value
* * visKeyControl * ***visKeyControl ** 88
* * visKeyShift * ***visKeyShift ** 44
* * visMouseLeft * ***visMouseLeft ** 11
* * visMouseMiddle * ***visMouseMiddle ** 1616
* * visMouseRight * ***visMouseRight ** 22

如果将_CancelDefault_设置为True ,Visio 不处理单击鼠标按钮时所接收的消息。If you set CancelDefault to True , Visio does not process the message received when the mouse button is clicked.

与其他一些 Visio 事件不同KeyDown不具有前缀"Query,"但它仍然是 query 事件。Unlike some other Visio events, KeyDown does not have the prefix "Query," but it is still a query event. 也就是说,您可以取消处理KeyDown ,可通过将_CancelDefault_设置为True ,发送的消息或者,如果您使用的VisEventProc方法来处理该事件,通过返回TrueThat 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) in this Automation Reference.

注释 按下加速键组合,例如,CTRL + C,不会触发KeyDown事件。Note Pressing an accelererator 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对象,使用添加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 you want to create, see Event codes.

示例Example

此类模块演示如何定义名为 KeyboardListener侦听的活动窗口中的键盘操作而触发的事件接收器类。This class module shows how to define a sink class called KeyboardListener that listens for events fired by keyboard actions in the active window. 它使用WithEvents关键字声明对象变量_vsoWindow_ 。It 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.