c7aesa7r Posted June 5, 2017 Share Posted June 5, 2017 (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 June 5, 2017 by c7aesa7r Link to comment Share on other sites More sharing options...
InunoTaishou Posted June 5, 2017 Share Posted June 5, 2017 (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 expandcollapse popup#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 June 5, 2017 by InunoTaishou genius257 1 Link to comment Share on other sites More sharing options...
c7aesa7r Posted June 5, 2017 Author Share Posted June 5, 2017 (edited) @InunoTaishou Wow thankyou for the help, when i run the first script it gives a error. And the second script when i click in the determined area nothing happens; Edited June 5, 2017 by c7aesa7r Link to comment Share on other sites More sharing options...
InunoTaishou Posted June 5, 2017 Share Posted June 5, 2017 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 More sharing options...
c7aesa7r Posted June 5, 2017 Author Share Posted June 5, 2017 ; 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 Link to comment Share on other sites More sharing options...
EmilyLove Posted June 5, 2017 Share Posted June 5, 2017 52 minutes ago, c7aesa7r said: @InunoTaishou Wow thankyou for the help, when i run the first script it gives a error. 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 More sharing options...
c7aesa7r Posted June 5, 2017 Author Share Posted June 5, 2017 I did that but that small script consume like 20% cpu, and also not working when i click in the xy pos declared Link to comment Share on other sites More sharing options...
EmilyLove Posted June 5, 2017 Share Posted June 5, 2017 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 Link to comment Share on other sites More sharing options...
c7aesa7r Posted June 5, 2017 Author Share Posted June 5, 2017 But if my other script send the click while it sleeping? the second script have low cpu usage but not working:'( Link to comment Share on other sites More sharing options...
EmilyLove Posted June 5, 2017 Share Posted June 5, 2017 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. Link to comment Share on other sites More sharing options...
c7aesa7r Posted June 5, 2017 Author Share Posted June 5, 2017 (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 June 5, 2017 by c7aesa7r Link to comment Share on other sites More sharing options...
EmilyLove Posted June 5, 2017 Share Posted June 5, 2017 Again, we aren't going to be able to fully help you unless we can see both scripts. Link to comment Share on other sites More sharing options...
c7aesa7r Posted June 5, 2017 Author Share Posted June 5, 2017 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 Link to comment Share on other sites More sharing options...
junkew Posted June 5, 2017 Share Posted June 5, 2017 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. EmilyLove 1 FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now