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::Click ; Have button #2 send a click of left mouse button. 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
See the Key List for the complete list of keys and mouse/joystick buttons.
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
Auto-repeating a keystroke: Some programs or games might require a key to be sent repeatedly (as though you are holding it down on the keyboard). The following example achieves this by sending spacebar keystrokes repeatedly while you hold down the joystick's second button:
Joy2:: Send {Space down} ; Press the spacebar down. SetTimer, WaitForJoy2, 30 ; Reduce the number 30 to 20 or 10 to send keys faster. Increase it to send slower. return WaitForJoy2: if not GetKeyState("Joy2") ; The button has been released. { Send {Space up} ; Release the spacebar. SetTimer, WaitForJoy2, off ; Stop monitoring the button. return } ; Since above didn't "return", the button is still being held down. Send {Space down} ; Send another Spacebar keystroke. return
Context-sensitive Joystick Buttons: The directives #IfWinActive/Exist can be used to make selected joystick buttons perform a different action (or none at all) depending on the type of window that is active or exists.
Using a Joystick as a Mouse: The Joystick-To-Mouse script converts a joystick into a mouse by remapping its buttons and axis control.
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 (or no key is needed). 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 (or no key is needed). 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
Auto-repeating a keystroke: Both examples above can be modified to send the key repeatedly rather than merely holding it down (that is, they can mimic physically holding down a key on the keyboard). To do this, replace the following line:
return ; Do nothing.
WITH:
{ if KeyToHoldDown Send, {%KeyToHoldDown% down} ; Auto-repeat the keystroke. return }
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.
Joystick-To-Mouse script (using a joystick as a mouse)
List of joystick buttons, axes, and controls
GetKeyState
Remapping the keyboard and mouse