Jump to content
Sign in to follow this  
Xibalba

Getting Mouse Button Statuses?

Recommended Posts

Xibalba

$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.

Share this post


Link to post
Share on other sites
Spiff59

#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

Share this post


Link to post
Share on other sites
Xibalba

Thanks, a lot.

Share this post


Link to post
Share on other sites
Xibalba

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!

Share this post


Link to post
Share on other sites
wakillon

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.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
Xibalba

Ye. okay. I should have clarified..

If I want to get the position from WinAPI like the above events (write an own GetMousePos function)?

Share this post


Link to post
Share on other sites
seandisanti

Ye. okay. I should have clarified..

If I want to get the position from WinAPI like the above events (write an own GetMousePos function)?

Where do you think MouseGetPos() gets your mouse position from?

Share this post


Link to post
Share on other sites
Xibalba

Where do you think MouseGetPos() gets your mouse position from?

I don't know. That's why i asked for some someone who knows, one who can post an URL where I can read about this. Or even better, explain.

Share this post


Link to post
Share on other sites
seandisanti

I don't know. That's why i asked for some someone who knows, one who can post an URL where I can read about this. Or even better, explain.

Personally i say why reinvent the wheel when MouseGetPos() handles the api interaction for you, BUT It's been done

Share this post


Link to post
Share on other sites
Kalin

Personally i say why reinvent the wheel when MouseGetPos() handles the api interaction for you, BUT It's been done

you have to use an array in the MouseGetPos()

$pos = MouseGetPos()

so something like this.

$pos[0]

Share this post


Link to post
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
Sign in to follow this  

×