Blockinput, mousemove fix or alternative

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Blockinput, mousemove fix or alternative

Post by hemsith14_ » 12 Aug 2022, 00:02

Hey
How do I get the following function to also block the x axis in addition to y (both of them)?
I know about blockinputs, but still.

Thanks!

Code: Select all

ClipCursor(x := "", y := "", w := "", h := "") {
   if p := x != "" {
      VarSetCapacity(RECT, 16, 0), p := &RECT
      for k, v in [x, y, x + w, y + h]
         NumPut(v, p + 4*(k - 1), "Int")
   }
   DllCall("ClipCursor", "Ptr", p)
}
Last edited by hemsith14_ on 15 Aug 2022, 16:07, edited 1 time in total.

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

Re: Clip cursor quesiton

Post by Rohwedder » 12 Aug 2022, 01:49

Hallo,
try:

Code: Select all

q:: ;On/Off
CoordMode, Mouse, Screen
MouseGetPos, x, y
(Clip:=!Clip)?ClipCursor(x,y,0,0):ClipCursor()
Return
ClipCursor(x := "", y := "", w := "", h := "") {
   if p := x != "" {
      VarSetCapacity(RECT, 16, 0), p := &RECT
      for k, v in [x, y, x + w, y + h]
         NumPut(v, p + 4*(k - 1), "Int")
   }
   DllCall("ClipCursor", "Ptr", p)
}

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Clip cursor quesiton

Post by hemsith14_ » 12 Aug 2022, 09:47

Thank you!

It works almost perfectly, but it still possible to move the mouse a tiny amount to each side. Is there a way to prevent that?

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

Re: Clip cursor quesiton

Post by Rohwedder » 12 Aug 2022, 11:06

Then perhaps?:

Code: Select all

q:: (BlockInputMouse := !BlockInputMouse)
	?hHookMouse := SetWindowsHookEx(WH_MOUSE_LL := 14
	, RegisterCallback("True")):UnhookWindowsHookEx(hHookMouse)
True() {
	Return, True
}SetWindowsHookEx(idHook, pfn) {
	Return DllCall("SetWindowsHookEx", "int", idHook, "Uint"
	, pfn, "Uint", DllCall("GetModuleHandle", "Str"), "Uint", 0)
}UnhookWindowsHookEx(hHook) {
	Return DllCall("UnhookWindowsHookEx", "Uint", hHook)
}
Ingredients extracted from:
teadrinker viewtopic.php?p=271462#p271462

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Clip cursor quesiton

Post by wetware05 » 12 Aug 2022, 11:53

Rohwedder, very effective, too...!!! how do you unlock it from your last script?

(Warning: putting in a way to exit, for example ^F3:: ExitApp, has taken me a long time to remove the execution of the script from memory.)

Reissued:

Ah, already, I had not understood the concept, activate and deactivate it in the same keyboard shortcut. But what if you don't want to use it within a script, and not because it has to be activated/deactivated by a keyboard shortcut?

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Clip cursor quesiton

Post by hemsith14_ » 12 Aug 2022, 14:11

Rohwedder wrote:
12 Aug 2022, 11:06
Then perhaps?:

Code: Select all

q:: (BlockInputMouse := !BlockInputMouse)
	?hHookMouse := SetWindowsHookEx(WH_MOUSE_LL := 14
	, RegisterCallback("True")):UnhookWindowsHookEx(hHookMouse)
True() {
	Return, True
}SetWindowsHookEx(idHook, pfn) {
	Return DllCall("SetWindowsHookEx", "int", idHook, "Uint"
	, pfn, "Uint", DllCall("GetModuleHandle", "Str"), "Uint", 0)
}UnhookWindowsHookEx(hHook) {
	Return DllCall("UnhookWindowsHookEx", "Uint", hHook)
}
Ingredients extracted from:
teadrinker viewtopic.php?p=271462#p271462
Thank you so much!
How can I use it as function like?

Code: Select all

1::
BlockInputMouse := True
KeyWait, 1
BlockInputMouse := False
Return
Also, how can it be used in conjunction with mousemove so it will basically act like blockinput?

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

Re: Clip cursor quesiton

Post by Rohwedder » 13 Aug 2022, 02:07

Then:

Code: Select all

