Autohotkey get width of current active child window - autohotkey

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

Related

How does UIScreen.main.nativeBounds relate to x and y coordinates?

If I print UIScreen.main.nativeBounds.height it returns 2048 for my iPad simulator which is fine. But how does this relate to the x and y coordinates? For example, the middle of the screen is x: 0, y: 0. And if I move to the left on the x coordinates I use minus, like x: -100, and if I move to the right I use plus, like x: 100.
In my head this would mean that UIScreen.main.nativeBounds.height is divided by 2 so that I can move between x: -1024 and x: 1024. Is that correct? Well, it seems that it's not because this code:
portrait = gui.addPortrait(width: 80, height: 80, x:-350 , y: 180)
addChild(portrait)
will take me to the far end of the device's x-axis, like this:
meaning that the total x-axis must be something like 750 in total. What happened to 1024? I think I must misunderstand how the x-coordinates relate to the UIScreen.main.nativeBounds.height. The reason I want to understand this is because I want to set the x position dynamically so that it works on all devices. Something like x: -(UIScreen.main.nativeBounds.height / 3). What am I missing?
In contrast to UIScreen.bounds, which specifies "[t]he bounding rectangle of the screen, measured in points" (docs), UIScreen.nativeBounds specifies "[t]he bounding rectangle of the physical screen, measured in pixels" (docs).
Thus, you'll want to transform the values into points with the help of UIScreen.nativeScale:
let width = round(UIScreen.main.nativeBounds.width / UIScreen.main.nativeScale)
let height = round(UIScreen.main.nativeBounds.height / UIScreen.main.nativeScale)
You can get the height and width of the screen in points like this:
let height = UIScreen.main.bounds.height
let width = UIScreen.main.bounds.width
No point of the screen has negative coordinates:
The top left-hand corner of the screen is x: 0, y :0
The top right-hand corner of the screen is x: width, y: 0
The bottom left-hand corner of the screen is x: 0, y: height
The bottom right-hand corner of the screen is x: width, y: height

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.

how to setup pixelSearch only in middle of window mode screen

im playing ragnarok using macro.. my script is doing good.. but how can i pixelsearch/attack only the middle area? im using 1024 x 768 game window mode, using the CoordMode, Pixel, Relative
CoordMode, Pixel, Relative
CoordMode, Mouse, Relative
Home:: ;click home to start
Loop {
PixelSearch, X, Y, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, 0x00FF00, 0, fast ;<--- this is the color of green boxed monsters to attack
if(ErrorLevel=0) {
MouseClick, left, %X%, %Y%
}
else {
Send {F1} ; <--- if no monster present in screen, press teleport to search monsters
}
}
return
PgUp::Pause
End::ExitApp
As I commented in your previous question:
0, 0, %A_ScreenWidth%, %A_ScreenHeight% are coordinates. Replace these with any nubers (preferably with any variable contents) you like. Measure your exact coordinates with with WindowSpy. Open WindowSpy by right-clicking on any AHK script in the taskbar and selecting it. I suggest you then also take a look at coordmode.
Documentation says:
PixelSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ColorID [, Variation, Fast|RGB]
X1, Y1 The X and Y coordinates of the upper left corner of the rectangle to search
X2, Y2 The X and Y coordinates of the lower right corner of the rectangle to search
You are using: PixelSearch, X, Y, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%. So, the coordinates of your selected rectangle's upper left corner are 0|0 and the lower right corner's are %A_ScreenWidth%|%A_ScreenHeight%. Let's assume your screen is 1366 pixels broad and 768 pixels wide. Then, the coordinates of the lower right corner are 1366|768.
You can replace these numbers with anything you like, for example
PixelSearch, X, Y, 300, 100, 600, 200, 0x00FF00, 0, fast
, or preferably
upperLeftX := 300
upperLeftY := 100
lowerRightX := 600
lower RightY := 200
PixelSearch, X, Y, %upperLeftX%, %upperLeftY%, %lowerRightX%, %lowerRightY%, 0x00FF00, 0, fast
(which is the same as above),
where 300|100 are the coordinates of the upper left corner and 600|200 the coordinates of the lower right corner for PixelSearch to search for your Pixel in. Determine the coords of your preference with WindowSpy.

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.

get mouse position in gtk scrolled window

I have a scrolled window with a viewport, which is some 4000 pixels tall. Is there a way to figure out the mouse position in viewport coordinates? At the moment, what I get for the position is the position in the segment that I actually see, and if I scroll down to the bottom, I still get something like 600 pixels for the vertical position (that is the size of my window), instead of the 4000 that I expect. If there is no easy way, how can one determine by how much the scrolled window is scrolled? I believe, I could then put the pieces together.
Thanks,
v923z
You are looking for the get_vadjustment method of the scrolled_window.
For instance if you bind the button_press_event to the scrolled window:
def button_press_event(self, widget, event):
v_scroll_pos = widget.get_vadjustment().get_value()
print "y pos + scroll pos = %f" % (event.y + v_scroll_pos,)
return gtk.TRUE
Would print the y position + the amount it is scrolled in the y direction.