# Using GetMousePos for calculating a MouseClick

## Recommended Posts

Hello There!

I've encountered a small issue i just cant seem to program my way out of...

What im doing is:        ;or rather, what im trying to do is
Pixelsearch for a color, within a part of my screen,
If color is present, i will rightclick it, for a menu to appear,
in which i want to Leftclick an option. Simplicity itself.

The issue is that because this menu appears in different parts of my screen, its complicated to select the option i want to click.
There is no special color to it, and i cannot define the area in which to click, because the menu appears different places.
The only thing that is in common for the menu is that the Leftclick needs to be (lets say) 30pixels below the rightclick

.

What can be done,  but seemingly not by me, is to get the coordinates of the rightclick, and use them as the "starting point" from where the coordinates for the leftclick will be.
(Not sure if that makes sence, but maybe the code below will help you understand)

You guys like example codes, so therefore i will give it my best shot, and hope it proves usefull

$pos = Pixelsearch(200, 400, 200, 400, 0xFFFFFF) ;This color is what i need to rightclick to bring up the menu If not @Error then ;If the color is present (Mouseclick("Right",$pos[0], $pos[1], 1, 1) ;Rightclick it, to bring up menu ;Here comes the part i cannot get to work$x = MousegetPos(X)                                                          ;To get the Coordinate "X", so we can calculated with it for the position of the leftclick
$y = MousegetPos(Y) ; To get the Coordinate "Y", so that can be calculated with for the position of the leftclick MouseClick("Left",$x[0], $y-30[1], 1, 1) ;This was supposed to give me a leftclick that is 30pixels below the rightclick. (The MouseGetPos(X), may seem as a waste of space in this example, but in my script i will need both. Im only using one in the example, mainly so that i wont confuse myself, but also to make it easier to digest for you guys ) I have not been able to find any topic that covers this, but my apologies if such a topic already exist in the forums. Maybe while im at it, can i ask, what good does the [0] and [1] do, when using a "$variable" as coordinate?

- DiveHigh

$a = MouseGetPos() MouseClick("Left",$a[0],$a[1]-30) Edited by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. ##### Link to post ##### Share on other sites To anyone else that might find this useful i did some more test. if you search for a color, and want to click it, but maybe with more randomized position of the click, then you can do:$pos = Pixelsearch(200, 400, 200, 400, 0xFFFFFF)
If not @Error then
MouseClick
("Left", $pos[0] +Random(-10,10),$pos[1]+Random(-10,10), 1, 1)

[+Random(-10,10)  = this is +/- 10 pixels within the area which the color was found]

Have Fun!
- DiveHigh

##### Share on other sites

Just out of curiosity: Which program do you try to automate with this script?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
WebDriver - Wiki

##### Share on other sites

@water: may be a browser game

##### Share on other sites
• 3 weeks later...

This was used for a project in my class where we had a week to "create intelligence"..
I got 3rd best grade

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

• ### Similar Content

• By dejhost
Hi.
I am trying to automate a software called "LabelImg" (https://pypi.org/project/labelImg/).
My autoit-script is started once I selected a folder with images within LabelImg. Pressing the button "Next Image" or pressing the shortcut "d" (https://github.com/tzutalin/labelImg#Hotkeys) jumps to the next image in the selected folder. This shall happen once per second.
#include <Misc.au3> #include <MsgBoxConstants.au3> #include <AutoItConstants.au3> Opt("WinTitleMatchMode", 1) Local $hDLL = DllOpen("user32.dll") While 1 If _IsPressed("1B",$hDLL) Then ExitLoop Else Local $temp = WinActivate("labelImg") ConsoleWrite($temp & @CRLF) If WinActivate("labelImg") Then ConsoleWrite("All Set!" & @CRLF) EndIf ;Send("d") Local $temp = MouseClick($MOUSE_CLICK_RIGHT, 50, 200) If $temp <> 1 Then MsgBox(1, "$temp", $temp) ExitLoop EndIf Sleep(1000) EndIf WEnd DllClose($hDLL) So the Send ("d")-command and the MouseClick are alternative methods to jump to the next image. Both fail.
Both ConsoleWrite's deliver proper feedback (I continiously get the handle and "All set" ).

Could you tell me what I'm doing wrong?
Thank you.

• Hello,
i have been making a countdown timer and its all working just how i like it, its just skipping the 0's en going from 1 - 59 ( it does wait the time needed for the counter to go from 1 - 0 - 59) It just goes from 1 to 59, showing the 1 for the time needed to go from 1 to 0 and then to 59.

Here's my code:
For $i = 60 To 1 Step -1 Next If$i = 0 Then $Sc =$Sc - 1 EndIf If $Sc = 0 Then$Sc = 0 ;~ <--- Shouldnt this work? Setting it to 0 then wait 1 sec to set it to 59? Sleep(1000) $Mn =$Mn - 1 $Sc = 59 EndIf If$Mn = 0 And $Sc = 0 Then$CDHrs = 0 Sleep(1000) $Mn = 59 EndIf GUICtrlSetData($Countdown, 'Hrs:' & $CDHrs & ' ' & 'Min:' &$Mn & ' ' & 'Sec:' & $Sc) Sleep(1000) I dont need the solution in code, just point me into the general direction where i am going wrong, i'd like to learn from my mistake. Thanks in advance. ~ Maffiagang • it recomended to use the builten function _DateDiff hello autoit team i made a simple function to help me get the difference between to date i liked to share it with you the paramatrs of this function is firstdate and lastedate the two params is as the following year/month/day hours:minuts:seconds here is the function #include <date.au3> func _dateBetween($s_firstDate, $s_lastDate = default) if not (StringRegExp($s_firstDate, "((\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\:(\d{2})\:(\d{2}))", 0) = 1) then Return SetError(1, 0, 0) if $s_lastDate = default then$s_lastDate = @year & "/" & @mon & "/" & @mday & " " & @hour & ":" & @min & ":" & @sec if not (StringRegExp($s_lastDate, "((\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\:(\d{2})\:(\d{2}))", 0) = 1) then Return SetError(2, 0, 0) local$a_FirstSplitDate = StringSplit(StringRegExpReplace($s_firstDate, "((.*)\s(.*))", "$2"), "/", 2) local $a_FirstSplitTime = StringSplit(StringRegExpReplace($s_firstDate, "((.*)\s(.*))", "$3"), ":", 2) local$a_lastSplitDate = StringSplit(StringRegExpReplace($s_lastDate, "((.*)\s(.*))", "$2"), "/", 2) local $a_LastSplitTime = StringSplit(StringRegExpReplace($s_lastDate, "((.*)\s(.*))", "$3"), ":", 2) local$i_firstDateValue = _DateToDayValue($a_FirstSplitDate[0],$a_FirstSplitDate[1], $a_FirstSplitDate[2]) local$i_LastDateValue = _DateToDayValue($a_LastSplitDate[0],$a_LastSplitDate[1], $a_LastSplitDate[2]) if$i_firstDateValue > $i_LastDateValue then Return SetError(3, 0, 0) local$i_totalDays = $i_LastDateValue-$i_firstDateValue if not ($i_totalDays = 0) then if$a_FirstSplitTime[0] <= $a_lastSplitTime[0] then$i_totalDays += 1 $a_lastSplitTime[0] -=$a_FirstSplitTime[0] elseIf $a_FirstSplitTime[0] >$a_lastSplitTime[0] then if $i_totalDays > 0 then$i_totalDays -= 1 $a_FirstSplitTime[0] -=$a_lastSplitTime[0] endIf endIf endIf local $i_totalYears = 0 if$i_totalDays >= 365 then While $i_totalDays >= 365$i_totalYears += 1 $i_totalDays -= 365 Wend endIf local$i_TotalMonths = 0 if $i_totalDays >= 30 then While$i_totalDays >= 30 $i_totalMonths += 1$i_totalDays -= 30 Wend endIf local $i_totalWeeks = 0 if$i_totalDays >= 7 then While $i_totalDays >= 7$i_totalWeeks += 1 $i_totalDays -= 7 Wend endIf$i_totalSeconds = ((($a_FirstSplitTime[0]*60)*60)+($a_FirstSplitTime[1]*60)+($a_FirstSplitTime[2]))-((($a_lastSplitTime[0]*60)*60)+($a_lastSplitTime[1]*60)+($a_LastSplitTime[2])) local $minus = "" if$i_totalSeconds < 0 then $minus = "-"$i_totalSeconds = StringReplace($i_totalSeconds, "-", "") endIf local$i_totalHours = 0 if $i_totalSeconds >= 3600 then While$i_totalSeconds >= 3600 $i_totalHours += 1$i_totalSeconds -= 3600 Wend endIf local $i_totalminuts = 0 if$i_totalSeconds >= 60 then While $i_totalSeconds >= 60$i_totalminuts += 1 $i_totalSeconds -= 60 Wend endIf local$a_arrayReturn[7] $a_arrayReturn[0] =$i_totalYears $a_arrayReturn[1] =$i_totalMonths $a_arrayReturn[2] =$i_totalWeeks $a_arrayReturn[3] =$i_totalDays $a_arrayReturn[4] =$minus & $i_totalHours$a_arrayReturn[5] = $minus &$i_totalminuts $a_arrayReturn[6] =$minus & $i_totalSeconds return$a_arrayReturn endFunc
here is an example

$a_calc = _dateBetween("2015/02/12 23:00:05", "2030/02/12 23:25:50") msgBox(64, "result", StringFormat("the difference is : %i years and %i months and %i weeks and %i days and %i hours and %i minuts and %i seconds",$a_calc[0], $a_calc[1],$a_calc[2], $a_calc[3],$a_calc[4], $a_calc[5],$a_calc[6]))
with my greetings
• By Rskm
Hi, recently i created a GUI for some calculations in AutoIT.  The GUI has 2 tabs and on the first tab, it has few inputboxes where i use to click with mouse and start entering the inputs. i recently made some changes in the position of these textboxes. I made this by changing the autoit code file instead of making changes in KODA. Now i cannot click inside these textboxes with mouse. However, i can use TAB key to cycle through/enter input in these input boxes. what could have made the inputboxes not responding to mouseclicks. thanks

• Hi All,
I am trying to login into our application using AutoIT. When I launch the application a splash screen appears and the login screen goes behind it. At this point the script pauses and I loose control of everything. I have literally tried everything for the past two days but nothing has worked yet. Any suggestions would be appreciated.
RunWait("\\mchsv411.siplaceworld.net\splmshare\int00\conf\start_apps\windows\start_nx110.bat en portal_client int00 tc112", "", @SW_MAXIMIZE)
AutoItSetOption("SendKeyDelay", 200)
Local \$hWnd = WinWaitActive("Teamcenter Login")
MouseClick("left", @DesktopWidth / 2, @DesktopHeight / 2)
The splash screen has title "Teamcenter" and is static.
I tried to click on the center to get control of login window.
I have tried WinActivate, WinSetState, ControlCommand and a lot of other things.
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...