Jump to content

Getting Mouse Button Statuses?


Recommended Posts

$pos = MouseGetPos() retrieves the current position of the mouse cursor.

But what about the buttons? Is there a way of checking whether the left mouse button is down (clicked)?

Do I need some DLL or such to accomplish this?

I'm a bit surprised that there is no function like MouseGetState("left") which would return information about the state.

Why? I want to check how fast my manual mouse clicks are performed - i.e how long my Mouse-Down times are.

Link to comment
Share on other sites

#include <WindowsConstants.au3>
#Include <WinAPI.au3>

Global $mouseevent, $timer

$Gui = GuiCreate("", 300, 200)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_SETCURSOR, 'WM_SETCURSOR')
GUIRegisterMsg($WM_MOUSEWHEEL, "WM_MOUSEWHEEL")

While 1
    $Msg = GUIGetMsg()
    If $Msg = -3 Then ExitLoop ; $GUI_EVENT_CLOSE
    If $mouseevent Then
        Beep(800,50)
        tooltip("Mouse event: " & $mouseevent)
        $timer = TimerInit()
        $mouseevent = ""
    EndIf
    If $timer And TimerDiff($timer) > 500 Then
        tooltip("")
        $timer = 0
    EndIf
WEnd

Func WM_SETCURSOR($hWnd, $iMsg, $iWParam, $iLParam)
    $iLParamhigh = BitShift($iLParam, 16) ;HiWord
    $iLParamlow  = BitAnd($iLParam, 0x0000FFFF) ;LoWord

    If $hWnd = $Gui And $iLParamlow = 1 And $iLParamhigh > 512 Then
        Switch $iLParamhigh
            Case $WM_LBUTTONDOWN
                $mouseevent = "LEFT-DOWN"
            Case $WM_LBUTTONUP
                $mouseevent = "LEFT-UP"
            Case $WM_RBUTTONDOWN
                $mouseevent = "RIGHT-DOWN"
            Case $WM_RBUTTONUP
                $mouseevent = "RIGHT-UP"
            Case $WM_MBUTTONDOWN
                $mouseevent = "MIDDLE-DOWN"
            Case $WM_MBUTTONUP
                $mouseevent = "MIDDLE-UP"
        EndSwitch
    EndIf
EndFunc

Func WM_MOUSEWHEEL($hWnd, $iMsg, $iwParam, $ilParam)
    If _WinAPI_HiWord($iwParam) > 0 Then
        $mouseevent = "SCROLL-UP"
    Else
        $mouseevent = "SCROLL-DOWN"
    EndIf
    Return 0
EndFunc

Edit: added detection of mouse scroll wheel.

PS - You could measure the delay between the down event, and the up event, and determine your "downtime".

Edited by Spiff59
Link to comment
Share on other sites

  • 1 month later...

After testing this script a bit, I would also like to get info about current mouse position (x y).

I'm a little lost when it comes to using the $iLParamhigh and $iLParamlow vars in example above.. perhaps someone can give a hint?

Where can I read more about how to use WinAPI mouse functions? Best would be basic example code.

Thanks a lot!

Link to comment
Share on other sites

After testing this script a bit, I would also like to get info about current mouse position (x y).

I'm a little lost when it comes to using the $iLParamhigh and $iLParamlow vars in example above.. perhaps someone can give a hint?

Where can I read more about how to use WinAPI mouse functions? Best would be basic example code.

Thanks a lot!

For get info about current mouse position (x y) see MouseGetPos function ! Posted Image

AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...