Mouse and keyboard macros and hotkeys.

Home | Download | Documentation | Changelog | Support | Forum

Remapping a Joystick to Keyboard or Mouse

Table of Contents

Important Notes

Making a Joystick Button Send Keystrokes or Mouse Clicks

Below are three approaches, starting at the simplest and ending with the most complex. The most complex method works in the broadest variety of circumstances (such as games that require a key or mouse button to be held down).

Method #1: This method sends simple keystrokes and mouse clicks. For example:

Joy1::Send {Left}  ; Have button #1 send a left-arrow keystroke.
Joy2::Send {LButton}  ; Have button #2 send a mouse left-click.
Joy3::Send a{Esc}{Space}{Enter}  ; Have button #3 send the letter "a" followed by Escape, Space, and Enter.
Joy4::Send Sincerely,{Enter}John Smith  ; Have button #4 send a two-line signature.

To have a button perform more than one command, put the first command beneath the button name and make the last command a return. For example:

Joy5::
Run Notepad
WinWait Untitled - Notepad
WinActivate
Send This is the text that will appear in Notepad.{Enter}
return


Method #2
: This method is necessary in cases where a key or mouse button must be held down for the entire time that you're holding down a joystick button. The following example makes the joystick's second button become the left-arrow key:

Joy2::
Send {Left down}  ; Hold down the left-arrow key.
KeyWait Joy2  ; Wait for the user to release the joystick button.
Send {Left up}  ; Release the left-arrow key.
return


Method #3: This method is necessary in cases where you have more than one joystick hotkey of the type described in Method #2, and you sometimes press and release such hotkeys simultaneously. The following example makes the joystick's third button become the left mouse button:

Joy3::
Send {LButton down}   ; Hold down the left mouse button.
SetTimer, WaitForButtonUp3, 10
return

WaitForButtonUp3:
if GetKeyState("Joy3")  ; The button is still, down, so keep waiting.
	return
; Otherwise, the button has been released.
Send {LButton up}  ; Release the left mouse button.
SetTimer, WaitForButtonUp3, off
return


Using a Joystick as a Mouse
: The Joystick-To-Mouse script converts a joystick into a mouse by remapping its buttons and axis control.

Making a Joystick Axis or POV Hat Send Keystrokes or Mouse Clicks

To have a script respond to movement of a joystick's axis or POV hat, use SetTimer and GetKeyState. The following example makes the joystick's X and Y axes behave like the arrow key cluster on a keyboard (left, right, up, and down):

#Persistent  ; Keep this script running until the user explicitly exits it.
SetTimer, WatchAxis, 5
return

WatchAxis:
GetKeyState, JoyX, JoyX  ; Get position of X axis.
GetKeyState, JoyY, JoyY  ; Get position of Y axis.
KeyToHoldDownPrev = %KeyToHoldDown%  ; Prev now holds the key that was down before (if any).

if JoyX > 70
	KeyToHoldDown = Right
else if JoyX < 30
	KeyToHoldDown = Left
else if JoyY > 70
	KeyToHoldDown = Down
else if JoyY < 30
	KeyToHoldDown = Up
else
	KeyToHoldDown =

if KeyToHoldDown = %KeyToHoldDownPrev%  ; The correct key is already down.
	return  ; Do nothing.

; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1  ; Avoid delays between keystrokes.
if KeyToHoldDownPrev   ; There is a previous key to release.
	Send, {%KeyToHoldDownPrev% up}  ; Release it.
if KeyToHoldDown   ; There is a key to press down.
	Send, {%KeyToHoldDown% down}  ; Press it down.
return

The following example makes the joystick's POV hat behave like the arrow key cluster on a keyboard; that is, the POV hat will send arrow keystrokes (left, right, up, and down):

#Persistent  ; Keep this script running until the user explicitly exits it.
SetTimer, WatchPOV, 5
return

WatchPOV:
GetKeyState, POV, JoyPOV  ; Get position of the POV control.
KeyToHoldDownPrev = %KeyToHoldDown%  ; Prev now holds the key that was down before (if any).

; Some joysticks might have a smooth/continous POV rather than one in fixed increments.
; To support them all, use a range:
if POV < 0   ; No angle to report
	KeyToHoldDown =
else if POV > 31500                 ; 315 to 360 degrees: Forward
	KeyToHoldDown = Up
else if POV between 0 and 4500      ; 0 to 45 degrees: Forward
	KeyToHoldDown = Up
else if POV between 4501 and 13500  ; 45 to 135 degrees: Right
	KeyToHoldDown = Right
else if POV between 13501 and 22500 ; 135 to 225 degrees: Down
	KeyToHoldDown = Down
else                                ; 225 to 315 degrees: Left
	KeyToHoldDown = Left

if KeyToHoldDown = %KeyToHoldDownPrev%  ; The correct key is already down.
	return  ; Do nothing.

; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1  ; Avoid delays between keystrokes.
if KeyToHoldDownPrev   ; There is a previous key to release.
	Send, {%KeyToHoldDownPrev% up}  ; Release it.
if KeyToHoldDown   ; There is a key to press down.
	Send, {%KeyToHoldDown% down}  ; Press it down.
return

Remarks

A joystick other than first may be used by preceding the button or axis name with the number of the joystick. For example, 2Joy1 would be the second joystick's first button.

To find other useful joystick scripts, visit the AutoHotkey forum. A keyword search such as Joystick and GetKeyState and Send is likely to produce topics of interest.

Related

Joystick-To-Mouse script
List of joystick buttons, axes, and controls
GetKeyState
Remapping the keyboard and mouse