1::
BlockInputMouse(True)
KeyWait, 1
BlockInputMouse(False)
Return

BlockInputMouse(Block)
{ Static hHookMouse
	IF Block And !hHookMouse
		hHookMouse := SetWindowsHookEx(WH_MOUSE_LL := 14, RegisterCallback("True"))
	Else IF !Block And hHookMouse
		UnhookWindowsHookEx(hHookMouse), hHookMouse := False
}True() {
	Return, True
}SetWindowsHookEx(idHook, pfn) {
	Return DllCall("SetWindowsHookEx", "int", idHook, "Uint"
	, pfn, "Uint", DllCall("GetModuleHandle", "Str"), "Uint", 0)
}UnhookWindowsHookEx(hHook) {
	Return DllCall("UnhookWindowsHookEx", "Uint", hHook)
}
However, MouseMoves are also blocked!
I don't drink enough tea to understand what exactly is happening.

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Clip cursor quesiton

Post by hemsith14_ » 13 Aug 2022, 12:14

It's working great but the whole point of this is to block user input during the mouse movements so I need to have it working somehow :crazy:

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

Re: Clip cursor quesiton

Post by Rohwedder » 13 Aug 2022, 13:47

Then perhaps:

Code: Select all

OnExit("UnClip_ExitApp")
Radius := 300, X0 := 500, Y0 := 500
Alpha := 0, 2PI := 8*Atan(1)
SetTimer, Circle, 10
Esc::ExitApp
Circle:
Alpha := Mod(Alpha + .2, 2PI)
MouseMove_Clip(X0+Radius*Cos(Alpha), Y0+Radius*Sin(Alpha))
Return

MouseMove_Clip(X:=0, Y:=0)
{
    VarSetCapacity(I, 16, 0)
    For k, v in [X, Y, X, Y]
        NumPut(v, &I + 4*(k - 1), "Int")
	DllCall("ClipCursor") ;UnClip
    DllCall("ClipCursor", "UInt", &I) ;MouseMove + Clip
}
UnClip_ExitApp()
{
	DllCall("ClipCursor")
	ExitApp
}
End the circling with the Escape key.

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Clip cursor quesiton

Post by hemsith14_ » 13 Aug 2022, 16:16

Rohwedder wrote:
13 Aug 2022, 13:47
Then perhaps:

Code: Select all

OnExit("UnClip_ExitApp")
Radius := 300, X0 := 500, Y0 := 500
Alpha := 0, 2PI := 8*Atan(1)
SetTimer, Circle, 10
Esc::ExitApp
Circle:
Alpha := Mod(Alpha + .2, 2PI)
MouseMove_Clip(X0+Radius*Cos(Alpha), Y0+Radius*Sin(Alpha))
Return

MouseMove_Clip(X:=0, Y:=0)
{
    VarSetCapacity(I, 16, 0)
    For k, v in [X, Y, X, Y]
        NumPut(v, &I + 4*(k - 1), "Int")
	DllCall("ClipCursor") ;UnClip
    DllCall("ClipCursor", "UInt", &I) ;MouseMove + Clip
}
UnClip_ExitApp()
{
	DllCall("ClipCursor")
	ExitApp
}
End the circling with the Escape key.
And this as a temporary hotkey like the one with keywait? :D
Basically, I'm just looking for a blockinput mousemove alternative that will not cause the mouse to move while a mouse movement is in progress. Something like

- Block mouse here
- hotkey here that includes mouse move
- Unblock

P.S I'm using a relative mosuemove
Basically a hotkey like:

Code: Select all

^0::
;Block mouse
Loop 100
MouseMove, 1, 0,, R
;Unblock mouse
Return

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

Re: Clip cursor quesiton

Post by Rohwedder » 14 Aug 2022, 02:48

Debugged and three examples added:

Code: Select all

OnExit("ExitApp")
Esc::ExitApp

^0::
MouseMoveBlock(,, 1) ;Block mouse
ToolTip, mouse blocked
Sleep, 2000
ToolTip, blocked mouse moves
Loop 200
{
	MouseMoveBlock(1, 0, 1) ;Relative MoveBlock X=1,Y=0
	Sleep, 10
}
ToolTip, blocked mouse stops
Sleep, 2000
MouseMoveBlock(,, -1) ;Unblock mouse
ToolTip, mouse unblocked
Sleep, 1000
ToolTip
Return
^1::MouseMoveBlock(500, 500) ;Absolute MoveBlock X=500,Y=500
^2::MouseMoveBlock(,, -1) ;Unblock mouse

