在VBA中,是否可以同时检测到"Ctrl"键被按下和鼠标滚轮被滚动?
In VBA, is it possible to detect the "Ctrl" key being pressed and the mouse wheel being scrolled at the same time?
我有兴趣使用 Ctrl 键和鼠标滚动事件在 Access VBA 中实现表单缩放功能。如果用户按住 Ctrl 键并向上滚动,表单放大。如果用户按住 Ctrl 键并向下滚动,表单缩小。
我知道如何捕获 Ctrl 按键,我知道如何检测鼠标滚动事件,但我不知道如何同时执行这两项操作。可能吗?
好的,知道了。我在函数定义之外创建了一个布尔变量,以了解何时按下 Ctrl 键。
Private ctrlKeyIsPressed As Boolean
然后我在 KeyDown
事件中将该变量更改为 True
或 False
:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'Detect if the "Ctrl" key was pressed
If (Shift And acCtrlMask) > 0 Then
ctrlKeyIsPressed = True
End If
End Sub
更新:
您还需要使用表单的 KeyUp 事件将 ctrlKeyIsPressed
is pressed 变量设置为 false。
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
ctrlKeyIsPressed = False
End Sub
然后在鼠标滚轮事件中,我使用那个变量:
Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
'If the "Ctrl" key is also being pressed, then zoom the form in or out
If ctrlKeyIsPressed Then
If Count < 0 Then
Debug.Print "Zoom In"
ElseIf Count > 0 Then
Debug.Print "Zoom Out"
End If
End If
End Sub
我有兴趣使用 Ctrl 键和鼠标滚动事件在 Access VBA 中实现表单缩放功能。如果用户按住 Ctrl 键并向上滚动,表单放大。如果用户按住 Ctrl 键并向下滚动,表单缩小。
我知道如何捕获 Ctrl 按键,我知道如何检测鼠标滚动事件,但我不知道如何同时执行这两项操作。可能吗?
好的,知道了。我在函数定义之外创建了一个布尔变量,以了解何时按下 Ctrl 键。
Private ctrlKeyIsPressed As Boolean
然后我在 KeyDown
事件中将该变量更改为 True
或 False
:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'Detect if the "Ctrl" key was pressed
If (Shift And acCtrlMask) > 0 Then
ctrlKeyIsPressed = True
End If
End Sub
更新:
您还需要使用表单的 KeyUp 事件将 ctrlKeyIsPressed
is pressed 变量设置为 false。
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
ctrlKeyIsPressed = False
End Sub
然后在鼠标滚轮事件中,我使用那个变量:
Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
'If the "Ctrl" key is also being pressed, then zoom the form in or out
If ctrlKeyIsPressed Then
If Count < 0 Then
Debug.Print "Zoom In"
ElseIf Count > 0 Then
Debug.Print "Zoom Out"
End If
End If
End Sub