Using the arrow keys to control mouse: ignored cursor keys? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
submeg
Posts: 91
Joined: 14 Apr 2017, 20:39
GitHub: submeg

Using the arrow keys to control mouse: ignored cursor keys?

09 Aug 2020, 15:28

Hi all,

I found this script on the old AHK forums, and it works well. The only issue I noticed is that in certain programs, the up and down cursor keys become non-responsive.

For example, when I'm in OneNote, only the up and down arrow keys stop responding, but the left and right keys remain functional? Any ideas as to why? I can get around this by toggling the script on and off in my main script, but I'd rather just run it all as one, so I can use it whenever I need.

Code: Select all


SetBatchLines -1
#UseHook
Increment = 1 ; number of pixels to move mouse....gets multiplied depending on keypress length
MouseDelay = 0

Left::
Right::
Up::
Down::

xVal=
yVal=
If GetKeyState("CapsLock","T")
	{
		IncrementValue := Increment ; Set the Increment value (we change it)
		; Infinite loop....breaks when key not pressed anymore
		Loop, 
		{
		If (A_Index > IncrementValue * 15) and (IncrementValue < Increment * 5) ; Increase the Increment value depending on how long we held down the key
			IncrementValue := IncrementValue * 2
		If GetKeyState("Down", "P")
			yVal := IncrementValue
		Else If GetKeyState("Up", "P")
			yVal := -IncrementValue
		If !yVal
			yVal := 0
		If GetKeyState("Left", "P")
			xVal := -IncrementValue
		Else If GetKeyState("Right", "P")
			xVal := IncrementValue
		If !xVal
			xVal := 0
		If GetKeyState(A_ThisHotKey, "P") ; Make sure we are still pressing the key
			MouseMove, %xVal%, %yVal%,%MouseDelay%,R
		Else ; we're not pressing the key...break the loop
			Break
		}
	}
Else
	Send % "{" . A_ThisHotKey . "}"
return

Esc::ExitApp

____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
boiler
Posts: 5919
Joined: 21 Dec 2014, 02:44

Re: Using the arrow keys to control mouse: ignored cursor keys?  Topic is solved

09 Aug 2020, 15:41

I don't know if this will solve the issue, but it might. This generally is the better way to go anyway, which is to use an #If directive so that the hotkey is not even triggered at all if Caps mode isn't active rather than to use an if/else inside the subroutine and send the key in the else branch.

Code: Select all

SetBatchLines -1
#UseHook
Increment = 1 ; number of pixels to move mouse....gets multiplied depending on keypress length
MouseDelay = 0

#If GetKeyState("CapsLock","T")
Left::
Right::
Up::
Down::

xVal=
yVal=
		IncrementValue := Increment ; Set the Increment value (we change it)
		; Infinite loop....breaks when key not pressed anymore
		Loop, 
		{
		If (A_Index > IncrementValue * 15) and (IncrementValue < Increment * 5) ; Increase the Increment value depending on how long we held down the key
			IncrementValue := IncrementValue * 2
		If GetKeyState("Down", "P")
			yVal := IncrementValue
		Else If GetKeyState("Up", "P")
			yVal := -IncrementValue
		If !yVal
			yVal := 0
		If GetKeyState("Left", "P")
			xVal := -IncrementValue
		Else If GetKeyState("Right", "P")
			xVal := IncrementValue
		If !xVal
			xVal := 0
		If GetKeyState(A_ThisHotKey, "P") ; Make sure we are still pressing the key
			MouseMove, %xVal%, %yVal%,%MouseDelay%,R
		Else ; we're not pressing the key...break the loop
			Break
		}
return

Esc::ExitApp
User avatar
submeg
Posts: 91
Joined: 14 Apr 2017, 20:39
GitHub: submeg

Re: Using the arrow keys to control mouse: ignored cursor keys?

09 Aug 2020, 16:36

Good thinking!

I tried

Code: Select all

#Else 
{
	
	Send % "{" . A_ThisHotKey . "}"
}
return
But it doesn't like that. If I tell it to run when CapsLock is off:

Code: Select all

#Else If GetKeyState("CapsLock","F")
Also doesn't like that...I've used the #If method for active programs before, but not key states.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
boiler
Posts: 5919
Joined: 21 Dec 2014, 02:44

Re: Using the arrow keys to control mouse: ignored cursor keys?

09 Aug 2020, 16:46

#Else is not a valid directive. You can't just put # in front of other things. It doesn't work that way. It needs to be #If, and it needs to be before the hotkey, not within the code that follows it. Did you try the code as I posted it? It works in general. The question is whether it will solve your issue with your specific programs that weren't working well with your original code.
User avatar
submeg
Posts: 91
Joined: 14 Apr 2017, 20:39
GitHub: submeg

Re: Using the arrow keys to control mouse: ignored cursor keys?

09 Aug 2020, 16:54

boiler wrote:
09 Aug 2020, 16:46
It needs to be #If, and it needs to be before the hotkey, not within the code that follows it.
Sorry @boiler, I didn't realise you had made changes. Apologies.

Yes indeed it works, now that makes more sense! After seeing where you moved the hotkeys to, it makes perfect sense. Far out, so simple; no conflicts with other programs now.

Absolute champion!
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], GreatGreen, Peaceful, Xtra and 44 guests