MouseMoveBlock(X:=0, Y:=0, Mode:=0)
{ ;Mode: 0,1,-1 : Absolute+Block, Relative+Block, Unblock
	Static X0, Y0, OldMode := 0
	IF (Mode = 1)
	{
		IF (OldMode <> 1)
		{
			CoordMode, Mouse, Screen
			MouseGetPos, X0, Y0
			OldMode := 1
		}
		X0 := X += X0, Y0 := Y += Y0
	}
	Else If (0 > OldMode:=Mode)
	{
		DllCall("ClipCursor", "Ptr", 0)
		Return
	}
	VarSetCapacity(I, 16, 0)
	For k, v in [X, Y, X, Y]
		NumPut(v, &I + 4*(k - 1), "Int")
	DllCall("ClipCursor", "Ptr", 0)
	DllCall("ClipCursor", "UInt", &I)
}
ExitApp()
{
	MouseMoveBlock(,, -1) ;Unblock mouse
	ExitApp
}

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Clip cursor quesiton

Post by hemsith14_ » 14 Aug 2022, 16:00

Thanks so much!!!

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Clip cursor quesiton

Post by hemsith14_ » 14 Aug 2022, 16:32

For some reason the following is too fast in order to stop the loop in time, while with the normal blockinput it worked fine. Any idea on how to fix this?
Using sleep, even if it's just sleep 1, makes it too slow..

Code: Select all

!RButton::
MouseMoveBlock(,, 1) ;Block mouse
Sleep 1
While (A_Cursor = "SizeNS")
MouseMoveBlock(1, 0, 1)
Click, Down
MouseMoveBlock(,, -1) ;Unblock mouse
KeyWait, RButton
Click, Up
Return

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

Re: Clip cursor quesiton

Post by Rohwedder » 15 Aug 2022, 00:26

Then use Moves smaller than 1 e.g. .2

Code: Select all

While (A_Cursor = "SizeNS")
	MouseMoveBlock(.2, 0, 1)
or Sleep and greater Moves:

Code: Select all

While (A_Cursor = "SizeNS")
{
	MouseMoveBlock(20, 0, 1)
	Sleep, 1
}
This many DllCalls in MouseMoveBlock() are not elegant. If somehow possible one should better use BlockInput.
Last edited by Rohwedder on 15 Aug 2022, 00:42, edited 1 time in total.

BoBo
Posts: 6563
Joined: 13 May 2014, 17:15

Re: Clip cursor quesiton

Post by BoBo » 15 Aug 2022, 00:43

@teadrinker :lol:
"I don't drink enough tea to understand what exactly is happening."
https://www.autohotkey.com/boards/viewtopic.php?p=477205#p477205

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Clip cursor quesiton

Post by hemsith14_ » 15 Aug 2022, 00:47

It's still not "catching" the cursor changes like the normal mousemove tend to do. Could you test it and see if you can get the same result as I?

Even better, is there a way to fix the blockinput mousemove? perhaps by starting the script differently or something?

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

Re: Clip cursor quesiton

Post by Rohwedder » 15 Aug 2022, 01:13

See https://www.autohotkey.com/docs/FAQ.htm#uac
UAC may also prevent SendPlay and BlockInput from working.

teadrinker
Posts: 4562
Joined: 29 Mar 2015, 09:41
Contact:

Re: Clip cursor quesiton

Post by teadrinker » 15 Aug 2022, 12:56

@BoBo
@Rohwedder
Guys, you don't have to limit yourself to tea! :lol:

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Clip cursor quesiton

Post by hemsith14_ » 15 Aug 2022, 16:06

Rohwedder wrote:
15 Aug 2022, 01:13
See https://www.autohotkey.com/docs/FAQ.htm#uac
UAC may also prevent SendPlay and BlockInput from working.
Just tried the normal blockinput mousemove with the UAC setting, but it didn't fix it, I'm starting to lose hope regarding this. Why is it happening? Why can blockinput mousemove can just block the mouse move regardless of anything that happens?

Post Reply

Return to “Ask for Help (v1)”