Guest User

Untitled

a guest
Feb 1st, 2017
40
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. ; Using a Joystick as a Mouse
  4. ; http://www.autohotkey.com
  5. ; This script converts a joystick into a three-button mouse. It allows each
  6. ; button to drag just like a mouse button and it uses virtually no CPU time.
  7. ; Also, it will move the cursor faster depending on how far you push the joystick
  8. ; from center. You can personalize various settings at the top of the script.
  9.  
  10. ; Increase the following value to make the mouse cursor move faster:
  11. JoyMultiplier = 0.30
  12.  
  13. ; Decrease the following value to require less joystick displacement-from-center
  14. ; to start moving the mouse. However, you may need to calibrate your joystick
  15. ; -- ensuring it's properly centered -- to avoid cursor drift. A perfectly tight
  16. ; and centered joystick could use a value of 1:
  17. JoyThreshold = 3
  18.  
  19. ; Change the following to true to invert the Y-axis, which causes the mouse to
  20. ; move vertically in the direction opposite the stick:
  21. InvertYAxis := false
  22.  
  23. ; Change these values to use joystick button numbers other than 1, 2, and 3 for
  24. ; the left, right, and middle mouse buttons. Available numbers are 1 through 32.
  25. ; Use the Joystick Test Script to find out your joystick's numbers more easily.
  26. ButtonLeft = 1
  27. ButtonRight = 2
  28. ButtonMiddle = 3
  29.  
  30. ; If your joystick has a POV control, you can use it as a mouse wheel. The
  31. ; following value is the number of milliseconds between turns of the wheel.
  32. ; Decrease it to have the wheel turn faster:
  33. WheelDelay = 250
  34.  
  35. ; If your system has more than one joystick, increase this value to use a joystick
  36. ; other than the first:
  37. JoystickNumber = 1
  38.  
  39. ; END OF CONFIG SECTION -- Don't change anything below this point unless you want
  40. ; to alter the basic nature of the script.
  41.  
  42. #SingleInstance
  43.  
  44. JoystickPrefix = %JoystickNumber%Joy
  45. Hotkey, %JoystickPrefix%%ButtonLeft%, ButtonLeft
  46. Hotkey, %JoystickPrefix%%ButtonRight%, ButtonRight
  47. Hotkey, %JoystickPrefix%%ButtonMiddle%, ButtonMiddle
  48.  
  49. ; Calculate the axis displacements that are needed to start moving the cursor:
  50. JoyThresholdUpper := 50 + JoyThreshold
  51. JoyThresholdLower := 50 - JoyThreshold
  52. if InvertYAxis
  53. YAxisMultiplier = -1
  54. else
  55. YAxisMultiplier = 1
  56.  
  57. SetTimer, WatchJoystick, 10 ; Monitor the movement of the joystick.
  58.  
  59. GetKeyState, JoyInfo, %JoystickNumber%JoyInfo
  60. IfInString, JoyInfo, P ; Joystick has POV control, so use it as a mouse wheel.
  61. SetTimer, MouseWheel, %WheelDelay%
  62.  
  63. return ; End of auto-execute section.
  64.  
  65.  
  66. ; The subroutines below do not use KeyWait because that would sometimes trap the
  67. ; WatchJoystick quasi-thread beneath the wait-for-button-up thread, which would
  68. ; effectively prevent mouse-dragging with the joystick.
  69.  
  70. ButtonLeft:
  71. SetMouseDelay, -1 ; Makes movement smoother.
  72. MouseClick, left,,, 1, 0, D ; Hold down the left mouse button.
  73. SetTimer, WaitForLeftButtonUp, 10
  74. return
  75.  
  76. ButtonRight:
  77. SetMouseDelay, -1 ; Makes movement smoother.
  78. MouseClick, right,,, 1, 0, D ; Hold down the right mouse button.
  79. SetTimer, WaitForRightButtonUp, 10
  80. return
  81.  
  82. ButtonMiddle:
  83. SetMouseDelay, -1 ; Makes movement smoother.
  84. MouseClick, middle,,, 1, 0, D ; Hold down the right mouse button.
  85. SetTimer, WaitForMiddleButtonUp, 10
  86. return
  87.  
  88. WaitForLeftButtonUp:
  89. if GetKeyState(JoystickPrefix . ButtonLeft)
  90. return ; The button is still, down, so keep waiting.
  91. ; Otherwise, the button has been released.
  92. SetTimer, WaitForLeftButtonUp, off
  93. SetMouseDelay, -1 ; Makes movement smoother.
  94. MouseClick, left,,, 1, 0, U ; Release the mouse button.
  95. return
  96.  
  97. WaitForRightButtonUp:
  98. if GetKeyState(JoystickPrefix . ButtonRight)
  99. return ; The button is still, down, so keep waiting.
  100. ; Otherwise, the button has been released.
  101. SetTimer, WaitForRightButtonUp, off
  102. MouseClick, right,,, 1, 0, U ; Release the mouse button.
  103. return
  104.  
  105. WaitForMiddleButtonUp:
  106. if GetKeyState(JoystickPrefix . ButtonMiddle)
  107. return ; The button is still, down, so keep waiting.
  108. ; Otherwise, the button has been released.
  109. SetTimer, WaitForMiddleButtonUp, off
  110. MouseClick, middle,,, 1, 0, U ; Release the mouse button.
  111. return
  112.  
  113. WatchJoystick:
  114. MouseNeedsToBeMoved := false ; Set default.
  115. SetFormat, float, 03
  116. GetKeyState, joyx, %JoystickNumber%JoyX
  117. GetKeyState, joyy, %JoystickNumber%JoyY
  118. if joyx > %JoyThresholdUpper%
  119. {
  120. MouseNeedsToBeMoved := true
  121. DeltaX := joyx - JoyThresholdUpper
  122. }
  123. else if joyx < %JoyThresholdLower%
  124. {
  125. MouseNeedsToBeMoved := true
  126. DeltaX := joyx - JoyThresholdLower
  127. }
  128. else
  129. DeltaX = 0
  130. if joyy > %JoyThresholdUpper%
  131. {
  132. MouseNeedsToBeMoved := true
  133. DeltaY := joyy - JoyThresholdUpper
  134. }
  135. else if joyy < %JoyThresholdLower%
  136. {
  137. MouseNeedsToBeMoved := true
  138. DeltaY := joyy - JoyThresholdLower
  139. }
  140. else
  141. DeltaY = 0
  142. if MouseNeedsToBeMoved
  143. {
  144. SetMouseDelay, -1 ; Makes movement smoother.
  145. MouseMove, DeltaX * JoyMultiplier, DeltaY * JoyMultiplier * YAxisMultiplier, 0, R
  146. }
  147. return
  148.  
  149. MouseWheel:
  150. GetKeyState, JoyPOV, %JoystickNumber%JoyPOV
  151. if JoyPOV = -1 ; No angle.
  152. return
  153. if (JoyPOV > 31500 or JoyPOV < 4500) ; Forward
  154. Send {WheelUp}
  155. else if JoyPOV between 13500 and 22500 ; Back
  156. Send {WheelDown}
  157. return
  158.  
  159.  
  160. ; you got to press the two button at the same time to exit this script, joy 5&6 (bumpers button)
  161. joy5::
  162. joy6::
  163. if getkeystate("joy5")
  164. if getkeystate("joy6")
  165. ExitApp
  166. return
RAW Paste Data