KeyButtonState Property [Visio 2003 SDK Documentation]

Returns the state of mouse buttons and the SHIFT and CTRL keys associated with a keyboard or mouse event.

longRet = object.KeyButtonState

longRet    Long. The state of mouse buttons and SHIFT and CTRL keys for the event. See Remarks for possible values.

object    Required. An expression that returns a KeyboardEvent or MouseEvent object.

Version added

2003

Remarks

Possible values for longRet can be a combination of any of the values shown in the following table, which are declared in VisKeyButtonFlags in the Visio type library. For example, if KeyButtonState returns 9, it indicates that the user clicked the left mouse button while pressing CTRL.

Constant Value

visKeyControl

8

visKeyShift

4

visMouseLeft

1

visMouseMiddle

16

visMouseRight

2

Example

As it applies to the KeyboardEvent object.

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.

To run this example, insert a new class module in your Microsoft Visual Basic for Applications (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

Save the document to initialize the class, and then press any key to fire a KeyDown event. 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.

As it applies to the MouseEvent object.

This class module shows how to define a sink class called MouseListener that listens for events fired by mouse actions in the active window. It declares the object variable vsoWindow by using the WithEvents keyword. The class module also contains an event handler for the MouseDown event that prints to the Immediate window the state of the mouse buttons and CTRL and SHIFT keys when the event fired.

To run this example, insert a new class module in your VBA project, name it MouseListener, 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_MouseDown(ByVal Button As Long, ByVal KeyButtonState As Long, ByVal x As Double, ByVal y As Double, CancelDefault As Boolean)
    
    Debug.Print "KeyButtonState is"; KeyButtonState
        
End Sub

Then, insert the following code in the ThisDocument project.

Dim myMouseListener As MouseListener

Private Sub Document_DocumentSaved(ByVal doc As IVDocument)

    Set myMouseListener = New MouseListener

End Sub

Private Sub Document_BeforeDocumentClose(ByVal doc As IVDocument)

    Set myMouseListener = Nothing
    
End Sub

Save the document to initialize the class, and then click anywhere in the active window (optionally, while pressing SHIFT and/or CTRL) to fire a MouseDown event. In the Immediate window, the handler prints the name of the mouse button that was clicked to fire the event. If you pressed either or both of the keys, the name of the key or keys you pressed will print as well.

Applies to | KeyboardEvent object | MouseEvent object

See Also | KeyAscii property | KeyCode property | KeyDown event | KeyPress event | KeyUp event