Mouse centred when not holding left click and moveable when holding left click down

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MatthewTheBeginner

Mouse centred when not holding left click and moveable when holding left click down

24 Feb 2018, 10:38

I'm a complete beginner when it comes to AutoHotkey and I'm wondering if there's any way to create a script that centres the mouse to the middle of the screen when you're not holding down left click, but lets you move it when holding it down. I also want this script to only work when I'm using the Firefox browser application.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Mouse centred when not holding left click and moveable when holding left click down

24 Feb 2018, 11:00

Code: Select all

CoordMode, Mouse, Screen
CenterActive := 1
SetTimer, CenterMouse, 50
return

CenterMouse:
	if CenterActive && WinActive("ahk_exe firefox.exe")
		MouseMove, A_ScreenWidth/2, A_ScreenHeight/2, 0
return

#IfWinActive, ahk_exe firefox.exe
LButton::
	CenterActive := 0
	KeyWait, LButton
	CenterActive := 1
return
#If

Esc::ExitApp
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Mouse centred when not holding left click and moveable when holding left click down

24 Feb 2018, 11:08

boiler wrote:

Code: Select all

CoordMode, Mouse, Screen
CenterActive := 1
SetTimer, CenterMouse, 50
return

CenterMouse:
	if CenterActive && WinActive("ahk_exe firefox.exe")
		MouseMove, A_ScreenWidth/2, A_ScreenHeight/2, 0
return

#IfWinActive, ahk_exe firefox.exe
LButton::
	CenterActive := 0
	KeyWait, LButton
	CenterActive := 1
return
#If

Esc::ExitApp
Nice! Btw can you please explain this one line?
if CenterActive && WinActive("ahk_exe firefox.exe")
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Mouse centred when not holding left click and moveable when holding left click down

24 Feb 2018, 11:13

It means that it will only do what follows (the MouseMove) if the value of the variable CenterActive is true (or anything that is not false, like 1) and that FireFox is the active window.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Mouse centred when not holding left click and moveable when holding left click down

24 Feb 2018, 11:15

boiler wrote:It means that it will only do what follows (the MouseMove) if the value of the variable CenterActive is true (or anything that is not false, like 1) and that FireFox is the active window.
so 'if *variable* &'
means if variable is not false?

and the second & is and?

Or does variable &&
mean if variable is not false?
I am your average ahk newbie. Just.. a tat more cute. ;)
MatthewTheBeginner

Re: Mouse centred when not holding left click and moveable when holding left click down

24 Feb 2018, 11:23

boiler wrote:

Code: Select all

CoordMode, Mouse, Screen
CenterActive := 1
SetTimer, CenterMouse, 50
return

CenterMouse:
	if CenterActive && WinActive("ahk_exe firefox.exe")
		MouseMove, A_ScreenWidth/2, A_ScreenHeight/2, 0
return

#IfWinActive, ahk_exe firefox.exe
LButton::
	CenterActive := 0
	KeyWait, LButton
	CenterActive := 1
return
#If

Esc::ExitApp
Thank you so much, it's exactly what I need. However, I can't seem to click or select anything in the browser when using this script, is there any solution to this problem?
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Mouse centred when not holding left click and moveable when holding left click down

24 Feb 2018, 12:08

Nwb wrote:so 'if *variable* &'
means if variable is not false?

and the second & is and?

Or does variable &&
mean if variable is not false?
if variable evaluates variable itself to see if it's true. A variable value of 0, False, or "" (null string) is false. Anything else is true. I used 1 to represent true, which is standard.

&& means "and" so both parts have to be true -- the varible has to be true and the WinActive has to be true.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Mouse centred when not holding left click and moveable when holding left click down

24 Feb 2018, 12:09

MatthewTheBeginner wrote:Thank you so much, it's exactly what I need. However, I can't seem to click or select anything in the browser when using this script, is there any solution to this problem?
I don't see how you can expect to select or click on anything if you need to hold the mouse button down just to be able to move the mouse, which is what you wanted. Perhaps you didn't think through how this would even be usable.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Mouse centred when not holding left click and moveable when holding left click down

25 Feb 2018, 06:16

As said, I don't think it is possible not without another key intervention. Try this:

Code: Select all

CoordMode, Mouse, Screen
CenterActive := 1
SetTimer, CenterMouse, 50
return

CenterMouse:
	if CenterActive && WinActive("ahk_exe firefox.exe")
		MouseMove, A_ScreenWidth/2, A_ScreenHeight/2, 0
return

#IfWinActive, ahk_exe firefox.exe
LButton::
	CenterActive := 0
	KeyWait, LButton
	CenterActive := 1
return
#If

f:: ; change to preferred hotkey
Pause, Toggle 
return

Esc::ExitApp
f pauses the script. You can select whatever you want as the hotkey instead of f. Click f to stop the mouse from being in the center, allowing you to move it, and click f again so the mouse goes back to the center.

Is that what you wanted?
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Mouse centred when not holding left click and moveable when holding left click down

25 Feb 2018, 06:23

boiler wrote:
Nwb wrote:so 'if *variable* &'
means if variable is not false?

and the second & is and?

Or does variable &&
mean if variable is not false?
if variable evaluates variable itself to see if it's true. A variable value of 0, False, or "" (null string) is false. Anything else is true. I used 1 to represent true, which is standard.

&& means "and" so both parts have to be true -- the varible has to be true and the WinActive has to be true.
Thank you soooooooooo much! I didn't know if *variable* was that. NOW IT ALL MAKES SENSE HEHE :dance: I was trying so hard to understand how toggling and all of that works and now it's all so clear. Can't thank you enough! :dance:
I am your average ahk newbie. Just.. a tat more cute. ;)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: scriptor2016 and 339 guests