Moving the mouse cursor

How would I go about moving the mouse cursor without moving the mouse; i.e. When a user click on the command button 2 random numbers are generated, X and Y.  Then the mouse cursor would move to the generated random numbers.  therefore the mouse has not been moved but the cursor moves.  I would appreciate some sample code as I am new and the books I have don't help me do this.
master01Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
setiawanConnect With a Mentor Commented:
Hi master01,

Sorry, I just paste the code from rejected answers :)

You are right, It should be "User32", since you use Visual Basic 32-bit

BTW, Thanks for your points


  danny
0
 
AristippusCommented:
Add the following Declare statement to the General Declarations section of Form1 (note that this Declare statement should be typed as one single line of code):

Declare Sub SetCursorPos Lib "User" (ByVal X As Integer, ByVal Y As Integer)



Then within your code:

Sub Command1_Click()
    Dim X As Integer, Y As Integer
    SetCursorPos X, Y
End Sub


0
 
master01Author Commented:
I did exactly what you said, but a compile error meassage appeared and said

Compile error:

Constants, Fixed-Length strings, arrays, user-defined types and Declare statments not allowed as Public members of Object modules.



(General) (Declarations)

Declare Sub SetCursorPos Lib "User" (ByVal X As Integer, ByVal Y As Integer)
---------------------------------------
Private Sub Command1_Click()
    Dim X As Integer, Y As Integer
    SetCursorPos X, Y
End Sub
---------------------------------------
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
setiawanCommented:
Hi Master01,

Try to declare as private

Private Declare Sub SetCursorPos Lib "User" (ByVal X As Integer, ByVal Y As Integer)

Private Sub Command1_Click()
    Dim X As Integer, Y As Integer
    'give X and Y value, before call setcursorpos
    X = 5
    Y = 5
    SetCursorPos X, Y
End Sub


  danny
0
 
master01Author Commented:
Thanks for the help both of you.  Danny, where you put    Lib "User"   I typed it and it gave an error but when I replaced it with "User32" it worked.  Just turn your comment into an answer and you can have the points.

Thanks Lee
0
 
nutwissCommented:
Actually, to be strictly correct - (ie, you're using a 32-bit version of Windows) you should use:

Private Declare Sub SetCursorPos Lib "User32" (ByVal X As Long, ByVal Y As Long)
0
All Courses

From novice to tech pro — start learning today.