Disable mouse input in games

Ask gaming related questions (AHK v1.1 and older)
lophornis
Posts: 8
Joined: 04 Jun 2021, 09:30

Disable mouse input in games

Post by lophornis » 04 Jun 2021, 09:53

Hi can someone help me with this problem? :beard:
When i want to play certain pc games like cuphead and jump force they reveal my mouse and keyboard as first priority, before the controller
this sometimes cause a problem because if i didnt remember to connect the controller first i have to restart the game
also some other games bind my mouse in their windows and to have freedoom i have to alt tab :x ( like dragonball xenoverse 2 )

i tried something with BlockInput but the thing is i dont have to block the mouse in all the pc but onl that game
is there a way to do it? maybe hiding the device?

Thanks :headwall:
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Disable mouse input in games

Post by mikeyww » 04 Jun 2021, 17:36

Code: Select all

Loop {
 WinWaitActive, ahk_exe notepad.exe
 BlockInput, MouseMove
 WinWaitNotActive
 BlockInput, MouseMoveOff
}
lophornis
Posts: 8
Joined: 04 Jun 2021, 09:30

Re: Disable mouse input in games

Post by lophornis » 05 Jun 2021, 09:21

mikeyww wrote:
04 Jun 2021, 17:36

Code: Select all

Loop {
 WinWaitActive, ahk_exe notepad.exe
 BlockInput, MouseMove
 WinWaitNotActive
 BlockInput, MouseMoveOff
}
Yeah i already tried that way, but i was trying to disable mouse moving and input only inside the specific application, with that type of code when the window gets focus it disables the mouse moving for the whole OS until the windows loses focus. What i meant to do is to have the window ignore mouse moving/input as if there was no native support in it.
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Disable mouse input in games

Post by mikeyww » 05 Jun 2021, 09:29

There is ClipCursor. Another approach is to use SetTimer with MouseGetPos to see what window is under the cursor, and act accordingly.

Alternatives:

Code: Select all

#Persistent
SetTimer, Check, 100
Check:
MouseGetPos,,, uid
WinGet, proc, ProcessName, % wTitle := "ahk_id " uid
If (proc != "notepad.exe")
 Return
WinGetPos, x, y,,, %wTitle%
CoordMode, Mouse
MouseMove, x - 5, y - 5
Return

Code: Select all

#Persistent
Loop {
 WinWaitActive, % "ahk_exe " proc := "notepad.exe"
 SetTimer, Check, 100
 WinWaitNotActive
 BlockInput, MouseMoveOff
}
Check:
MouseGetPos,,, uid
WinGet, thisProc, ProcessName, ahk_id %uid%
If (thisProc != proc)
 Return
BlockInput, MouseMove
SetTimer,, Off
Return
lophornis
Posts: 8
Joined: 04 Jun 2021, 09:30

Re: Disable mouse input in games

Post by lophornis » 10 Jun 2021, 12:11

mikeyww wrote:
05 Jun 2021, 09:29
There is ClipCursor. Another approach is to use SetTimer with MouseGetPos to see what window is under the cursor, and act accordingly.

Alternatives:

Code: Select all

#Persistent
SetTimer, Check, 100
Check:
MouseGetPos,,, uid
WinGet, proc, ProcessName, % wTitle := "ahk_id " uid
If (proc != "notepad.exe")
 Return
WinGetPos, x, y,,, %wTitle%
CoordMode, Mouse
MouseMove, x - 5, y - 5
Return

Code: Select all

#Persistent
Loop {
 WinWaitActive, % "ahk_exe " proc := "notepad.exe"
 SetTimer, Check, 100
 WinWaitNotActive
 BlockInput, MouseMoveOff
}
Check:
MouseGetPos,,, uid
WinGet, thisProc, ProcessName, ahk_id %uid%
If (thisProc != proc)
 Return
