Results 1 to 5 of 5

Thread: Is there a API to capture mouse movement ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Location
    Malaysia
    Posts
    69
    What I want is the trigger an event whenever the user move or click with the mouse no matter where the pointer is something like to halt the screensaver back to desktop.

    Thanks.

  2. #2
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Here ya go.

    This should help you.

    Code:
    Option Explicit
    
    Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
    
    Private Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
    Private Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long
    Private Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long
    
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Const WM_KEYDOWN = &H100
    Const WM_KEYFIRST = &H100
    Const WM_KEYLAST = &H108
    Const WM_KEYUP = &H101
    Const WM_LBUTTONDBLCLK = &H203
    Const WM_LBUTTONDOWN = &H201
    Const WM_LBUTTONUP = &H202
    Const WM_RBUTTONDBLCLK = &H206
    Const WM_RBUTTONDOWN = &H204
    Const WM_RBUTTONUP = &H205
    Const WM_MBUTTONDBLCLK = &H209
    Const WM_MBUTTONDOWN = &H207
    Const WM_MBUTTONUP = &H208
    Const WM_MOUSEMOVE = &H200
    
    Private Type POINTAPI
      X As Long
      Y As Long
    End Type
    
    Private Type Msg
      hWnd As Long
      message As Long
      wParam As Long
      lParam As Long
      time As Long
      pt As POINTAPI
    End Type
        
    Dim lEventHandle As Long
    Dim aMsg As Msg
    
    Sub Start()
      lEventHandle = SetTimer(0, 0, 10, AddressOf MyTimer)
    End Sub
    
    Sub MyTimer(ByVal hWnd As Long, ByVal lngMsg As Long, ByVal lngID As Long, ByVal lngTime As Long)
      KillTimer 0, lEventHandle  'Kill timer
      Do While GetMessage(aMsg, 0, 0, 0) 'Gets the message
        TranslateMessage aMsg 'translates it to something
        DispatchMessage aMsg  'sends the message on to the system
        Select Case aMsg.message
          Case 275, 280:
          Case WM_KEYFIRST To WM_KEYLAST:
          Case WM_MOUSEMOVE:
          Case WM_LBUTTONDOWN:
            MsgBox "LeftMouseDown"
          Case WM_RBUTTONDOWN:
            MsgBox "RightMouseDown"
          Case WM_LBUTTONUP:
            MsgBox "LeftMouseUp"
          Case WM_RBUTTONUP:
            MsgBox "RightMouseUp"
          Case Else:
        End Select
        Sleep 0&
      Loop 'Loops until program is closed ( i think )
    End Sub

  3. #3
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175

    Re: Here ya go.

    Thomas, I tried your code and it works fine but can you please tell me how can I use it so it can detect mouse no mather where it is (On external applications?)
    Thanks
    Mass

  4. #4
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    Where the Case LBUTTONDOWN you should be able to put your code there. I think it will trigger within your program as well as in external programs. Just do something like this:

    Code:
    Case WM_MOUSEMOVE:
      Msgbox "You just moved your mouse."
      Exit Loop 'to exit the loop
    Case WM_LBUTTONDOWN:
      Msgbox "You pressed the left mousebutton"
      Exit Loop 'to exit the loop

  5. #5
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175
    I am afraid it dos not work on external applications.
    Mass

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