简体   繁体   English

AutoHotkey MouseMove无法正确居中

[英]AutoHotkey MouseMove not centering properly

I'm running the below code and I expect the mouse to move to the center of the currently active window when I hit comma.....instead it is moving to different points on the screen, depending on where the window is on the screen. 我正在运行以下代码,我希望当我按下逗号时鼠标移动到当前活动窗口的中心.....相反,它会移动到屏幕上的不同点,具体取决于窗口在屏幕上的位置。屏幕。 It only centers the mouse properly when the window is positioned at the top left (x=0, y=0). 仅当窗口位于左上角(x = 0,y = 0)时,鼠标才能正确居中。

#NoEnv
SendMode Input
#WinActivateForce

Sysget, Mon2, Monitor, 2

,::

WinGetActiveStats, Title, Width, Height, X, Y
 {
MsgBox, The active window "%Title%" is %Width% wide`, %Height% tall`, and positioned at %X%`,%Y%.

;center_x:=X+(Width*.5)
;center_y:=Y+(Height*.5)

MouseMove, X+(Width*.5), Y+(Height*.5), 90

 }
Return

Check out CoordMode in the documentation. 在文档中查看CoordMode

Sets coordinate mode for various commands to be relative to either the active window or the screen. 将各种命令设置为相对于活动窗口或屏幕的坐标模式。

CoordMode, ToolTip|Pixel|Mouse|Caret|Menu [, Screen|Window|Client] CoordMode,ToolTip | Pixel | Mouse | Caret | Menu [,Screen | Window | Client]

The default CoordMode is Screen which is why you get two different locations. 默认的CoordModeScreen ,这就是为什么您获得两个不同的位置。 Set the CoordMode to Window to assure that your mouse centering works on the active window. CoordMode设置为Window以确保鼠标居中在活动窗口中起作用。

You can set it for the entire script by calling it during the Auto-Execute section of the script. 您可以通过在脚本的“自动执行”部分调用它来为整个脚本设置它。

I would be sure that Width and Height are the actual dimensions of the window and not the screen resolution. 我可以确定“宽度”和“高度”是窗口的实际尺寸,而不是屏幕分辨率。 Then check X and Y to ensure they're the actual top-left corner of the active window. 然后检查X和Y以确保它们是活动窗口的实际左上角。

If the width and height are not from the actual window, ie the screen size, then this is expected behaivor. 如果宽度和高度不是实际窗口的大小,即屏幕尺寸,那么这是预期的行为。 Perhaps you could show us the calling function to get a better idea of where those parameters are coming from. 也许您可以向我们展示调用函数,以更好地了解这些参数的来源。

the problem was that MousMove uses the coordinates of the window by default, so I changed the MouseMove line to the following: 问题是默认情况下MousMove使用窗口的坐标,因此我将MouseMove行更改为以下内容:

MouseMove, Width*.5, Height*.5

All is good. 一切都很好。

The following script will move mouse to Active Window on dual screen system. 以下脚本将鼠标移至双屏系统上的活动窗口。

I could not get it to work until I put in the sleep line, WinGetPos was getting info before the window had moved. 在进入睡眠行之前,我无法正常工作, WinGetPos在窗口移动之前WinGetPos获取信息。

~#+right::
~#+left::
    Sleep,1000
    WinGetPos, X, Y, width, height, A
    center_x:=width/2
    center_y:=height/2
    MouseMove,center_x,center_y,
return

Tried it all nothing works. 尝试了一切都没有用。 The trick is to use DllCall("SetCursorPos", int, x, int, y) . 诀窍是使用DllCall("SetCursorPos", int, x, int, y) Here is the code to move mouse to center of window. 这是将鼠标移到窗口中心的代码。 Works on multi-montior and non-fullscreen windows. 适用于多蒙版和非全屏窗口。

MoveMouseInCenterOfActiveWindow:
CoordMode,Mouse,Screen
WinGetPos, winTopL_x, winTopL_y, width, height, A
;MouseMove, X, Y, 0 ; does not work with multi-monitor (always off)
    winCenter_x := winTopL_x + width/2
    winCenter_y := winTopL_y + height/2
DllCall("SetCursorPos", int, winCenter_x, int, winCenter_y)
Tooltip winTopL_x:%winTopL_x% winTopL_y:%winTopL_y% winCenter_x:%winCenter_x% winCenter_y:%winCenter_y%
return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM