Tuesday, June 27, 2017

AutoHotKey - Control Your Mouse With Your Keyboard

     A month ago I bought a 40% keyboard. Some people might be wondering what a 40% keyboard is, it is just what you would think it is, it is 40% of the size of the standard 105 key layout. All the keys that have been removed are now stored in programmable layers. so for instance to make an exclamation mark I have to hold down, Fn1 + right shift + Capslock, this might seem insane but it is actually very intuitive after a week or so and now that I have fully adjusted I can hit any key without moving my fingers from the home row. Good-bye wrist pain!

Vortex Core

To be completely honest I did not think I would like using a 40% keyboard, especially as a daily driver. But after a week or two I found myself enjoying typing like seriously, typing is now fun and enjoyable.

Vortex Core

Alright now that we have gotten that out of the way, the point of this post is to share an AutoHotKey script that I have been running which allows me to control my mouse without actually having a mouse!

This script was developed using the AutoHotKey programming language and was aimed towards a 40% keyboard but with some slight modifications, it can work on any keyboard. Please review the comments and adjust accordingly for a full-size keyboard.

Here's the script in all its glory!
; MouseControl.ahk
; Hold down Control and then use the arrow keys to move the mouse cursor around the screen
; Updated on June 22nd, 2017 by Matt Kerfoot.
; Updated on June 27nd, 2017
; www.TheOvernightAdmin.com

#Persistent ; Keeps the script permanently running (that is, until the user closes it or ExitApp is encountered)

SetDefaultMouseSpeed, 1 ; Mouse speed, kind of like reaction time I do believe
distance = 30        ; how far the mouse moves each turn of the timer
multiplier = 1.16    ; how much farther (exponentially) the mouse moves in a direction the longer you hold that direction down
CFKM = 30            ; how often to run the timer
SetTimer, CheckForKeyMouse, %CFKM% ; Controls mouse movement speed
return ; end of mouse control movement control function

CheckForKeyMouse: ; Beginning of Function statement
if not GetKeyState("Control") ; Requires Ctrl to be held down
return ; required otherwise down control will not work

GetKeyState("Down")  ? (d*=multiplier) : (d:=1) ; While holding down shift, the down arrow will allow you to move the mouse to the down
GetKeyState("Up")    ? (u*=multiplier) : (u:=1) ; While holding down shift, the up arrow will allow you to move the mouse to the up
GetKeyState("Right") ? (r*=multiplier) : (r:=1) ; While holding down shift, the right arrow will allow you to move the mouse to the down
GetKeyState("Left")  ? (l*=multiplier) : (l:=1) ; While holding down shift, the left arrow will allow you to move the mouse to the left

y := (d-u) * distance ; math to tell how far to move the cursor
x := (r-l) * distance ; more math to define how far the cursor moves per key press

MouseMove, x, y, , R ; Initiates the action of moving the mouse
^PgDn::Click,      right ; Sets Ctrl + PgDn to perform a right click
^BackSpace::Click, right ; Sets Ctrl + backspace to perform a left click
^PgUp::Click,      left  ; Sets Ctrl + PgUp to perform a left click
^Esc::Click,       left  ; Sets Ctrl + Escape to perform a left click
^r::Reload  ; Assign Ctrl-R as a hotkey to restart the script.
Return ; End of Function statement

 I hope you enjoy it as much as I do! Any questions, just ask!

-Matt Kerfoot