MouseMove in Background/Inactive Window

Ask gaming related questions (AHK v1.1 and older)
JollyJoeHelpMePls
Posts: 3
Joined: 25 Nov 2022, 12:58

MouseMove in Background/Inactive Window

Post by JollyJoeHelpMePls » 28 Nov 2022, 17:05

Is there a way to simulate mouse movement in a specific window?

For example:
I want to turn my character in-game, which is controlled moving my mouse but my game is not my active window. (It is not minimized, it is just in the background or on a separate screen)
I want to be able to do other things with my mouse while the script runs, meaning it can't move my actual cursors position but only in the game.

Similar to ControlClick and ControlSend but instead it is simulating mouse movement instead of keystrokes or mouse clicks.

Thank you for the help.

User avatar
mikeyww
Posts: 26595
Joined: 09 Sep 2014, 18:38

Re: MouseMove in Background/Inactive Window

Post by mikeyww » 28 Nov 2022, 17:10

As the computer has a total of one pointer, I am not aware of any such thing, but this question has been asked multiple times previously, so you might want to check the answers from other posts, too.

JollyJoeHelpMePls
Posts: 3
Joined: 25 Nov 2022, 12:58

Re: MouseMove in Background/Inactive Window

Post by JollyJoeHelpMePls » 28 Nov 2022, 17:31

mikeyww wrote:
28 Nov 2022, 17:10
As the computer has a total of one pointer, I am not aware of any such thing, but this question has been asked multiple times previously, so you might want to check the answers from other posts, too.
Ironically, I believe you commented on one of these past post. I think someone mentioned WM_MOUSEMOVE? I am not sure exactly what that is or how to implement it.

From the previous post, I am not able to really conclude if people found solutions or not. I am hoping with a more specific use-case and details, someone can give me a more clear answer.

User avatar
mikeyww
Posts: 26595
Joined: 09 Sep 2014, 18:38

Re: MouseMove in Background/Inactive Window

Post by mikeyww » 28 Nov 2022, 17:53

Fair enough!

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: MouseMove in Background/Inactive Window

Post by Rohwedder » 30 Nov 2022, 04:46

Hallo,
try:

Code: Select all

#NoEnv
SetBatchLines, -1
SetWinDelay, 0
SendMode Input
Global Hook, XC, YC, XF, YF, CursorMove
Coordmode, Mouse, Screen
Gui, Cursor:New, -DPIScale +HwndCID, Cursor
Gui, Cursor:Margin, 0, 0
Gui, Cursor:Font, s25  cBlack
Gui, Cursor:Add, Text, x-4 y-18,% CHR(0x21D6)
Gui, Cursor:Color, F0F0F0
Gui, Cursor:+LastFound -Caption +AlwaysOnTop +ToolWindow -Border
Winset, TransColor, F0F0F0
Return

q:: ; Cursor will be hooked
MouseGetPos, XC, YC
XF:= XC, YF := YC, Hook()
Return

#IF Hook ;Cursor is hooked
q::UnHock() ;Cursor will be unhooked
*RButton::
UnHock()
Click %XF% %YF% Down
KeyWait, RButton
Click Up
MouseMove, XC, YC, 0
Hook()
Return
w::
a::
s::
d::
CursorMove := True
MouseMove, XC+=10*(GetKeyState("d","P")-GetKeyState("a","P"))
	, YC+=10*(GetKeyState("s","P")-GetKeyState("w","P")), 0
CursorMove := False
Return
#IF

MouseMove(nCode, wParam, lParam)
{   Global  ; WM_MOUSEMOVE = 0x0200
    If (wParam = 0x0200) And !CursorMove {
        WinMove, ahk_id %CID%,
        ,% XF += NumGet(lParam+0, "Short") - XC
        ,% YF += NumGet(lParam+4, "Short") - YC
        Return, 1
    }
    Return, DllCall("CallNextHookEx", "UInt", 0, "Int"
    , nCode, "UInt", wParam, "UInt", lParam)
} Hook() {
	Gui, Cursor:Show, NoActivate x%XF% y%YF%, Cursor
	Hook := DllCall("SetWindowsHookEx", "Int", 14
	, "Ptr", RegisterCallback("MouseMove")
	, "Ptr", DllCall("GetModuleHandle", "Ptr", 0, "Ptr"), "UInt", 0)
} UnHock() {
	Gui, Cursor:Hide
	DllCall("UnhookWindowsHookEx", "Ptr", Hook), Hook:=CursorMove:=False
}
The Q key is used to hook and unhook the Cursor. As long as the Cursor is hooked, it can be clicked there with LButton, but only a Fake Cursor will move with the mouse. The Cursor can be moved with the WASD keys. As long as RButton is pressed, this Fake Cursor becomes the real one.
Last edited by Rohwedder on 30 Nov 2022, 11:26, edited 2 times in total.

User avatar
mikeyww
Posts: 26595
Joined: 09 Sep 2014, 18:38

Re: MouseMove in Background/Inactive Window

Post by mikeyww » 30 Nov 2022, 05:47

That is a cool script. I was unaware that it could be done. :clap:

I will be interested to know if this works in the target window as intended!

Post Reply

Return to “Gaming Help (v1)”