Advertisement
cragrim

AutoHotkey-StarboundJoyPad-ProofOfConcept

Dec 19th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Starbound Joypad Test by Cragrim
  2.  
  3. ; --- DECLARATIONS ---
  4. #Persistent
  5. #SingleInstance force
  6. #NoEnv
  7. SendMode Input
  8. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  9. WheelDelay := 5 ;Milliseconds between keypad event
  10. MouseOffset := 300 ;Increase on higher screen resolutions, or decrease on lower resolutions
  11.  
  12. ; Auto-detect the joystick number if called for:
  13. if JoystickNumber <= 0
  14. {
  15.     Loop 16  ; Query each joystick number to find out which ones exist.
  16.     {
  17.         GetKeyState, JoyName, %A_Index%JoyName
  18.         if JoyName <>
  19.         {
  20.             JoystickNumber = %A_Index%
  21.             break
  22.         }
  23.     }
  24.     if JoystickNumber <= 0
  25.     {
  26.         Msgbox No joypad was detected! Please connect a joypad and restart!
  27.         ExitApp
  28.     }
  29. }
  30.  
  31. Msgbox Joypad was detected in slot %JoystickNumber%
  32.  
  33. ;Declare timers for keydown events
  34. SetTimer DigitalPad, %WheelDelay%
  35.  
  36. Hotkey %JoystickNumber%Joy1, JumpButtonDown, on
  37. Hotkey %JoystickNumber%Joy2, AttackButtonDown, on
  38. Hotkey %JoystickNumber%Joy6, InventoryNext, on
  39. Hotkey %JoystickNumber%Joy5, InventoryPrev, on
  40. return  ; End of auto-execute section.
  41.  
  42. ; --- KEY BINDINGS ---
  43.  
  44.  
  45. #IfWinActive ahk_class SDL_app
  46. DigitalPad:
  47.     SetMouseDelay, -1  ; Makes movement smoother.
  48.     GetKeyState, POV, %JoystickNumber%JoyPOV
  49.     KeyToHoldDownPrev = %KeyToHoldDown%  ; Prev now holds the key that was down before (if any).
  50.  
  51.     if WinActive("ahk_class SDL_app")
  52.     {
  53.         ; Some joysticks might have a smooth/continous POV rather than one in fixed increments.
  54.         ; To support them all, use a range:
  55.         if POV < 0   ; No angle to report
  56.         {
  57.             KeyToHoldDown =
  58.         }
  59.        
  60.         else if POV = 31500 ; UpLeft
  61.         {
  62.             KeyToHoldDown = W
  63.             DllCall("SetCursorPos", int, (A_ScreenWidth/2-MouseOffset), int, (A_ScreenHeight/2-MouseOffset))
  64.         }
  65.         else if POV = 0 ; Up
  66.         {
  67.             KeyToHoldDown = W
  68.             DllCall("SetCursorPos", int, (A_ScreenWidth/2), int, (A_ScreenHeight/2-MouseOffset))
  69.         }
  70.         else if POV = 4500 ; UpRight
  71.         {
  72.             KeyToHoldDown = D
  73.             DllCall("SetCursorPos", int, (A_ScreenWidth/2+MouseOffset), int, (A_ScreenHeight/2-MouseOffset))
  74.         }
  75.         else if POV = 9000 ; Right
  76.         {
  77.             KeyToHoldDown = D
  78.             DllCall("SetCursorPos", int, (A_ScreenWidth/2+MouseOffset), int, (A_ScreenHeight/2))
  79.         }
  80.         else  if POV = 13500 ; DownRight
  81.         {
  82.             KeyToHoldDown = D
  83.             DllCall("SetCursorPos", int, (A_ScreenWidth/2+MouseOffset), int, (A_ScreenHeight/2+MouseOffset))
  84.         }
  85.         else if POV = 18000 ; Down
  86.         {
  87.             KeyToHoldDown = S
  88.             DllCall("SetCursorPos", int, (A_ScreenWidth/2), int, (A_ScreenHeight/2+MouseOffset))
  89.         }
  90.         else if POV = 22500 ; DownLeft
  91.         {
  92.             KeyToHoldDown = A
  93.             DllCall("SetCursorPos", int, (A_ScreenWidth/2-MouseOffset), int, (A_ScreenHeight/2+MouseOffset))
  94.         }
  95.         else if POV = 27000 ; Left
  96.         {
  97.             KeyToHoldDown = A
  98.             DllCall("SetCursorPos", int, (A_ScreenWidth/2-MouseOffset), int, (A_ScreenHeight/2))
  99.         }
  100.        
  101.         if KeyToHoldDown = %KeyToHoldDownPrev%  ; The correct key is already down (or no key is needed).
  102.             return  ; Do nothing.
  103.  
  104.         if KeyToHoldDownPrev   ; There is a previous key to release.
  105.         {
  106.             Send, {%KeyToHoldDownPrev% up}  ; Release it.
  107.         }
  108.  
  109.         SetKeyDelay -1  ; Avoid delays between keystrokes.
  110.         if KeyToHoldDown   ; There is a key to press down.
  111.         {
  112.             Send, {%KeyToHoldDown% down}  ; Press it down.
  113.         }
  114.     }
  115. return
  116.  
  117. #IfWinActive ahk_class SDL_app
  118. JumpButtonDown:
  119.     SetMouseDelay, -1  ; Makes movement smoother.
  120.     Send, {Space Down}
  121.     SetTimer, JumpButtonUp, 5
  122. return
  123.  
  124. JumpButtonUp:
  125.     if GetKeyState(JoystickNumber . "Joy1")
  126.         return  ; The button is still, down, so keep waiting.
  127.     ; Otherwise, the button has been released.
  128.     SetMouseDelay, -1  ; Makes movement smoother.
  129.     SetTimer, JumpButtonUp, off ; Turn itself off
  130.     Send, {Space Up}
  131. return
  132.  
  133. #IfWinActive ahk_class SDL_app
  134. AttackButtonDown:
  135.     SetMouseDelay, -1  ; Makes movement smoother.
  136.     MouseClick, left,,, 1, 0, D  ; Hold down the left mouse button.
  137.     SetTimer, AttackButtonUp, 5
  138. return
  139.  
  140. AttackButtonUp:
  141.     if GetKeyState(JoystickNumber . "Joy2")
  142.         return  ; The button is still, down, so keep waiting.
  143.     ; Otherwise, the button has been released.
  144.     SetMouseDelay, -1  ; Makes movement smoother.
  145.     SetTimer, AttackButtonUp, off ; Turn itself off
  146.     MouseClick, left,,, 1, 0, U  ; Release the mouse button.
  147. return
  148.  
  149. #IfWinActive ahk_class SDL_app
  150. InventoryNext:
  151.     SetMouseDelay, -1  ; Makes movement smoother.
  152.     Send, {WheelDown}
  153. return
  154.  
  155. #IfWinActive ahk_class SDL_app
  156. InventoryPrev:
  157.     SetMouseDelay, -1  ; Makes movement smoother.
  158.     Send, {WheelUp}
  159. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement