AutoHotkey: calculating with mouse coordinates does not work - autohotkey

Assume that upon clicking and holding, I want to show a colored box which covers the area between clicking and releasing the left mouse button. The following simple script traces the mouse position upon clicking and releasing if Ctrl is hold and shows a tooltip with the covered area. Showing the box only works if I specify a fixed size for the box:
However, if I try to calculate the size of the box (uncomment the last line of code), most of the time no box is shown?
Ctrl & LButton::
MouseGetPos, start_x, start_y
Keywait, LButton
MouseGetPos, end_x, end_y
; show coordinates for debugging
ToolTip % "dx: " . end_x-start_x . "`ndy: " . end_y-start_y
Gui, -Caption -Border +AlwaysOnTop
Gui, Color, red
; fixed size works ...
Gui, Show, % "x" start_x "y" start_y "w" 100 "h" 10
; ... but the following doesnt work
;Gui, Show, % "x" start_x "y" start_y "w" end_x-start_x "h" end_y-start_y

Since screen coordinates can be negative, use abs()(docs) to make a negative width or height positive:
Gui, Show, % "x" start_x "y" start_y "w" abs(end_x-start_x) "h" abs(end_y-end_x)

Related

Having problem with pixelgetcolor within loop and would be open to any suggestion

This is my code. What i want to do, is this loop to finish what its doing when i find this this color.
I belive the problem is with the cordinates from pixelgetcolor, i have no idea how to put cordinates there. Iv tried with pixelsearch and it wasnt a succes either.Do you have any suggestion what should i do ? The idea is to click non-stop unless this color is on the screen.
CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
Loop ; This loop do non stop, unless the color is on the screen.
{
MouseMove, %X%, %Y%
Click
sleep,%flick%
Click
sleep,%flick1%
PixelGetColor,Dead_Monster,X,Y,RGB
if(Dead_Monster = 0x3CC4C4)
{
break ΒΈ
}
}
You need to wrap your variables in % for PixelGetColor to use the value of those variables. Also, your variables X and Y are never set anywhere...
CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
; initial values?
X := 800
Y := 600
Loop ; This loop do non stop, unless the color is on the screen.
{
MouseMove, %X%, %Y%
Click
sleep,%flick%
Click
sleep,%flick1%
PixelGetColor,Dead_Monster,%X%,%Y%,RGB
if(Dead_Monster == 0x3CC4C4){
break
}
; insert line here to update X, Y ?
}
If you're looking to search the screen for a pixel, you can do the following:
; Modified example from https://www.autohotkey.com/docs/commands/PixelSearch.htm
Dead_Monster := 0x3CC4C4
;Search rectangle
S_Left := 0
S_Top := 0
S_Bottom := A_ScreenHeight
S_Right := A_ScreenWidth
PixelSearch, Px, Py, %S_Left%, %S_Top%, %S_Bottom%, %S_Right%, %Dead_Monster%, 3, Fast
if ErrorLevel
MsgBox, That color was not found in the specified region.
else
MsgBox, A color within 3 shades of variation was found at X%Px% Y%Py%.
However, I don't recommend searching the whole screen area as the search time increases greatly with larger search areas.

WinGetPos returning negative coordinates

I am trying to get the position and size of windows. I am seeing however that I am getting -9 for both X and Y on any full screen windows.
WinGetTitle, WT, A
WinGet, WID, ID, A
WinGetPos, X, Y, Width, Height, %WT%
ListVars
WinWaitActive ahk_class AutoHotkey
ControlSetText Edit1, [Title]`r`n%WT%`r`n[ID]`r`n%WID%`r`n[Dimensions]`r`nX: %X%`r`nY: %Y%`r`nWidth: %Width%`r`nHeight: %Height%
WinWaitClose
Output of the above code is:
[Title]
Stuff.ahk - SciTE4AutoHotkey
[ID]
0x4e079a
[Dimensions]
X: -9
Y: -9
Width: 1938
Height: 1048
Is it just my resolution that is causing this? When I use a window spy I can clearly see that the top left corner of the window is at 0,0 absolute or 9,9 by window.
Any idea why this is happening?
The Client area is -9 x y from the window area to reach 0x0 of your screen. This is just how it is.

Autohotkey get width of current active child window

I am trying to write a script that moves mouse cursor to the center of active child window.
WinGetActiveStats, Title, Width, Height, X, Y
MouseMove, Width / 2, Height / 2, 0
Above does the trick for the Main window. However, I can't seem to find a way to grab the width and height of active child window so that I can move the mouse cursor to the center of child window.
This is very useful feature when using with softwares such as AutoCAD where you could have more than one child window open at any given moment.
Thanks,
The following should work:
ControlGetFocus, cr, A ; get the focused(active) control(child window) of the active window
ControlGetPos, x, y, width, Height, %cr%, A ; get the position and dimensions of this control
MouseMove, % x + Width / 2, % y + Height / 2, 0

pixelsearch in middle of screen/monitor?

i wanted to afk or have some break while leveling my character,the green box are the monster yea i messed around inside game files and edited it with photoshop lol,
my script is doing perfectly fine and attacking monster, but how can I make pixelsearch/attack only in the middle screen? to avoid my character from deaths. It doesn't work i dont know why. can you suggest me what's wrong?
CoordMode, Pixel, Relative
SetMouseDelay, -1
Home:: press home to start
;assuming to search in a reactangle area 200x200px
leftBound := A_ScreenWidth / 2 - 100
rightBound := A_ScreenWidth / 2 + 100
topBound := A_ScreenHeight / 2 - 100
bottomBound := A_ScreenHeight / 2 + 100
Loop {
PixelSearch, X, Y, leftBound, topBound, rightBound, bottomBound, 0x00FF00, 0, fast
if(ErrorLevel=0)
{
MouseClick, left, %X%, %Y%
}
else {
Send {F9}
sleep, 50
}
}
return
PgUp::Pause
End::ExitApp
You need to put the variables into percent signs %:
PixelSearch, X, Y, %leftBound%, %topBound%, %rightBound%, %bottomBound%, 0x00FF00, 0, fast
Also see the documentation for details:
For X1,Y1, it says:
The X and Y coordinates of the upper left corner of the rectangle to search
By "coordinates", obviously a number is described. This means, that in this case, you need to state a value and not variable's name. The values are stored inside the variables, so use the percent signs to access the variable content.

Snap mouse cursor to center of screen

When F4 is pressed I'm attempting to snap the cursor to center of screen. Here is what I'm trying :
F4::
x := (A_ScreenWidth / 2)
y := (A_ScreenHeight / 2)
mousemove, x, y
return
But when I run this script the mouse position moves on opening and when press F4 the mouse does not moves position ?
Try this:
#Persistent ;//keeps script running
CoordMode, Mouse, Screen
Return ;//stops auto execution
F4:: ;//your code
x := (A_ScreenWidth / 2)
y := (A_ScreenHeight / 2)
mousemove, x, y
return
Without #Persistent, script would close after executing all the lines of code.
Autohotkey executes every line of code until the first "Return".
CoordMode line will ensure the mouse movement is relative to the screen as opposed to the active window (credit: #user3419297)
Toodles
F4::
CoordMode, Mouse, Screen ; If this command is not used, the coordinates are relative to the active window.
x := (A_ScreenWidth / 2)
y := (A_ScreenHeight / 2)
mousemove, x, y
return
http://ahkscript.org/docs/commands/CoordMode.htm#Remarks
Nircmd by Nir Sofer has the following option:
nircmd setcursor x y
You can create a shortcut to this command line and assign any hotkey to it. There are a lot of other options for mouse cursor as detailed in the nircmd.chm file.