Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Improved Joystick POV Hat -> Keyboard script

  • Please log in to reply
No replies to this topic
  • Members
  • 1 posts
  • Last active: Sep 26 2015 06:03 PM
  • Joined: 11 Jan 2015

This suggestion is in reference to an example script in the documentation: http://www.autohotke...ystick.htm#axis


I tried the second of those two scripts, because mapping the POV hat of my gamepad to the cursor keys was what I wanted. However, I found that in some situations inputs got lost, especially when quickly changing direction from a cardinal direction to a diagonal direction. Upon closer examination of the script it seems the code assumes you only ever want to use the cardinal directions; it is unclear to me why diagonal directions even work at all.


At any rate, I set out to improve the script and arrived at this:

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

GetKeyState, POV, JoyPOV  ; Get position of the POV control.

KeyList := {"Up": 0, "Down": 0, "Left": 0, "Right": 0}

; Some joysticks might have a smooth/continous POV rather than one in fixed increments.
; To support them all, use a range:
; Note that the arc for each direction overlaps with the two adjacent ones.
if POV >= 28250                 ; 282.5 to 360 degrees: Up
    KeyList["Up"] := 1
if POV between 0 and 6750       ; 0 to 67.5 degrees: Up
    KeyList["Up"] := 1
if POV between 2250 and 15750  ; 22.5 to 157.5 degrees: Right
    KeyList["Right"] := 1
if POV between 10250 and 24750 ; 102.5 to 247.5 degrees: Down
    KeyList["Down"] := 1
if POV between 20250 and 33750 ; 202.5 to 337.5 degrees: Left
    KeyList["Left"] := 1

SetKeyDelay -1  ; Avoid delays between keystrokes.
for Key, KeyState in KeyList { ; Update all keys
    if (GetKeyState(Key) != KeyState) ; Check if the keyboard key state matches the POV direction.
        if (KeyState) ; Check if we need to press this key down...
            Send, {%Key% down}
        else ; ...or up.
            Send, {%Key% up}

I have found this version of the script to be more responsive, and so I would like to submit it as a replacement for the one included in the documentation. The key repeat behavior mentioned at the bottom of that section can be achieved by copying lines 27 and 28 and pasting them on line 31.