Snap mouse cursor to center of screen - autohotkey

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.

Related

Determine which monitor the focus window is on?

Small AHK function to determine which monitor the focus window is on.
I was writing a script that needed context on which monitor the focus window was on. I found quite a few solutions, but none were too easy to follow, or were a little more complex than needed.
Below will get you just that. The monitor Index in AHK so you can reference it.
GetFocusWindowMonitorIndex(){
;Get number of monitor
SysGet, monCount, MonitorCount
;Iterate through each monitor
Loop %monCount%{
;Get Monitor working area
SysGet, workArea, Monitor, % A_Index
;Get the position of the focus window
WinGetPos, X, Y, , , A
;Check if the focus window in on the current monitor index
if (X >= workAreaLeft && X < workAreaRight && Y >= workAreaTop && Y < workAreaBottom ){
;Return the monitor index since its within that monitors borders.
return % A_Index
}
}
}
Note Below is a modified version where the window is passed as an argument, and not the default; focus window
GetFocusWindowMonitorIndex(thisWindow){
;Get number of monitor
SysGet, monCount, MonitorCount
;Iterate through each monitor
Loop %monCount%{
;Get Monitor working area
SysGet, workArea, Monitor, % A_Index
;Get the position of the focus window
WinGetPos, X, Y, , , %thisWindow%
;Check if the focus window in on the current monitor index
if (X >= workAreaLeft && X < workAreaRight && Y >= workAreaTop && Y < workAreaBottom ){
;Return the monitor index since it's within that monitors borders.
return % A_Index
}
}
}
Even if this helps one more person, I'll call it a win.
EDIT:
Should you need the working area (exclude the tarkbar from the working area) use this functions.
GetFocusWindowMonitorIndex(){
;Get number of monitor
SysGet, monCount, MonitorCount
;Iterate through each monitor
Loop %monCount%{
;Get Monitor working area
SysGet, workArea, MonitorWorkArea , % A_Index
;Get the position of the focus window
WinGetPos, X, Y, , , A
;Check if the focus window in on the current monitor index
if (X >= workAreaLeft && X < workAreaRight && Y >= workAreaTop && Y < workAreaBottom ){
;Return the monitor index since its within that monitors borders.
return % A_Index
}
}
}
I would like to point out that the previous answer will not work if:
You have a maximized window (the window will go beyond the monitor boundaries slightly, in my case by 10 pixels in every direction)
The window overlaps multiple windows. In that case it would be most appropriate to see which monitor contains the largest portion of the active window.
GetFocusWindowMonitorIndex(){
;Get number of monitor
SysGet, monCount, MonitorCount
;Get the position of the focus window
WinGetPos, WindowX, WindowY, WindowWidth, WindowHeight, A
;Make an array to hold the sub-areas of the window contained within each monitor
monitorSubAreas := []
;Iterate through each monitor
Loop %monCount%{
;Get Monitor working area
SysGet, Monitor, MonitorWorkArea , % A_Index
;Calculate sub-area of the window contained within each monitor
xStart := max(WindowX, MonitorLeft)
xEnd := min(WindowX + WindowWidth, MonitorRight)
yStart := max(WindowY, MonitorTop)
yEnd := min(WindowY + WindowHeight, MonitorBottom)
area := (xEnd - xStart) * (yEnd - yStart)
;Remember these areas, and which monitor they were associated with
monitorSubAreas.push({"area": area, "index": A_Index})
}
;If there is only one monitor in the array, then you already have your answer
if(monitorSubAreas.length() == 1) {
return monitorSubAreas[1].index
}
;Otherwise, loop to figure out which monitor's recorded sub-area was largest
winningMonitor := 0
winningArea := 0
for index, monitor in monitorSubAreas {
winningMonitor := monitor.area > winningArea ? monitor.index : winningMonitor
winningArea := monitor.area > winningArea ? monitor.area : winningArea
}
return winningMonitor
}

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.

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

MouseMove doesn't seem to work

I made a (working) auto clicker and I wanted to add a little modification to it.
I want the code to grab the mouse's current position when you call the AutoClick function. Now when it calls the ClickClick function it gets the now current position of the mouse. After that, it snaps the mouse back to the original position (oX, oY) and clicks. After clicking, it jumps to the position the mouse was just at (x, y). The clicking part works, but the mouse doesn't move at all.
I have no idea what to do to try to fix it.
^h::AutoClick()
^j::ExitApp
AutoClick(Interval=100){
MouseGetPos, xpos, ypos
oX = %xpos%
oY = %ypos%
static Toggler
Toggler := !Toggler
TPer := Toggler ? Interval : "off"
SetTimer, ClickClick, %TPer%
return
ClickClick:
BlockInput On
MouseGetPos, x, y
MouseMove, %oX%, %oY%, 0
Click
MouseMove, %x%, %y%, 0
BlockInput Off
return
}
Fistly, you have some restructuring you need to take care of- Get your Timer's subroutine out of the function. It doesn't belong there; plus it won't be isolated to the function anyway because it's global.
The oX, oY variables are in fact isolated to the function and are therefore only available within that function. Unless you declare them as global.
oX:=oY:=""
^h::AutoClick()
^j::ExitApp
AutoClick(Interval=100){
global oX, oY
static Toggler
MouseGetPos, xpos, ypos
oX = %xpos%
oY = %ypos%
Toggler := !Toggler
TPer := Toggler ? Interval : "off"
SetTimer, ClickClick, %TPer%
return
}
ClickClick:
BlockInput On
MouseGetPos, x, y
MouseMove, %oX%, %oY%, 0
Click
MouseMove, %x%, %y%, 0
BlockInput Off
return
Alternatively, you can have your function return a value (in this case mouse coords) and pass those back to your ClickClick.

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.