Jump to content

Trigger event on mouse position in x,y


Recommended Posts

Try this

#include <WinAPIGdi.au3>
#include <Misc.au3>

While 1
    If _IsPressed("01") Then
        Local $pos = MouseGetPos()
        If ($pos[0] = 900 And $pos[1] = 800) Then
            ConsoleWrite("Clicked at 900, 800" & @LF)
        EndIf
        ; Better to check if in an area, not at exact point
        If (_WinAPI_PtInRectEx($pos[0], $pos[1], 800, 700, 1000, 900)) Then
            ConsoleWrite("Clicked in area 800, 700, 1000, 900" & @LF)
        EndIf
    EndIf
WEnd

Slightly more complicated but would be better

#include <MsgBoxConstants.au3>
#include <StructureConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>

OnAutoItExitRegister(Cleanup)

Global $hCallBack = DllCallbackRegister(MouseProc, "long", "int;wparam;lparam")
Global $hMod = _WinAPI_GetModuleHandle(0)
Global $hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hCallBack), $hMod)

While (True)
    Sleep(100)
WEnd

Func MouseProc($nCode, $wParam, $lParam)
    If ($nCode < 0) Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    Local $tMouse = DllStructCreate($tagPOINT & ";dword mouseData;dword flags;dword time;ulong_ptr dwExtraInfo", $lParam)
    Local $iX = DllStructGetData($tMouse, 1)
    Local $iY = DllStructGetData($tMouse, 2)
    $tMouse = Null

    Switch ($wParam)
        Case $WM_LBUTTONDOWN
            If ($iX = 900 And $iY = 800) Then
                ConsoleWrite("Clicked at 900, 800" & @LF)
            EndIf
            ; Better to check if in an area, not at exact point
            If (_WinAPI_PtInRectEx($iX, $iY, 800, 700, 1000, 900)) Then
                ConsoleWrite("Clicked in area 800, 700, 1000, 900" & @LF)
            EndIf

        Case $WM_LBUTTONUP

        Case $WM_MOUSEMOVE

        Case $WM_MOUSEWHEEL

        Case $WM_MOUSEHWHEEL

        Case $WM_RBUTTONDOWN

        Case $WM_RBUTTONUP

        Case $WM_MBUTTONDOWN

        Case $WM_MBUTTONUP

    EndSwitch

    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>MouseProc

Func Cleanup()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hCallBack)
EndFunc   ;==>Cleanup

 

Edited by InunoTaishou
Link to comment
Share on other sites

The first one will fail because I only included the needed #include to get the _WinApi_PtInRectEx function to work, I did not include Misc.au3 because I assumed this snippit was being used in a larger file. Just edited the first snippit to make it a fully working snippit by itself.

Second snippit works just fine. All it does it is print to the console that you clicked in the area. Perhaps you're not clicking in the correct area. Add a tooltip under the Case $WM_MOUSEMOVE showing the $iX and $iY position to see where you're clicking

Link to comment
Share on other sites

52 minutes ago, c7aesa7r said:

@InunoTaishou

Wow thankyou for the help, when i run the first script it gives a error.

Screenshot_2.png

 

 

And the second script when i click in the determined area nothing happens;

Just to clarify on this, the function failed because the following line is missing at the top of your script.

#include <misc.au3>

Once you add this line, you will no longer get an error on line 14.

 

Have a great day.

 

 

Link to comment
Share on other sites

Then also read faq 31 in frequently asked questions. That will give you indications like getcontrol from position x y. So then you are basing your action on the content of the control like menutext. Much more reliable then position clicking.

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