Posted July 13, 2019
Hello,
you can use AutoHotKey (an Open Source software under GNU GPLv2 license) to convert joystick/gamepad input into keyboard keys. And it works.
You must download the AutoHotKey application from the www.autohotkey.com website.
Here below is a script that works for me with one gamepad.
With the AutoHotKey application you can compile or run directly the script below.
You can modify the script if you like.
;----------------------Begin of the script----------------------------------------------------------------------------------------
#Persistent ; Keep this script running until the user explicitly exits it.
SetTimer, WatchJoystick, 5
return
WatchJoystick:
; Direction
JoyX := GetKeyState("JoyX") ; Get position of X axis.
JoyY := GetKeyState("JoyY") ; Get position of Y axis.
KeyXToHoldDownPrev := KeyXToHoldDown ; Prev now holds the key that was down before (if any).
KeyYToHoldDownPrev := KeyYToHoldDown ; Prev now holds the key that was down before (if any).
; POV
JoyPOV := GetKeyState("JoyPOV") ; Get position of the POV control.
KeyPOVToHoldDownPrev := KeyPOVToHoldDown ; Prev now holds the key that was down before (if any).
; Joy1 to Joy10
KJoy1 := GetKeyState("Joy1") ; Space
KJoy2 := GetKeyState("Joy2") ; c
KJoy3 := GetKeyState("Joy3") ; z
KJoy4 := GetKeyState("Joy4") ; LShift
KJoy5 := GetKeyState("Joy5") ; Control
KJoy6 := GetKeyState("Joy6") ; Backspace
KJoy9 := GetKeyState("Joy9") ; Enter
KJoy10 := GetKeyState("Joy10") ; Tab
Key1ToHoldDownPrev := Key1ToHoldDown
Key2ToHoldDownPrev := Key2ToHoldDown
Key3ToHoldDownPrev := Key3ToHoldDown
Key4ToHoldDownPrev := Key4ToHoldDown
Key5ToHoldDownPrev := Key5ToHoldDown
Key6ToHoldDownPrev := Key6ToHoldDown
Key9ToHoldDownPrev := Key9ToHoldDown
Key10ToHoldDownPrev := Key10ToHoldDown
; Direction
if (JoyX > 70)
KeyXToHoldDown := "Right"
else if (JoyX < 30)
KeyXToHoldDown := "Left"
else
KeyXToHoldDown = ""
if (JoyY > 70)
KeyYToHoldDown := "Down"
else if (JoyY < 30)
KeyYToHoldDown := "Up"
else
KeyYToHoldDown := ""
if (KeyXToHoldDown = KeyXToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if KeyXToHoldDownPrev ; There is a previous key to release.
Send, {%KeyXToHoldDownPrev% up} ; Release it.
if KeyXToHoldDown ; There is a key to press down.
Send, {%KeyXToHoldDown% down} ; Press it down.
}
if (KeyYToHoldDown = KeyYToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if KeyYToHoldDownPrev ; There is a previous key to release.
Send, {%KeyYToHoldDownPrev% up} ; Release it.
if KeyYToHoldDown ; There is a key to press down.
Send, {%KeyYToHoldDown% down} ; Press it down.
}
; POV
; Some joysticks might have a smooth/continous POV rather than one in fixed increments.
; To support them all, use a range:
if (JoyPOV < 0) ; No angle to report
KeyPOVToHoldDown := ""
else if (JoyPOV > 31500) ; 315 to 360 degrees: Forward
KeyPOVToHoldDown := ""
else if JoyPOV between 0 and 4500 ; 0 to 45 degrees: Forward
KeyPOVToHoldDown := ""
else if JoyPOV between 4501 and 13500 ; 45 to 135 degrees: Right
KeyPOVToHoldDown := "q"
else if JoyPOV between 13501 and 22500 ; 135 to 225 degrees: Down
KeyPOVToHoldDown := ""
else ; 225 to 315 degrees: Left
KeyPOVToHoldDown := "e"
if (KeyPOVToHoldDown = KeyPOVToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if KeyPOVToHoldDownPrev ; There is a previous key to release.
Send, {%KeyPOVToHoldDownPrev% up} ; Release it.
if KeyPOVToHoldDown ; There is a key to press down.
Send, {%KeyPOVToHoldDown% down} ; Press it down.
}
; Joy1
if (KJoy1 > 0)
Key1ToHoldDown := "Space"
else
Key1ToHoldDown := ""
if (Key1ToHoldDown = Key1ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key1ToHoldDownPrev ; There is a previous key to release.
Send, {%Key1ToHoldDownPrev% up} ; Release it.
if Key1ToHoldDown ; There is a key to press down.
Send, {%Key1ToHoldDown% down} ; Press it down.
}
; Joy2
if (KJoy2 > 0)
Key2ToHoldDown := "c"
else
Key2ToHoldDown := ""
if (Key2ToHoldDown = Key2ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key2ToHoldDownPrev ; There is a previous key to release.
Send, {%Key2ToHoldDownPrev% up} ; Release it.
if Key2ToHoldDown ; There is a key to press down.
Send, {%Key2ToHoldDown% down} ; Press it down.
}
; Joy3
if (KJoy3 > 0)
Key3ToHoldDown := "z"
else
Key3ToHoldDown := ""
if (Key3ToHoldDown = Key3ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key3ToHoldDownPrev ; There is a previous key to release.
Send, {%Key3ToHoldDownPrev% up} ; Release it.
if Key3ToHoldDown ; There is a key to press down.
Send, {%Key3ToHoldDown% down} ; Press it down.
}
; Joy4
if (KJoy4 > 0)
Key4ToHoldDown := "LShift"
else
Key4ToHoldDown := ""
if (Key4ToHoldDown = Key4ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key4ToHoldDownPrev ; There is a previous key to release.
Send, {%Key4ToHoldDownPrev% up} ; Release it.
if Key4ToHoldDown ; There is a key to press down.
Send, {%Key4ToHoldDown% down} ; Press it down.
}
; Joy5
if (KJoy5 > 0)
Key5ToHoldDown := "Control"
else
Key5ToHoldDown := ""
if (Key5ToHoldDown = Key5ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key5ToHoldDownPrev ; There is a previous key to release.
Send, {%Key5ToHoldDownPrev% up} ; Release it.
if Key5ToHoldDown ; There is a key to press down.
Send, {%Key5ToHoldDown% down} ; Press it down.
}
; Joy6
if (KJoy6 > 0)
Key6ToHoldDown := "Backspace"
else
Key6ToHoldDown := ""
if (Key6ToHoldDown = Key6ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key6ToHoldDownPrev ; There is a previous key to release.
Send, {%Key6ToHoldDownPrev% up} ; Release it.
if Key6ToHoldDown ; There is a key to press down.
Send, {%Key6ToHoldDown% down} ; Press it down.
}
; Joy9
if (KJoy9 > 0)
Key9ToHoldDown := "Enter"
else
Key9ToHoldDown := ""
if (Key9ToHoldDown = Key9ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key9ToHoldDownPrev ; There is a previous key to release.
Send, {%Key9ToHoldDownPrev% up} ; Release it.
if Key9ToHoldDown ; There is a key to press down.
Send, {%Key9ToHoldDown% down} ; Press it down.
}
; Joy10
if (KJoy10 > 0)
Key10ToHoldDown := "Tab"
else
Key10ToHoldDown := ""
if (Key10ToHoldDown = Key10ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key10ToHoldDownPrev ; There is a previous key to release.
Send, {%Key10ToHoldDownPrev% up} ; Release it.
if Key10ToHoldDown ; There is a key to press down.
Send, {%Key10ToHoldDown% down} ; Press it down.
}
return
you can use AutoHotKey (an Open Source software under GNU GPLv2 license) to convert joystick/gamepad input into keyboard keys. And it works.
You must download the AutoHotKey application from the www.autohotkey.com website.
Here below is a script that works for me with one gamepad.
With the AutoHotKey application you can compile or run directly the script below.
You can modify the script if you like.
;----------------------Begin of the script----------------------------------------------------------------------------------------
#Persistent ; Keep this script running until the user explicitly exits it.
SetTimer, WatchJoystick, 5
return
WatchJoystick:
; Direction
JoyX := GetKeyState("JoyX") ; Get position of X axis.
JoyY := GetKeyState("JoyY") ; Get position of Y axis.
KeyXToHoldDownPrev := KeyXToHoldDown ; Prev now holds the key that was down before (if any).
KeyYToHoldDownPrev := KeyYToHoldDown ; Prev now holds the key that was down before (if any).
; POV
JoyPOV := GetKeyState("JoyPOV") ; Get position of the POV control.
KeyPOVToHoldDownPrev := KeyPOVToHoldDown ; Prev now holds the key that was down before (if any).
; Joy1 to Joy10
KJoy1 := GetKeyState("Joy1") ; Space
KJoy2 := GetKeyState("Joy2") ; c
KJoy3 := GetKeyState("Joy3") ; z
KJoy4 := GetKeyState("Joy4") ; LShift
KJoy5 := GetKeyState("Joy5") ; Control
KJoy6 := GetKeyState("Joy6") ; Backspace
KJoy9 := GetKeyState("Joy9") ; Enter
KJoy10 := GetKeyState("Joy10") ; Tab
Key1ToHoldDownPrev := Key1ToHoldDown
Key2ToHoldDownPrev := Key2ToHoldDown
Key3ToHoldDownPrev := Key3ToHoldDown
Key4ToHoldDownPrev := Key4ToHoldDown
Key5ToHoldDownPrev := Key5ToHoldDown
Key6ToHoldDownPrev := Key6ToHoldDown
Key9ToHoldDownPrev := Key9ToHoldDown
Key10ToHoldDownPrev := Key10ToHoldDown
; Direction
if (JoyX > 70)
KeyXToHoldDown := "Right"
else if (JoyX < 30)
KeyXToHoldDown := "Left"
else
KeyXToHoldDown = ""
if (JoyY > 70)
KeyYToHoldDown := "Down"
else if (JoyY < 30)
KeyYToHoldDown := "Up"
else
KeyYToHoldDown := ""
if (KeyXToHoldDown = KeyXToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if KeyXToHoldDownPrev ; There is a previous key to release.
Send, {%KeyXToHoldDownPrev% up} ; Release it.
if KeyXToHoldDown ; There is a key to press down.
Send, {%KeyXToHoldDown% down} ; Press it down.
}
if (KeyYToHoldDown = KeyYToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if KeyYToHoldDownPrev ; There is a previous key to release.
Send, {%KeyYToHoldDownPrev% up} ; Release it.
if KeyYToHoldDown ; There is a key to press down.
Send, {%KeyYToHoldDown% down} ; Press it down.
}
; POV
; Some joysticks might have a smooth/continous POV rather than one in fixed increments.
; To support them all, use a range:
if (JoyPOV < 0) ; No angle to report
KeyPOVToHoldDown := ""
else if (JoyPOV > 31500) ; 315 to 360 degrees: Forward
KeyPOVToHoldDown := ""
else if JoyPOV between 0 and 4500 ; 0 to 45 degrees: Forward
KeyPOVToHoldDown := ""
else if JoyPOV between 4501 and 13500 ; 45 to 135 degrees: Right
KeyPOVToHoldDown := "q"
else if JoyPOV between 13501 and 22500 ; 135 to 225 degrees: Down
KeyPOVToHoldDown := ""
else ; 225 to 315 degrees: Left
KeyPOVToHoldDown := "e"
if (KeyPOVToHoldDown = KeyPOVToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if KeyPOVToHoldDownPrev ; There is a previous key to release.
Send, {%KeyPOVToHoldDownPrev% up} ; Release it.
if KeyPOVToHoldDown ; There is a key to press down.
Send, {%KeyPOVToHoldDown% down} ; Press it down.
}
; Joy1
if (KJoy1 > 0)
Key1ToHoldDown := "Space"
else
Key1ToHoldDown := ""
if (Key1ToHoldDown = Key1ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key1ToHoldDownPrev ; There is a previous key to release.
Send, {%Key1ToHoldDownPrev% up} ; Release it.
if Key1ToHoldDown ; There is a key to press down.
Send, {%Key1ToHoldDown% down} ; Press it down.
}
; Joy2
if (KJoy2 > 0)
Key2ToHoldDown := "c"
else
Key2ToHoldDown := ""
if (Key2ToHoldDown = Key2ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key2ToHoldDownPrev ; There is a previous key to release.
Send, {%Key2ToHoldDownPrev% up} ; Release it.
if Key2ToHoldDown ; There is a key to press down.
Send, {%Key2ToHoldDown% down} ; Press it down.
}
; Joy3
if (KJoy3 > 0)
Key3ToHoldDown := "z"
else
Key3ToHoldDown := ""
if (Key3ToHoldDown = Key3ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key3ToHoldDownPrev ; There is a previous key to release.
Send, {%Key3ToHoldDownPrev% up} ; Release it.
if Key3ToHoldDown ; There is a key to press down.
Send, {%Key3ToHoldDown% down} ; Press it down.
}
; Joy4
if (KJoy4 > 0)
Key4ToHoldDown := "LShift"
else
Key4ToHoldDown := ""
if (Key4ToHoldDown = Key4ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key4ToHoldDownPrev ; There is a previous key to release.
Send, {%Key4ToHoldDownPrev% up} ; Release it.
if Key4ToHoldDown ; There is a key to press down.
Send, {%Key4ToHoldDown% down} ; Press it down.
}
; Joy5
if (KJoy5 > 0)
Key5ToHoldDown := "Control"
else
Key5ToHoldDown := ""
if (Key5ToHoldDown = Key5ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key5ToHoldDownPrev ; There is a previous key to release.
Send, {%Key5ToHoldDownPrev% up} ; Release it.
if Key5ToHoldDown ; There is a key to press down.
Send, {%Key5ToHoldDown% down} ; Press it down.
}
; Joy6
if (KJoy6 > 0)
Key6ToHoldDown := "Backspace"
else
Key6ToHoldDown := ""
if (Key6ToHoldDown = Key6ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key6ToHoldDownPrev ; There is a previous key to release.
Send, {%Key6ToHoldDownPrev% up} ; Release it.
if Key6ToHoldDown ; There is a key to press down.
Send, {%Key6ToHoldDown% down} ; Press it down.
}
; Joy9
if (KJoy9 > 0)
Key9ToHoldDown := "Enter"
else
Key9ToHoldDown := ""
if (Key9ToHoldDown = Key9ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key9ToHoldDownPrev ; There is a previous key to release.
Send, {%Key9ToHoldDownPrev% up} ; Release it.
if Key9ToHoldDown ; There is a key to press down.
Send, {%Key9ToHoldDown% down} ; Press it down.
}
; Joy10
if (KJoy10 > 0)
Key10ToHoldDown := "Tab"
else
Key10ToHoldDown := ""
if (Key10ToHoldDown = Key10ToHoldDownPrev) ; The correct key is already down (or no key is needed).
{
; Do nothing.
}
else
{
; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if Key10ToHoldDownPrev ; There is a previous key to release.
Send, {%Key10ToHoldDownPrev% up} ; Release it.
if Key10ToHoldDown ; There is a key to press down.
Send, {%Key10ToHoldDown% down} ; Press it down.
}
return