/docs/scripts/JoystickMouse.ahk

http://autohotkey-chinese.googlecode.com/ · AutoHotKey · 155 lines · 102 code · 23 blank · 30 comment · 15 complexity · 0d6a190e09cf147e9452350315c760e8 MD5 · raw file

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