c7aesa7r

Trigger event on mouse position in x,y

14 posts in this topic

#1 ·  Posted (edited)

How to trigger a event when detect mouse left click on a position x,y?

My script right now:

While 1
    If _IsPressed("01") Then
        $pos = MouseGetPos()
        
        
    EndIf
WEnd

But how i compare if it clicked in a determined x,y pos?

Like if $pos = 900,800 then

Edited by c7aesa7r

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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
1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

@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;

Edited by c7aesa7r

Share this post


Link to post
Share on other sites

#4 ·  Posted

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

Share this post


Link to post
Share on other sites

#5 ·  Posted

; Better to check if in an area, not at exact point
            If (_WinAPI_PtInRectEx($iX, $iY, 0, 1920, 0, 1080)) Then
                ConsoleWrite("Clicked in area 700, 900, 400, 900" & @LF)
            EndIf

I added the pos of my whole screen and independent where i click nothing happens

Share this post


Link to post
Share on other sites

#6 ·  Posted

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.


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

#7 ·  Posted

I did that but that small script consume like 20% cpu, and also not working when i click in the xy pos declared

Share this post


Link to post
Share on other sites

#8 ·  Posted

It is running at a high percentage because you have not told the script to sleep. Adding some short sleeps will eliminate high cpu usage during idle.

https://www.autoitscript.com/autoit3/docs/functions/Sleep.htm


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

#9 ·  Posted

But if my other script send the click while it sleeping? the second script have low cpu usage but not working:'(

Share this post


Link to post
Share on other sites

#10 ·  Posted

It would help if we could see the entire code. That would help us work out a solution that would work best for your project.


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Could help, telling me why second script not working here, it supposed to write a msg when i click in the declared pos right? Do you also tested it there?

Edited by c7aesa7r

Share this post


Link to post
Share on other sites

#12 ·  Posted

Again, we aren't going to be able to fully help you unless we can see both scripts.


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

#13 ·  Posted

I dont dit the second script yet, what im trying is run things when i click in x y pos like if i click pos 90 800 open firefox, if click pos 33 22 run music y, just examples

Share this post


Link to post
Share on other sites

#14 ·  Posted

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.

1 person likes this

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