Results 1 to 2 of 2

Thread: Mouse Control

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 1999
    Posts
    13

    Post

    I need to take control of the mouse (on a system wide level) for an application to help people with special disabilities. I have tried Desaware's Spyworks control, but the system wide ocx that allows you to trap mouse events, doesn't allow you to actually change these windows system messages that are sent to the graphical mouse pointer. Is there a way to take control of the mouse from Windows?

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    You can use the Mouse_Event API to Simulate Mouse Operations, here's a quick example which allows you to control to Mouse Pointer using the Cursor Keys, and the Zero Key on the NumericPad as the Left Mouse Button.

    In a Module, (No Forms Needed For this Project)..
    Code:
    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 GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Private Const MOUSEEVENTF_LEFTDOWN = &H2
    Private Const MOUSEEVENTF_LEFTUP = &H4
    Private Const MOUSEEVENTF_MOVE = &H1
    
    Private Const MOUSESPEED = 2
    
    Sub Main()
        Dim bClick As Boolean
        Dim x As Integer
        Dim y As Integer
        
        'Wait for a Clear Keyboard Buffer
        While GetAsyncKeyState(1)
            DoEvents
        Wend
        Do
            x = 0
            y = 0
            If GetAsyncKeyState(vbKeyLeft) Then
                x = -MOUSESPEED
            ElseIf GetAsyncKeyState(vbKeyRight) Then
                x = MOUSESPEED
            End If
            If GetAsyncKeyState(vbKeyUp) Then
                y = -MOUSESPEED
            ElseIf GetAsyncKeyState(vbKeyDown) Then
                y = MOUSESPEED
            End If
            If GetAsyncKeyState(vbKeyNumpad0) <> 0 Then
                If Not bClick Then
                    'Simulate Left Mouse Button Down
                    mouse_event MOUSEEVENTF_MOVE Or MOUSEEVENTF_LEFTDOWN, x, y, 0, 0
                Else
                    'Simulate Mouse Movement with Left Mouse Button Down, (Drag).
                    mouse_event MOUSEEVENTF_MOVE, x, y, 0, 0
                End If
                bClick = True
            Else
                If bClick Then
                    'Simulate Left Mouse Button Up
                    mouse_event MOUSEEVENTF_MOVE Or MOUSEEVENTF_LEFTUP, x, y, 0, 0
                ElseIf x <> 0 Or y <> 0 Then
                    'Simulate Mouse Movement
                    mouse_event MOUSEEVENTF_MOVE, x, y, 0, 0
                End If
                bClick = False
            End If
            DoEvents
            Sleep 1     'Pause to allow the cursor action to be seen.
        Loop Until GetAsyncKeyState(vbKeyEscape) 'Press ESC to Quit
    End Sub
    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    adyoung@win.bright.net

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