BlockInput, MouseMove
SetTimer,, Off
Return
i tried all of them but still my mouse is blocked somehow :?
the only way would be rewrite the game code i guess or modding
thanks
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Disable mouse input in games

Post by mikeyww » 10 Jun 2021, 16:01

In a sense, these are simulations. One will prevent the mouse from moving over the window. The other will block mouse input when the window becomes active and the mouse is over the window. As far as I can tell, this fits the description, "have the window ignore mouse moving/input". If you instead just want the mouse buttons not to work, they can be set to Return when the target window is active.
lophornis
Posts: 8
Joined: 04 Jun 2021, 09:30

Re: Disable mouse input in games

Post by lophornis » 13 Jun 2021, 16:04

mikeyww wrote:
10 Jun 2021, 16:01
In a sense, these are simulations. One will prevent the mouse from moving over the window. The other will block mouse input when the window becomes active and the mouse is over the window. As far as I can tell, this fits the description, "have the window ignore mouse moving/input". If you instead just want the mouse buttons not to work, they can be set to Return when the target window is active.
i only want to make the game ignore mouse and keyboard while also keeping the game window active but only for that game not all the pc
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Disable mouse input in games

Post by mikeyww » 13 Jun 2021, 18:19

I am finding a lack of clarity here. Your reports:

1. "i dont have to block the mouse in all the pc but onl that game"; followed by:
2. "but still my mouse is blocked somehow"; followed by:
3. "make the game ignore mouse and keyboard while also keeping the game window active but only for that game not all the pc"

At this point, perhaps you can describe exactly what should happen to the mouse, and exactly what event should trigger that.

To try yourself: the scripts here show how you can detect when your target window is active (WinWaitActive), and then act following that. See BlockInput for an explanation of how the block works. The scripts here also show how to determine which window is under the mouse (MouseGetPos), so you can adjust that if needed. Note that some uses of BlockInput may require admin mode. Links are provided in this paragraph.

In case helpful, here is a way to block mouse buttons.

Code: Select all

#If mouseOver("notepad")
LButton::
RButton::
MButton::
XButton1::
XButton2::SoundBeep, 1000
#If

mouseOver(proc) {
 MouseGetPos,,, uid
 WinGet, thisProc, ProcessName, ahk_id %uid%
 Return thisProc = proc ".exe"
}
Here is an interesting script to block the keyboard. You could probably integrate this into your script.

That's about all I know with these various techniques. You have a bunch of examples here. You can pick the ones that you want and use them in your script. It seems to me that an easy one is the first one that simply prevents the mouse from hovering anywhere over your target window. I think that would meet your description for the script. If you want to block all keyboard input, then you can simply make the window inactive as soon as it becomes active.
lophornis
Posts: 8
Joined: 04 Jun 2021, 09:30

Re: Disable mouse input in games

Post by lophornis » 16 Jun 2021, 15:42

mikeyww wrote:
13 Jun 2021, 18:19

Code: Select all

#If mouseOver("notepad")
LButton::
RButton::
MButton::
XButton1::
XButton2::SoundBeep, 1000
#If

mouseOver(proc) {
 MouseGetPos,,, uid
 WinGet, thisProc, ProcessName, ahk_id %uid%
 Return thisProc = proc ".exe"
}
:thumbup:

THIS is actually what i need and seems too works but only for a few games (game title can be tricky i guess)

like it works with cuphead

Code: Select all

#If mouseOver("Cuphead")
LButton::
RButton::
MButton::
XButton1::
XButton2::SoundBeep, 100
#If

mouseOver(proc) {
 MouseGetPos,,, uid
 WinGet, thisProc, ProcessName, ahk_id %uid%
 Return thisProc = proc ".exe"
}
the mouse cant click and its free, i can move it allright thanks
well doesnt prevent games from "capturing" the mouse like again db xenoverse 2 but its a start :think:
then what if i wanna also add the keyboard block?

BlockInput seems too much as i cant use keyboard for other things
i tried

#If keyboardOver("Cuphead")
EscButton::

