CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 1999
    Posts
    1

    Mouse Control help

    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


  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Mouse Control help

    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
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured