r/AutoHotkey•
TheFML

Script to make mouse movement induce "arrow keys" movement

Hi,

​

I am trying to make a script such that when I push a key (let's say "A" for the sake of simplicity), moving the mouse does the same as using the directional arrow keys. The goal is to move the screen without having to use arrow keys.

​

Is this feasible? I feel like I do not know the correct keywords to search for a solution.

​

Thanks for your help! Any tips are welcome :)

Posted Jul 22, 2019, 10:52 AM UTC

37
Ark565•

You'll need a toggle on "A" controlling MouseGetPos on a Timer to detect mouse movement "gestures" within some predefined limits which would send the appropriate directional key. I'm on mobile so I'll have a go later at some code.

2
evilC_UK•

There are two ways of interpreting mouse input...

Cursor (ABSOLUTE)

This reads coordinates of the cursor when you press A, the current position of the cursor will be recorded, and each time around a loop, if the cursor is now to the right of that position, the right arrow will be held. In order to release right arrow, you would need to move the cursor back to where it started. To hold left arrow, you would need to move the mouse cursor back past the original point and then left. If you cannot see the cursor, this will likely be confusing. This technique will also not work if the cursor is at the edge of the screen (ie if you press A while cursor is at right edge of screen, you would not be able to trigger right arrow)

Mouse Raw Data (RELATIVE)

In order to keep right arrow held, you would need to keep moving the mouse right. If you stop, right arrow would be released. If you move left, left arrow would instantly be held.

​

For a demo script that uses relative mode, see https://www.autohotkey.com/boards/viewtopic.php?t=10159

Sample script 2

On/off key defaults to F12, just change it to A, but otherwise the defaults do exactly what you want

2
TheMagicalCarrot•

Dude, thanks a lot. The link you provided is actually super helpful for some of the things I've been working on. Sweet.

1
TheMagicalCarrot•

This seems super useful, thanks for the link.

1
TheFMLOP•

Let me add one little thing: it's possible in my application to use 2 arrow keys to go diagonally. I feel like that's something I could easily replicate, for example by disecting the space in 8 rather 4 (which correspond to the 8 "possible" combinations that make sense, for example LEFT + RIGHT isn't a useful combination).

​

Regarding evilC_UK's very useful comment: the relative part seems relevant to me. Once the first movement has been registered, an adjustement should indeed be *relative* to the new position.

​

Small edit: is it possible to do so while "filtering out" all the mouse movement, meaning, the cursor stays exactly where it is?

1
TheMagicalCarrot•

Well, you could just check the mouse position when you start and then repeatedly move the mouse back to that position while the script loops.

You can probably hide the mouse while doing that as well... maybe.

1
grilledbacon32•

A::Right

Pretty simple, should be able to just do the above.

Reference this page for learning how to code for each key: https://www.autohotkey.com/docs/KeyList.htm

Sorry, on mobile so formatting might look bad.

0