give an error on every button or even "key", what should i use? :geek:
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Disable mouse input in games

Post by mikeyww » 16 Jun 2021, 16:28

OK. This particular script handles specified process names. You could change the function if you need window titles instead.

Perhaps you just want to disable the window, easy to do.

Code: Select all

WinSet, Disable,, ahk_exe notepad.exe
The name of Esc is Esc. See: list of keys.
lophornis
Posts: 8
Joined: 04 Jun 2021, 09:30

Re: Disable mouse input in games

Post by lophornis » 16 Jun 2021, 17:24

mikeyww wrote:
16 Jun 2021, 16:28
OK. This particular script handles specified process names. You could change the function if you need window titles instead.

Perhaps you just want to disable the window, easy to do.

Code: Select all

WinSet, Disable,, ahk_exe notepad.exe
The name of Esc is Esc. See: list of keys.
ooh thats why :?
disable the window no because i need to at least play with controller, i only have to disable mouse and keyboard not all
ok the esc now works and all the others too!
thanks for the patience gonna try many games
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Disable mouse input in games

Post by mikeyww » 16 Jun 2021, 17:29

OK. You can always try the "Block the keyboard" linked code. I have not tested it (and it might really block your keyboard, too!). You could try it-- when your target window is active-- but leave one key enabled so that you can re-enable your keyboard when needed. Example is below.

Code: Select all

Loop {
 WinWaitActive, % wTitle := "ahk_exe notepad.exe"
 SoundBeep, 1500
 blockKB("On")
 WinWaitNotActive
 SoundBeep, 1500
 blockKB("Off")
 SoundBeep, 1000
}

F3::WinMinimize, %wTitle%

blockKB(state) {
 ; https://stackoverflow.com/questions/61690600/how-to-make-blockinput-on-off-only-block-keyboard-inputs-not-mouse
 Loop, 512
  Hotkey, % Format("*SC{:X}", A_Index), KeyboardKey, %state% UseErrorLevel
 Hotkey, *SC3D, Off ; F3
 KeyboardKey:
 Return
}
lophornis
Posts: 8
Joined: 04 Jun 2021, 09:30

Re: Disable mouse input in games

Post by lophornis » 16 Jun 2021, 17:55

mikeyww wrote:
16 Jun 2021, 17:29
OK. You can always try the "Block the keyboard" linked code. I have not tested it (and it might really block your keyboard, too!). You could try it-- when your target window is active-- but leave one key enabled so that you can re-enable your keyboard when needed. Example is below.

Code: Select all

Loop {
 WinWaitActive, % wTitle := "ahk_exe notepad.exe"
 SoundBeep, 1500
 blockKB("On")
 WinWaitNotActive
 SoundBeep, 1500
 blockKB("Off")
 SoundBeep, 1000
}

F3::WinMinimize, %wTitle%

blockKB(state) {
 ; https://stackoverflow.com/questions/61690600/how-to-make-blockinput-on-off-only-block-keyboard-inputs-not-mouse
 Loop, 512
  Hotkey, % Format("*SC{:X}", A_Index), KeyboardKey, %state% UseErrorLevel
 Hotkey, *SC3D, Off ; F3
 KeyboardKey:
 Return
}
well its similar but more problematic, cant even alt tab :shock:
the only way would be CTRL ALT CANC
good thing i have the mouse free ahah
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Disable mouse input in games

Post by mikeyww » 16 Jun 2021, 18:23

Well, you did say block the keyboard..... :)

But F3 was left alone to minimize the target window.
lophornis
Posts: 8
Joined: 04 Jun 2021, 09:30

Re: Disable mouse input in games

Post by lophornis » 16 Jun 2021, 18:28

mikeyww wrote:
16 Jun 2021, 18:23
Well, you did say block the keyboard..... :)

But F3 was left alone to minimize the target window.
i noticed f3 just now :lol:
Post Reply

Return to “Gaming Help (v1)”