Click to See Complete Forum and Search --> : Mouse Control help


MrCriter
November 6th, 1999, 12:27 AM
I am trying to make a program that will take over the control of the mouse pointer, so i can move the mouse cursor around the screen when the user is not touching the mouse. Is this possible to do when my program has focus? how about when it loses focus and another window is selected? all help is greatly appreciated :) thanks

Aaron Young
November 6th, 1999, 01:21 AM
Try something like this:
In a Module, (No Form)..

private Type POINTAPI
x as Long
y as Long
End Type

private Declare Function GetAsyncKeyState Lib "user32" (byval vKey as Long) as Integer
private Declare Function GetCursorPos Lib "user32" (lpPoint as POINTAPI) as Long
private Declare Function SetCursorPos Lib "user32" (byval x as Long, byval y as Long) as Long

Sub Main()
Dim tPos as POINTAPI
Dim tLast as POINTAPI
Dim tPause as Single

'get Initial Mouse Coords
Call GetCursorPos(tLast)
'Wait for Keyboard Buffer to Clear
While GetAsyncKeyState(vbKeyQ)
Wend
'Initialize Idle Timer
tPause = Timer
'Press Q to Quit
While GetAsyncKeyState(vbKeyQ) = 0
'get the Mouse Position
Call GetCursorPos(tPos)
If tPos.x = tLast.x And tPos.y = tLast.y then
'Mouse Hasn't Moved
If (Timer - tPause) > 0.5 then
'No Mouse Activity or 0.5 Seconds
Randomize Timer
'Randomly Move the Mouse
tPos.x = tPos.x + (Rnd * 100) - (Rnd * 100)
tPos.x = tPos.y + (Rnd * 100) - (Rnd * 100)
Call SetCursorPos(tPos.x, tPos.y)
'Reset the Idle Timer
tPause = Timer
End If
else
'The Mouse is being moved..
'Reset the Idle Timer
tPause = Timer
End If
'Remember the Last Coords
tLast.x = tPos.x
tLast.y = tPos.y
'Allow other Apps to Process
DoEvents
Wend
End Sub





Aaron Young
Analyst Programmer
adyoung@win.bright.net
aarony@redwingsoftware.com