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

DrawingControl.KeyPress 事件 (Visio)DrawingControl.KeyPress Event (Visio)

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

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

语法Syntax

专用子_表达式__按键ByVal KeyAscii 作为长ByVal CancelDefault 作为布尔值Private Sub expressionKeyPress( **_ByVal KeyAscii As Long** , ByVal CancelDefault As Boolean )

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

参数Parameters

名称Name必需/可选Required/Optional数据类型Data Type说明Description
KeyAsciiKeyAscii必需RequiredLongLong按下的键的 ASCII 值。有关可能值,请参阅“说明”。The ASCII value of the key that was pressed. See Remarks for possible values.
CancelDefaultCancelDefault必需RequiredBooleanBoolean如果 Microsoft Visio 应处理该消息接收来自此事件;否则为则返回 TrueFalse if Microsoft Visio should process the message it receives from this event; otherwise, True .

注解Remarks

对于_KeyAscii_的可能值是 ASCII 码。若要查看这些代码的列表,请搜索 MSDN 上的"ASCII 字符代码"。Possible values for KeyAscii are the ASCII codes. To see a list of these codes, search for "ASCII character codes" on MSDN.

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

与某些其他 Visio 事件,不同的按键没有前缀"查询,"但仍不失为查询事件。也就是说,您可以取消处理按键,可以通过将_CancelDefault_设置为True ,则发送的消息或者,如果您正在使用VisEventProc方法来处理该事件,返回True 。有关详细信息,请参阅此自动化参考中的VisEventProc方法及其任何查询事件 (例如, QueryCancelSuspend事件) 的主题。Unlike some other Visio events, KeyPress does not have the prefix "Query," but it is still a query event. That is, you can cancel processing the message sent by KeyPress , either by setting CancelDefault to True , or, if you are using the VisEventProc method to handle the event, by returning True . 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.

请注意 按 accelererator 组合键,例如,CTRL + C 时,不会触发KeyPress事件。Note Pressing an accelererator key combination, for example, CTRL + C, does not fire the KeyPress event.

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

如果您想要创建您自己的事件对象,使用添加AddAdvise方法。若要创建一个事件对象,运行加载项,请使用Add方法,应用到事件列表集合。若要创建一个事件对象,该对象接收通知,请使用AddAdvise方法。若要查找您想要创建的事件的事件代码,请参阅事件代码If you want to create your own Event objects, use the Add or AddAdvise method. To create an Event object that runs an add-on, use the Add method as it applies to the EventList collection. To create an Event object that receives notification, use the AddAdvise method. To find an event code for the event you want to create, seeEvent codes.

示例Example

此类模块显示了如何定义一个名为KeyboardListener侦听事件触发的活动窗口中的键盘操作的接收器类。它通过使用WithEvents关键字声明对象变量_vsoWindow_ 。类模块还包含KeyDown按键KeyUp事件的事件处理程序。This class module shows how to define a sink class called KeyboardListener that listens for events fired by keyboard actions in the active window. It declares the object variable vsoWindow by using the WithEvents keyword. 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

然后,本文档项目中插入以下代码。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

保存文档以初始化类,,然后按任意键可激发KeyPress事件。在即时窗口中,处理程序打印为触发事件而按下的键的 ASCII 码。Save the document to initialize the class, and then press any key to fire a KeyPress event. In the Immediate window, the handler prints the ASCII code of the key that was pressed to fire the event.

© 2018 Microsoft