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.
MouseMove in Background/Inactive Window
Re: MouseMove in Background/Inactive Window
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.
-
- Posts: 3
- Joined: 25 Nov 2022, 12:58
Re: MouseMove in Background/Inactive Window
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.
Re: MouseMove in Background/Inactive Window
Fair enough!
Re: MouseMove in Background/Inactive Window
Hallo,
try: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.
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
}
Last edited by Rohwedder on 30 Nov 2022, 11:26, edited 2 times in total.
Re: MouseMove in Background/Inactive Window
That is a cool script. I was unaware that it could be done.
I will be interested to know if this works in the target window as intended!
I will be interested to know if this works in the target window as intended!