保持按键事件,vba

时间:2017-03-10 10:41:20

标签: vba powerpoint-vba

在vba中按下键(鼠标键)时有没有办法继续做某事?

%' As if there were for example a MouseLeftClicked vba function
Do While MouseLeftClicked 
Sheet1.Shapes("Picture 1").Rotation = Sheet1.Shapes("Picture 1").Rotation + 15
DoEvents

1 个答案:

答案 0 :(得分:3)

简短的回答是肯定的,但您需要在UserForm中捕获鼠标点击,如下所示:

在您的用户窗体

Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    booKeyPressed = True
    RotateIt

End Sub

Private Sub UserForm_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    booKeyPressed = False

End Sub

在模块中

Public booKeyPressed As Boolean

Function RotateIt()

    If Not booKeyPressed Then Exit Function
    Application.OnTime Now + TimeValue("00:00:01"), "RotateIt"
    Sheet1.Shapes("Picture 1").Rotation = Sheet1.Shapes("Picture 1").Rotation + 15
    DoEvents

End Function

只要在Userform上单击鼠标,上面的代码就会每1秒旋转一次图片。您可以将其更改为特定控件,也可以根据需要更改时间间隔。

我无法在工作表上捕获MouseClick事件,但如果更适合您的需求,您可以将其链接到工作表事件。

您的问题提到了按键,但您的示例代码提到了鼠标。您可以轻松地适应您需要的任务,因为它们都可用作Userform事件