12

I have a laptop with its main screen(A) and a connected one(B).

Sometimes the mouse is on one screen(B) and I want it on another screen(A), but i'm just looking at the one screen(A) where the mouse isn't. I could drag a lot and look for it seeing for it to appear on A, or if B is on I can turn my head to B to see the cursor location on it(B)..so I can see when it'll get to A as I move it, But it'd be easier if I could just with a keyboard shortcut, get the mouse cursor in the middle of my laptop screen(A).

So a key to move the cursor from one screen to the other screen to a place I'd know where it is like the center. Even a mouse movement that does it, so long as whatever the move or key is works wherever the mouse is, would be ok. Is there a way?

barlop
  • 25,198

3 Answers3

14

Use AutoHotKey, with the "CoordMode" and the "MouseMove" commands.

CoordMode:

Sets coordinate mode for various commands to be relative to either the active window or the screen.

MouseMove:

Moves the mouse cursor.

Here's an example to move it to the center of the current screen:

!z::
CoordMode, Mouse, Screen
MouseMove, (A_ScreenWidth // 2), (A_ScreenHeight // 2)
return
2

This script works for me:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


#SingleInstance Force ; if the script is updated, replace the one and only instance

!m::

; 80 = CM_MONITORS - which basically means - monitor count
SysGet, count, 80
if count !=2 return ; only handle duo, uno - nothing to do

CoordMode, Mouse, Screen    ; this is a must as the mouse coordinate are now relative to the whole screen (both monitors)

SysGet, mon_1, Monitor, 1
SysGet, mon_2, Monitor, 2
MouseGetPos mouse_x,mouse_y

; toggle the mouse to the middle of the other screen

if (mouse_x <= mon_1Right)
{
  new_x := mon_2Left + (mon_2Right - mon_2Left) // 2
  new_y := mon_2Top + (mon_2Bottom - mon_2Top) // 2
}
else
{
  new_x := mon_1Left + (mon_1Right - mon_1Left) // 2
  new_y := mon_1Top + (mon_1Bottom - mon_1Top) // 2
}

MouseMove, %new_x%, %new_y%
Click
return

Esc::ExitApp  ; Exit script with Escape key
1

Wrote my own script in v2 since scripts in previous answers are in v1.

  • Works with any number of monitors.
  • Handles different DPI res on different monitors.
  • Forward/back mouse buttons switch to next/previous monitor.
  • Will move cursor to same (scaled) relative position on monitor switched to
#SingleInstance Force

MouseSwitchMonitor(switchNumber := 1) { DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr") ; handles multiple monitors with different DPI settings CoordMode("Mouse", "Screen")

count := MonitorGetCount()

if (count &lt;= 1) {
    return
}

MouseGetPos(&amp;mouseX, &amp;mouseY)
relativeMouseX := 0.5
relativeMouseY := 0.5
mouseMonitor := 0

monitors := {}

counter := 1
Loop count {
    actual := MonitorGet(counter, &amp;left, &amp;top, &amp;right, &amp;bottom)
    monitors.%actual% := {left: left, top: top, right: right, bottom: bottom}
    if (left &lt;= mouseX &amp;&amp; mouseX &lt; right &amp;&amp; top &lt;= mouseY &amp;&amp; mouseY &lt; bottom) {  ; from testing, seems mouse position can be left or top but not right or bottom
        mouseMonitor := actual
        relativeMouseX := (mouseX - left) / (right - left)
        relativeMouseY := (mouseY - top) / (bottom - top)
    }
    counter++
}

mouseMonitor := Mod(mouseMonitor - 1 + Mod(switchNumber, count) + count, count) + 1  ; modular arithmetic to cycle, and make sure 1 &lt;= mouseMonitor &lt;= count

if (!HasProp(monitors, mouseMonitor)) {
    return
}
monitor := monitors.%mouseMonitor%

MouseMove(monitor.left + Floor(relativeMouseX * (monitor.right - monitor.left)), monitor.top + Floor(relativeMouseY * (monitor.bottom - monitor.top)), 0)  ; since mouse position can be left or top, but not right or bottom

}

XButton1::MouseSwitchMonitor(-1) ; backward mouse button, previous monitor XButton2::MouseSwitchMonitor(1) ; forward mouse button, next monitor

Shuri2060
  • 150