Solved

Time interval between lines of code

Posted on 2004-08-13
4
190 Views
Last Modified: 2010-05-02
Hello! I got a code that makes a CTRL + Mouse Click in a specific place of your screen. You define these places to click by typing in a

textbox something like "MouseClick 100, 100" (X and Y coordinates). You can also make a sequence of clicks by typing a command like

that in each line. But there's a problem in that, it's TOO FAST. I need to put intervals between these Mouse Click commands, but I

have no idea how to make it. The code is the following:


Option Explicit

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, _
ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, _
ByVal dwExtraInfo As Long)

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, _
ByVal y As Long) As Long

Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo

As Long)

Private Const VK_CONTROL = &H11
Private Const KEYEVENTF_KEYUP = &H2

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4

Private tmpData As String

Public Sub MouseClick(x As Long, y As Long)
SetCursorPos x, y
keybd_event VK_CONTROL, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN, x, y, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, x, y, 0, 0
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
End Sub

Private Sub Timer1_Timer()

tmpData = Text1.Text
Dim arrTmp() As String, i As Long, arrVal() As String

If tmpData <> "" Then
arrTmp = Split(tmpData, vbNewLine)
For i = 0 To UBound(arrTmp)
If arrTmp(i) <> "" Then
arrVal = Split(arrTmp(i), " ")
If UBound(arrVal) = 2 Then
MouseClick CLng(Left$(arrVal(1), Len(arrVal(1)) - 1)), CLng(arrVal(2))
Else
MsgBox arrTmp(i), vbCritical, "Error"
End If
End If
Next i
End If

End Sub



I need something like the following, to appear in my textbox:

MouseClick 100, 100
Wait 1 second
MouseClick 100, 100
Wait 0,5 second
...

Can someone help me? Please...
0
Comment
Question by:HyMaX_2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11794958
Try Sleep API

'This project needs a button
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Me.Caption = "Your system will sleep 5 sec."
    'Sleep for 5000 milliseconds
    Sleep 5000
    Me.Caption = ""
End Sub
Private Sub Form_Load()
    Me.Caption = ""
    Command1.Caption = "Sleep ..."
End Sub
0
 
LVL 1

Author Comment

by:HyMaX_2003
ID: 11795332
Well but this codes freezes the program completely... I need something like a timer, but I have no idea how to do it using my code...
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 11796342
The number after Wait is in milliseconds so 1000 would be a delay of one second.

Wait 100
MouseClick 100, 100
Wait 1000
MouseClick 150, 200
Wait 500
MouseClick 200, 175
Wait 250
MouseClick 125, 150

The project has only a multiline textbox and a commandbutton.

Option Explicit

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, _
    ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, _
    ByVal dwExtraInfo As Long)

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, _
    ByVal y As Long) As Long

Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const VK_CONTROL = &H11
Private Const KEYEVENTF_KEYUP = &H2
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4

Public Sub MouseClick(x As Long, y As Long)
    SetCursorPos x, y
    keybd_event VK_CONTROL, 0, 0, 0
    mouse_event MOUSEEVENTF_LEFTDOWN, x, y, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, x, y, 0, 0
    keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
End Sub

Private Sub Command1_Click()
    Dim tmpData As String
    Dim cmds As New Collection
    Dim arrTmp() As String, i As Long, arrVal() As String
   
    Command1.Enabled = False
   
    tmpData = LCase(Trim(Text1.Text))
    If tmpData <> "" Then
        arrTmp = Split(tmpData, vbNewLine)
        For i = 0 To UBound(arrTmp)
            If arrTmp(i) <> "" Then
                arrVal = Split(arrTmp(i), " ")
                Select Case arrVal(0)
                    Case "mouseclick"
                        If UBound(arrVal) = 2 Then
                            MouseClick CLng(Left$(arrVal(1), Len(arrVal(1)) - 1)), CLng(arrVal(2))
                        Else
                            MsgBox arrTmp(i), vbCritical, "Incorrect Format"
                        End If
                       
                    Case "wait"
                        If UBound(arrVal) = 1 Then
                            Sleep CLng(arrVal(1))
                        Else
                            MsgBox arrTmp(i), vbCritical, "Incorrect Format"
                        End If
               
                    Case Else
                        MsgBox arrTmp(i), vbCritical, "Unknown Command"
                       
                End Select
            End If
        Next i
    End If

    Command1.Enabled = True
End Sub
0
 
LVL 1

Author Comment

by:HyMaX_2003
ID: 11796575
Idle_Mind my friend.. I can't believe! YOU DID IT AGAIN! THAT'S ABSOLUTELY PERFECT! THANKS SO MUCH! :)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question