Advertisement
Guest User

Untitled

a guest
Aug 29th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.26 KB | None | 0 0
  1. /*
  2. Author: jpjazzy (Jeremy)
  3.  
  4. Last edit date: 11/25/2011
  5.  
  6. Description:
  7. Originally as part of the MW3 Joystick script, I decided I wanted to take it a step farther and let people make their own settings! This will work for any game that AutoHotkey can interact with (note that some full screen games function on a different level, so you will either need to window mode them some how or they will not work). This script is meant to be an alternative to joystick support programs that cost money such as Pinnacle Game Profiler.
  8.  
  9. Features:
  10. -Save your controller layout settings as a .jjc file (you can even give these to friends to help someone else with a controller layout for a new game)
  11. -Turn on and off 360 controller motors
  12. -A new way of reading the LT and RT buttons eliminates the Z axis problems, see lexikos script link for details
  13. -Send to tray
  14. -Mouse emulation
  15. -Threshold calibration
  16. -For people with more controllers than one, you can change which controller you use by changing the Joystick Number
  17. -Rapid fire (simply turns a semi-automatic to an automatic, it doesn't function all that fast)
  18. -Different key modes make getting the right setting for your game a lot easier
  19.  
  20. Future features:
  21. -Checking for updates
  22. -Help file
  23. -Game window sensitive hotkeys
  24.  
  25. Saved files extensions: \*.jjc [Jeremy's Joystick Configuration] [NOTE]
  26.  
  27. Required additions (Included at the bottom of the script):
  28. XInput.ahk - Script by Lexikos for xbox controller (http://www.autohotkey.com/forum/topic39091.html)
  29. json.ahk - Script by polyethene (http://www.autohotkey.com/forum/viewtopic.php?t=34565)
  30.  
  31. */
  32.  
  33. ; ###################### Initial settings ###############################
  34. #Persistent
  35. #SingleInstance, Force ; For restarting
  36. SetBatchLines, -1 ; Faster script settings
  37. Version := "1.0.0" ; Version number
  38.  
  39. ;function to make the LT/RT keys function properly rather than on one axis
  40. XInput_Init()
  41.  
  42. ; Increase the following value to make the mouse cursor move faster:
  43. JoyMultiplier = 0.40
  44.  
  45. ; Decrease the following value to require less joystick displacement-from-center
  46. ; to start moving the mouse. However, you may need to calibrate your joystick
  47. ; -- ensuring it's properly centered -- to avoid cursor drift. A perfectly tight
  48. ; and centered joystick could use a value of 1:
  49. JoyThreshold = 15
  50.  
  51. ; Same as above with RT/LT threshold displacement
  52. TriggerThreshold := 5
  53.  
  54. ; If your system has more than one joystick, increase this value to use a joystick
  55. ; other than the first:
  56. JoystickNumber = 1
  57.  
  58. ; For inverting the axis
  59. YAxisMultiplier := 1
  60.  
  61. ; Calculate the axis displacements that are needed to start moving the cursor:
  62. JoyThresholdUpper := 50 + JoyThreshold
  63. JoyThresholdLower := 50 - JoyThreshold
  64.  
  65. SetTimer, CheckTriggers, 5 ; Watches the triggers on the controller
  66. SetTimer, JoyToMouse, 10 ; Monitors analog to mouse
  67. SetTimer, WatchAxis, 10 ; Monitors analog to keys
  68. SetTimer, WatchPOV, 10 ; watch dpad
  69.  
  70. Loop 10 ; Handles joy1-10
  71. Hotkey, Joy%A_Index%, JoyButtonHandler
  72.  
  73.  
  74. Menu, Tray, Add, Unhide, Unhide ; Handles unhiding the GUI
  75. menu, Tray, Default, Unhide
  76. Menu, Tray, Click, 1
  77.  
  78. ; ###################### Generate Main GUI ###############################
  79. Gui, Color, 0xC20000
  80. Menu, FileMenu, Add, &Open Previous Game Settings..., MenuFileOpen
  81. Menu, FileMenu, Add, &Save Game Settings..., MenuFileSave
  82. Menu, FileMenu, Add, Send to tray, MenuHandler
  83. Menu, FileMenu, Add, E&xit, GuiClose
  84. Menu, HelpMenu, Add, &About, MenuHandler
  85. Menu, HelpMenu, Add, Help, MenuHandler
  86. Menu, HelpMenu, Add, &Check for updates..., MenuHandler
  87. Menu, MyMenuBar, Add, &File, :FileMenu ; Attach the two sub-menus that were created above.
  88. Menu, MyMenuBar, Add, &Help, :HelpMenu
  89. Gui, Menu, MyMenuBar
  90. Gui, Font, bold c700000
  91. Gui, Add, GroupBox, x15 y20 w130 h90 , Controller Vibration
  92. Gui, Add, CheckBox, x22 y40 w100 h30 Checked vLTV, LT Button Vibration
  93. Gui, Add, CheckBox, x22 y70 w100 h30 Checked vRTV, RT Button Vibration
  94. Gui, Add, GroupBox, x152 y20 w220 h80 , Controller options
  95. Gui, Add, Text, x172 y40 w100 h30 , Controller Threshold:
  96. Gui, Add, Slider, x272 y40 w90 h20 vJoyThreshold R0-30 ToolTip, 15
  97. Gui, Add, Text, x172 y70 w100 h20 , Joystick Number:
  98. Gui, Add, Edit, x272 y70 w90 h20 vJoystickNumber, 1
  99. Gui, Font, s12 bold c700000
  100. Gui, Add, Text, x10 y+40, Key Mappings:
  101. Gui, Font, s9 bold c000000, Times New Roman
  102. Gui, Add, ListView, x10 r25 w420 gJoyController grid, 360 Controller Button|Button Mode|Remap key(s)
  103. LV_Add("", "A Button", "", "")
  104. LV_Add("", "B Button", "", "")
  105. LV_Add("", "X Button", "", "")
  106. LV_Add("", "Y Button", "", "")
  107. LV_Add("", "LB Button", "", "")
  108. LV_Add("", "RB Button", "", "")
  109. LV_Add("", "Select Button", "", "")
  110. LV_Add("", "Start Button", "", "")
  111. LV_Add("", "Left Joystick Button", "", "")
  112. LV_Add("", "Right Joystick Button", "", "")
  113. LV_Add("", "RT Button", "", "")
  114. LV_Add("", "LT Button", "", "")
  115. LV_Add("", "Directional Pad Up Button", "", "")
  116. LV_Add("", "Directional Pad Down Button", "", "")
  117. LV_Add("", "Directional Pad Right Button", "", "")
  118. LV_Add("", "Directional Pad Left Button", "", "")
  119. LV_Add("", "Left Joystick Right", "", "")
  120. LV_Add("", "Left Joystick Left", "", "")
  121. LV_Add("", "Left Joystick Up", "", "")
  122. LV_Add("", "Left Joystick Down", "", "")
  123. LV_Add("", "Right Joystick Right", "", "")
  124. LV_Add("", "Right Joystick Left", "", "")
  125. LV_Add("", "Right Joystick Up", "", "")
  126. LV_Add("", "Right Joystick Down", "", "")
  127. LV_ModifyCol(1) ; Auto-size each column to fit its contents.
  128. Gui, Font, s7 bold c000000, Times New Roman
  129. Gui, Add, Text,, Jeremy's 360 controller support © 2011
  130. Gui, Show, Autosize, Universal 360 Controller Support
  131. return
  132.  
  133. ; ###################### Saving File Code ###############################
  134. MenuFileSave:
  135. Loop, 24
  136. {
  137. O_Index := A_Index
  138. LV_GetText(TempReader1, O_Index)
  139. LV_GetText(TempReader2, O_Index, 2)
  140. LV_GetText(TempReader3, O_Index, 3)
  141. If (!SaveFile)
  142. {
  143. SaveFile := TempReader1
  144. If (TempReader2)
  145. SaveFile .= "|" TempReader2
  146. If (TempReader3)
  147. SaveFile .= "|" TempReader3
  148. }
  149. else
  150. {
  151. Loop, 3
  152. {
  153. I_Index := A_Index
  154. If (I_Index = 1 && TempReader1 != "")
  155. SaveFile .= "`n" TempReader1
  156. else if (I_Index = 2 && TempReader2 != "")
  157. SaveFile .= "|" TempReader2
  158. else if (I_Index = 3 && TempReader3 != "")
  159. SaveFile .= "|" TempReader3
  160. }
  161. }
  162. }
  163. FileSelectFile, SaveLocation, S,,, Jeremy's Joystick Configuration (*.jjc)
  164. If ErrorLevel = 1
  165. return
  166. If (!RegExMatch(SaveLocation, "(.*)\.jjc$"))
  167. SaveLocation .= ".jjc"
  168. FileAppend, %SaveFile%, %SaveLocation%
  169. SaveLocation := ""
  170. SaveFile := "" ; Clear the variables
  171. return
  172.  
  173. ; ###################### Opening File Code ###############################
  174. MenuFileOpen:
  175. FileSelectFile, OpenLocation,,, Open file, Jeremy's Joystick Configuration (*.jjc)
  176. If (!RegExMatch(OpenLocation, "(.*)\.jjc$"))
  177. {
  178. MsgBox, 16, Error, The file selected is not a .jjc file.
  179. }
  180. else
  181. {
  182. Loop, Read, %OpenLocation%
  183. {
  184. StringSplit, Col, A_LoopReadLine, |
  185. ;~ MsgBox % Col1 "`n" Col2 "`n" Col3
  186. LV_Modify(A_Index, "", Col1, Col2, Col3)
  187. ;~ If (Col2)
  188. ;~ LV_Modify(A_Index, "", "", Col2)
  189. ;~ If (Col3)
  190. ;~ LV_Modify(A_Index, "", "", "", Col3)
  191.  
  192. Loop, 3
  193. Col%A_Index% := "" ; Clear variable
  194. }
  195. }
  196. OpenLocation := "" ; Clear variable
  197. return
  198.  
  199. ; ###################### Menu Handling Code ###############################
  200. MenuHandler:
  201. If (A_ThisMenuItem == "&About" && A_ThisMenu == "HelpMenu")
  202. {
  203. MsgBox, 64, About, Version: %Version%`nUniversal 360 Controller Support by Jeremy © 2011`n`nSpecial thanks to polyethene and Lexikos for the script functions.
  204. }
  205. Else If (A_ThisMenuItem == "Send to tray" && A_ThisMenu == "FileMenu")
  206. {
  207. Gui, 1: Default
  208. Gui, Hide
  209. }
  210. return
  211.  
  212. ; ###################### For LT and RT buttons ###############################
  213. CheckTriggers:
  214. GetTriggerState(LT, RT)
  215. RTNum := 11
  216. LTNum := 12
  217.  
  218. LV_GetText(RTKeyMode, RTNum, 2)
  219. LV_GetText(Keys, RTNum, 3)
  220. RHitKey := RegExReplace(Keys, "^(.*)\(When Pushed\)(.*)", "$1")
  221. LV_GetText(LTKeyMode, LTNum, 2)
  222. LV_GetText(Keys, LTNum, 3)
  223. LHitKey := RegExReplace(Keys, "^(.*)\(When Pushed\)(.*)", "$1")
  224. HitTriggers := false
  225.  
  226.  
  227. If (LT > TriggerThreshold)
  228. HitTriggers := true
  229. Else If (RT > TriggerThreshold)
  230. HitTriggers := true
  231.  
  232. If (HitTriggers)
  233. {
  234.  
  235. If (RTKeyMode == "Rapid Fire" && RT > TriggerThreshold)
  236. SendInput, {%LHitKey%}
  237. If (LTKeyMode == "Rapid Fire" && LT > TriggerThreshold)
  238. SendInput, {%RHitKey%}
  239. If (LT > TriggerThreshold && LTFlag != 1)
  240. {
  241. SendInput, {%RHitKey% down}
  242. LTFlag := 1
  243. }
  244. else if (LT <= TriggerThreshold)
  245. LTFlag := 0
  246. If (RT > TriggerThreshold && RTFlag != 1)
  247. {
  248. SendInput, {%LHitKey% down}
  249. RTFlag := 1
  250. }
  251. else if (RT <= TriggerThreshold)
  252. RTFlag := 0
  253. }
  254. Else
  255. {
  256. If (RTFlag)
  257. {
  258. SendInput, {%LHitKey% up}
  259. RTFlag := 0
  260. }
  261. If (LTFlag)
  262. {
  263. SendInput, {%RHitKey% up}
  264. LTFlag := 0
  265. }
  266. }
  267. return
  268.  
  269.  
  270. ; ###################### Add joystick configuration on double click ###############################
  271. JoyController:
  272. if (A_GuiEvent = "DoubleClick" && A_EventInfo <= 10) ; Only do this for the first 10 buttons
  273. {
  274. Gui, 2: Default
  275. Gui +Owner
  276. Gui, Add, Text,, Button Style:
  277. Gui, Add, Text,, Key(s)
  278. Gui, Add, DropDownList, r5 ys gEnDisFields vKeyType, Key Push||Key Push and Hold|Dual Key Push and Hold|Hold Key Until Release||Rapid Fire
  279. Gui, Add, Edit, v360Key1,
  280. Gui, Add, Edit, v360Key2,
  281. Gui, Add, Button, Default gSubmitButtonFields, Submit
  282. Gui, Add, Button, g2GuiClose, Cancel
  283. GuiControl, Disable, 360Key2
  284. Gui, Add, Button, ys gAboutModes, ?
  285. Gui, Add, Button, gShowSpecialKeys, Special key names
  286. Gui, -Border
  287. Gui, Show, Autosize, Add Setting
  288. Gui, 1: Default
  289. ActiveRow := A_EventInfo
  290. LV_GetText(360ButtonName, A_EventInfo)
  291. SetTimer, CheckActive, 50
  292. }
  293. Else if (A_GuiEvent = "DoubleClick" && (A_EventInfo = 11 || A_EventInfo = 12) ) ; Only do this for RT and LT buttons
  294. {
  295. Gui, 2: Default
  296. Gui +Owner
  297. Gui, Add, Text,, Button Style:
  298. Gui, Add, Text,, Key(s)
  299. Gui, Add, DropDownList, r2 ys gEnDisFields vKeyType, Key Push and Hold||Rapid Fire
  300. Gui, Add, Edit, v360Key1,
  301. Gui, Add, Edit, v360Key2,
  302. Gui, Add, Button, Default gSubmitButtonFields, Submit
  303. Gui, Add, Button, g2GuiClose, Cancel
  304. GuiControl, Disable, 360Key2
  305. Gui, Add, Button, ys gAboutModes, ?
  306. Gui, Add, Button, gShowSpecialKeys, Special key names
  307. Gui, -Border
  308. Gui, Show, Autosize, Add Setting
  309. Gui, 1: Default
  310. ActiveRow := A_EventInfo
  311. LV_GetText(360ButtonName, A_EventInfo)
  312. SetTimer, CheckActive, 50
  313. }
  314. Else if (A_GuiEvent = "DoubleClick" && (A_EventInfo >= 13 && A_EventInfo < 17) ) ; dpad
  315. {
  316. Gui, 2: Default
  317. Gui +Owner
  318. Gui, Add, Text,, Directional Pad Style:
  319. Gui, Add, Text,, Key(s)
  320. Gui, Add, DropDownList, r3 ys gEnDisFields vKeyType, KeyPush||Key Push and Hold|
  321. Gui, Add, Edit, v360Key1,
  322. Gui, Add, Edit, v360Key2,
  323. Gui, Add, Button, Default gSubmitButtonFields, Submit
  324. Gui, Add, Button, g2GuiClose, Cancel
  325. GuiControl, Disable, 360Key2
  326. Gui, Add, Button, ys gAboutModes, ?
  327. Gui, Add, Button, gShowSpecialKeys, Special key names
  328. Gui, -Border
  329. Gui, Show, Autosize, Add Setting
  330. Gui, 1: Default
  331. ActiveRow := A_EventInfo
  332. LV_GetText(360ButtonName, A_EventInfo)
  333. SetTimer, CheckActive, 50
  334. }
  335. Else if (A_GuiEvent = "DoubleClick" && (A_EventInfo >= 17 && A_EventInfo < 21) ) ; left joystick
  336. {
  337. Gui, 2: Default
  338. Gui +Owner
  339. Gui, Add, Text,, Left Joystick Style:
  340. Gui, Add, Text,, Key(s)
  341. Gui, Add, DropDownList, r3 ys gEnDisFields vKeyType, KeyPush|Key Push and Hold||Mouse Emulation
  342. Gui, Add, Edit, v360Key1,
  343. Gui, Add, Edit, v360Key2,
  344. Gui, Add, Button, Default gSubmitButtonFields, Submit
  345. Gui, Add, Button, g2GuiClose, Cancel
  346. GuiControl, Disable, 360Key2
  347. Gui, Add, Button, ys gAboutModes, ?
  348. Gui, Add, Button, gShowSpecialKeys, Special key names
  349. Gui, -Border
  350. Gui, Show, Autosize, Add Setting
  351. Gui, 1: Default
  352. ActiveRow := A_EventInfo
  353. LV_GetText(360ButtonName, A_EventInfo)
  354. SetTimer, CheckActive, 50
  355. }
  356. Else if (A_GuiEvent = "DoubleClick" && (A_EventInfo >= 21 && A_EventInfo < 25) ) ; right joystick
  357. {
  358. Gui, 2: Default
  359. Gui +Owner
  360. Gui, Add, Text,, Right Joystick Style:
  361. Gui, Add, Text,, Key(s)
  362. Gui, Add, DropDownList, r3 ys gEnDisFields vKeyType, KeyPush|Key Push and Hold||Mouse Emulation
  363. Gui, Add, Edit, v360Key1,
  364. Gui, Add, Edit, v360Key2,
  365. Gui, Add, Button, Default gSubmitButtonFields, Submit
  366. Gui, Add, Button, g2GuiClose, Cancel
  367. GuiControl, Disable, 360Key2
  368. Gui, Add, Button, ys gAboutModes, ?
  369. Gui, Add, Button, gShowSpecialKeys, Special key names
  370. Gui, -Border
  371. Gui, Show, Autosize, Add Setting
  372. Gui, 1: Default
  373. ActiveRow := A_EventInfo
  374. LV_GetText(360ButtonName, A_EventInfo)
  375. SetTimer, CheckActive, 50
  376. }
  377.  
  378. return
  379.  
  380. ; ###################### dpad to key ######################
  381. WatchPOV:
  382. Loop, 4
  383. {
  384. LV_GetText(DPAD%A_Index%KeyMode, 13+(A_Index-1), 2)
  385. LV_GetText(DPAD%A_Index%Keys, 13+(A_Index-1), 3)
  386. DPADKey%A_Index% := RegExReplace(DPAD%A_Index%Keys, "^(.*)\(When Pushed\)(.*)", "$1")
  387. }
  388.  
  389. GetKeyState, POV, JoyPOV ; Get position of the POV control.
  390.  
  391. ; Some joysticks might have a smooth/continous POV rather than one in fixed increments.
  392. ; To support them all, use a range:
  393. if POV < 0 ; No angle to report
  394. POVKeyToHoldDown =
  395. else if POV > 31500 ; 315 to 360 degrees: Forward
  396. POVKeyToHoldDown = %DPADKey1%
  397. else if POV between 0 and 4500 ; 0 to 45 degrees: Forward
  398. POVKeyToHoldDown = %DPADKey1%
  399. else if POV between 4501 and 13500 ; 45 to 135 degrees: Right
  400. POVKeyToHoldDown = %DPADKey3%
  401. else if POV between 13501 and 22500 ; 135 to 225 degrees: Down
  402. POVKeyToHoldDown = %DPADKey2%
  403. else ; 225 to 315 degrees: Left
  404. POVKeyToHoldDown = %DPADKey4%
  405.  
  406. If (POVKeyToHoldDown)
  407. {
  408. SendInput, %POVKeyToHoldDown%
  409. Sleep 300
  410. }
  411. return
  412.  
  413. ; ###################### Analog to key ######################
  414. WatchAxis:
  415. Loop, 8
  416. {
  417. LV_GetText(RKs2, 17+(A_Index-1), 3)
  418. If (RKs2 != "MOUSE" || RKs2 != "")
  419. {
  420. LV_GetText(JoystickSide2, 17+(A_Index-1))
  421. If (RegExMatch(JoystickSide2, "^Right Joystick"))
  422. AxisSet2 := "U|R"
  423. Else If (RegExMatch(JoystickSide2, "^Left Joystick"))
  424. AxisSet2 := "X|Y"
  425.  
  426. Break
  427. }
  428.  
  429. If (A_index = 8)
  430. AxisSet2 := ""
  431. }
  432. If (!AxisSet2) ; Determines if there is nothing assigned to mouse emulation
  433. return
  434. Loop, Parse, AxisSet2, | ; If there is this breaks the axis' up
  435. Axis%A_Index% := A_LoopField
  436.  
  437. If (AxisSet2 == "X|Y")
  438. {
  439. Loop, 4
  440. {
  441. LV_GetText(Joy%A_Index%KeyMode, 17+(A_Index-1), 2)
  442. LV_GetText(Joy%A_Index%Keys, 17+(A_Index-1), 3)
  443. JoyKey%A_Index% := RegExReplace(Joy%A_Index%Keys, "^(.*)\(When Pushed\)(.*)", "$1")
  444. }
  445. }
  446. Else If (AxisSet2 == "U|R")
  447. {
  448. Loop, 4
  449. {
  450. LV_GetText(Joy%A_Index%KeyMode, 21+(A_Index-1), 2)
  451. LV_GetText(Joy%A_Index%Keys, 21+(A_Index-1), 3)
  452. JoyKey%A_Index% := RegExReplace(Joy%A_Index%Keys, "^(.*)\(When Pushed\)(.*)", "$1")
  453. }
  454. }
  455.  
  456. GetKeyState, JoyX, Joy%Axis1% ; Get position of X axis.
  457. GetKeyState, JoyY, Joy%Axis2% ; Get position of Y axis.
  458. KeyToHoldDownPrev = %KeyToHoldDown% ; Prev now holds the key that was down before (if any).
  459.  
  460. if (JoyX > 50+JoyThreshold)
  461. KeyToHoldDown = %JoyKey1%
  462. else if (JoyX < 50-JoyThreshold)
  463. KeyToHoldDown = %JoyKey2%
  464. else if (JoyY > 50+JoyThreshold)
  465. KeyToHoldDown = %JoyKey4%
  466. else if (JoyY < 50-JoyThreshold)
  467. KeyToHoldDown = %JoyKey3%
  468. else
  469. KeyToHoldDown =
  470.  
  471. if KeyToHoldDown = %KeyToHoldDownPrev% ; The correct key is already down (or no key is needed).
  472. return ; Do nothing.
  473.  
  474. ; Otherwise, release the previous key and press down the new key:
  475. SetKeyDelay -1 ; Avoid delays between keystrokes.
  476. if KeyToHoldDownPrev ; There is a previous key to release.
  477. Send, {%KeyToHoldDownPrev% up} ; Release it.
  478. if KeyToHoldDown ; There is a key to press down.
  479. Send, {%KeyToHoldDown% down} ; Press it down.
  480.  
  481. return
  482.  
  483.  
  484. ; ###################### Converts a joystick to mouse movement ######################
  485. JoyToMouse:
  486. Loop, 8
  487. {
  488. LV_GetText(RKs, 17+(A_Index-1), 3)
  489. If (RKs == "MOUSE")
  490. {
  491. LV_GetText(JoystickSide, 17+(A_Index-1))
  492. If (RegExMatch(JoystickSide, "^Right Joystick"))
  493. AxisSet := "U|R"
  494. Else If (RegExMatch(JoystickSide, "^Left Joystick"))
  495. AxisSet := "X|Y"
  496.  
  497. Break
  498. }
  499.  
  500. If (A_index = 8)
  501. AxisSet := ""
  502. }
  503.  
  504. If (!AxisSet) ; Determines if there is nothing assigned to mouse emulation
  505. return
  506. Loop, Parse, AxisSet, | ; If there is this breaks the axis' up
  507. %A_Index%Axis := A_LoopField
  508.  
  509. MouseNeedsToBeMoved := false ; Set default.
  510. SetFormat, float, 03
  511. GetKeyState, joyx, %JoystickNumber%Joy%1Axis%
  512. GetKeyState, joyy, %JoystickNumber%Joy%2Axis%
  513. if joyx > %JoyThresholdUpper%
  514. {
  515. MouseNeedsToBeMoved := true
  516. DeltaX := joyx - JoyThresholdUpper
  517. }
  518. else if joyx < %JoyThresholdLower%
  519. {
  520. MouseNeedsToBeMoved := true
  521. DeltaX := joyx - JoyThresholdLower
  522. }
  523. else
  524. DeltaX = 0
  525. if joyy > %JoyThresholdUpper%
  526. {
  527. MouseNeedsToBeMoved := true
  528. DeltaY := joyy - JoyThresholdUpper
  529. }
  530. else if joyy < %JoyThresholdLower%
  531. {
  532. MouseNeedsToBeMoved := true
  533. DeltaY := joyy - JoyThresholdLower
  534. }
  535. else
  536. DeltaY = 0
  537. if MouseNeedsToBeMoved
  538. {
  539. SetMouseDelay, -1 ; Makes movement smoother.
  540. MouseMove, DeltaX * JoyMultiplier, DeltaY * JoyMultiplier * YAxisMultiplier, 0, R
  541. }
  542. return
  543.  
  544. ; ###################### Destroys the GUI if it is not active ######################
  545. CheckActive:
  546. IfWinNotActive, Add Setting ahk_class AutoHotkeyGUI
  547. {
  548. Gui, 2: Destroy
  549. SetTimer, CheckActive, off
  550. }
  551. return
  552.  
  553. ; ###################### Information about the key modes ###############################
  554. AboutModes:
  555. MsgBox, 64, Mode information, Key Push - Pushes the designated key one time`n`nKey Push and Hold - Pushes the designated key then waits 400 milliseconds and then continues pressing the key every 50 milliseconds until release`n`nDual Key Push and Hold - Pushes the designed first key then waits 400 milliseconds and holds the next key until release`n`nHold Key Until Release - Holds the key until release`n`nRapid Fire - For automatically clicking a button quickly while it is held.`n`nMouse Emulation - Makes the joystick emulate mouse movement
  556. return
  557.  
  558. ; ###################### Information on key remappings ###############################
  559. ShowSpecialKeys:
  560. MsgBox, 64, Special Keys, You will now be redirected to the AutoHotkey documentation key list.
  561. Run, http://www.autohotkey.com/docs/KeyList.htm
  562. return
  563.  
  564. ; ###################### Submit new key set ###############################
  565. SubmitButtonFields:
  566. Gui, 2: Submit
  567. Gui, 2: Destroy
  568. If (KeyType == "Mouse Emulation") ; makes the desired joystick a mouse emulation
  569. {
  570. If (ActiveRow >= 21 && ActiveRow < 25) ; Right
  571. {
  572. Loop, 4
  573. {
  574. Gui, 1: Default
  575. LV_GetText(360ButtonName, 21+(A_Index-1))
  576. LV_Modify(21+(A_Index-1), "", 360ButtonName, KeyType, "MOUSE")
  577. }
  578. }
  579. Else If (ActiveRow >= 17 && ActiveRow < 21) ; Left
  580. {
  581. Loop, 4
  582. {
  583. Gui, 1: Default
  584. LV_GetText(360ButtonName, 17+(A_Index-1))
  585. LV_Modify(17+(A_Index-1), "", 360ButtonName, KeyType, "MOUSE")
  586. }
  587. }
  588.  
  589. goto, Done ; Skip key inputting
  590. }
  591. If (!360Key1) ; Check the first key
  592. KeySet := "- Blank -"
  593. else
  594. KeySet := 360Key1 "(When Pushed)"
  595.  
  596. If (360Key2) ; Check second key
  597. KeySet .= " and " 360Key2 "(While Held)"
  598.  
  599. Gui, 1: Default ; Add the items
  600. LV_Modify(ActiveRow, "", 360ButtonName, KeyType, KeySet)
  601.  
  602. Done:
  603. KeySet := "" ; clear variables
  604. 360Key1 := ""
  605. 360Key2 := ""
  606. KeyType := ""
  607. return
  608.  
  609. ; ###################### Enable/Disable key fields based on the type of key mode ###############################
  610. EnDisFields:
  611. Gui, 2: Default
  612. Gui, Submit, Nohide
  613. If (KeyType == "Key Push" || KeyType == "Key Push and Hold" || KeyType == "Hold Key Until Release")
  614. {
  615. GuiControl,, 360Key2,
  616. GuiControl, Disable, 360Key2
  617. }
  618. Else If (KeyType == "Dual Key Push and Hold")
  619. {
  620. GuiControl, Enable, 360Key2
  621. }
  622. Gui, 1: Default
  623. return
  624.  
  625. ; ###################### Handdles the button pressing ###############################
  626. JoyButtonHandler:
  627. Gui, 1: Default
  628. StringTrimLeft, JoyNumber, A_ThisHotkey, 3
  629. LV_GetText(KeyMode, JoyNumber, 2)
  630. LV_GetText(Keys, JoyNumber, 3)
  631. Key1 := RegExReplace(Keys, "^(.*)\(When Pushed\)(.*)", "$1")
  632. If (RegExMatch(Keys, "^(.*)and (.*)\(While Held\)"))
  633. Key2 := RegExReplace(Keys, "^(.*)and (.*)\(While Held\)", "$2")
  634. If (KeyMode == "" || Keys == "" || Keys == "- Blank -")
  635. return
  636.  
  637. ;~ MsgBox % Key2
  638. If (KeyMode == "Key Push")
  639. SendInput, {%Key1%}
  640. Else if (KeyMode == "Key Push and Hold")
  641. PushHold(Key1)
  642. Else if (KeyMode == "Dual Key Push and Hold")
  643. DualKeyHold(Key1, Key2)
  644. Else if (KeyMode == "Hold Key Until Release")
  645. HoldKeyUntilRelease(Key1)
  646. Else if (KeyMode == "Rapid Fire")
  647. RapidFire(Key1)
  648. return
  649.  
  650. ; ###################### For cancel button on 2nd GUI ###############################
  651. 2GuiClose:
  652. Gui, 2: Destroy
  653. return
  654.  
  655. GuiClose:
  656. ExitApp
  657.  
  658. ; ######################## Unhides GUI if you click on the tray icon #################
  659. Unhide:
  660. Gui, 1: Default
  661. Gui, Show, Autosize
  662. return
  663.  
  664.  
  665. ; ##################################### Functions ######################################
  666. ; $$$$$$$$$$$$$$$$$$$ FUNCTION TO KEEP KEY PUSHED UNTIL I RELEASE IT $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  667. ; Remap key = key to push with keyboard/mouse (including {} brackets if needed). Intial delay = delay before the key continues to be sent. Delay = Delay between each send key after initial delay if you hold the joy button.
  668. PushHold(RemapKey, InitialDelay=400, Delay=50)
  669. {
  670. SendInput, %RemapKey%
  671. Sleep %InitialDelay%
  672. while (GetKeyState(A_ThisHotkey, "P"))
  673. {
  674. SendInput, {%RemapKey%}
  675. Sleep %Delay%
  676. }
  677. return
  678. }
  679. ; $$$$$$$$$$ Rapid fire function to turn an button into an automaic button press $$$$$$$$$$$$$$
  680. RapidFire( rapidfirekey ){
  681. While (GetKeyState(A_ThisHotkey, "P"))
  682. {
  683. SendInput, {%rapidfirekey%
  684. Sleep, 10
  685. }
  686. }
  687.  
  688. ; $$$$$$$$$$ FUNCTION FOR KEYS THAT NEED MULTIPLE USES $$$$$$$$$$$$$$
  689. DualKeyHold(FirstKey, SecondKey, DelayBetweenKeys=400){
  690. SendInput, {%FirstKey%}
  691. Sleep %DelayBetweenKeys%
  692. If (GetKeyState(A_ThisHotkey, "P"))
  693. {
  694. SendInput, {%SecondKey% DOWN}
  695. Keywait, %A_ThisHotkey%
  696. SendInput, {%SecondKey% Up}
  697. }
  698. return
  699. }
  700.  
  701. ; $$$$$$$$$$$$$ HOLDS THE KEY UNTIL THE BUTTON IS RELEASED
  702. HoldKeyUntilRelease(KeyToHold){
  703. SendInput, {%KeyToHold% down}
  704. KeyWait, %A_ThisHotkey%
  705. SendInput, {%KeyToHold% up}
  706. }
  707.  
  708. ; $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ FUNCTION TO GET LT/RT STATES $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  709. GetTriggerState(ByRef LT, ByRef RT){
  710. Gui, 1: Default
  711. Gui, Submit, Nohide
  712. global RTV, LTV ; get the checkbox variables
  713. Loop, 4 {
  714. if XInput_GetState(A_Index-1, State)=0 {
  715. LT := json(State,"bLeftTrigger")
  716. RT := json(State,"bRightTrigger")
  717. XInput_SetState(A_Index-1, RT*257*RTV, LT*257*LTV) ; Vibrates for checked boxes
  718.  
  719. }
  720. }
  721. Return
  722. }
  723.  
  724. ; ############################## Additional needed scripts (I did not design these. See the top for info.) ####################
  725.  
  726. ; ################## json.ahk #########################
  727.  
  728. /*
  729. Function: JSON
  730.  
  731. Parameters:
  732. js - source
  733. s - path to element
  734. v - (optional) value to overwrite
  735.  
  736. Returns:
  737. Value of element (prior to change).
  738.  
  739. License:
  740. - Version 2.0 <http://www.autohotkey.net/~polyethene/#json>
  741. - Dedicated to the public domain <http://creativecommons.org/licenses/publicdomain/>
  742. */
  743. json(ByRef js, s, v = "") {
  744. j = %js%
  745. Loop, Parse, s, .
  746. {
  747. p = 2
  748. RegExMatch(A_LoopField, "([+\-]?)([^[]+)((?:\[\d+\])*)", q)
  749. Loop {
  750. If (!p := RegExMatch(j, "(?<!\\)(""|')([^\1]+?)(?<!\\)(?-1)\s*:\s*((\{(?:[^{}]++|(?-1))*\})|(\[(?:[^[\]]++|(?-1))*\])|"
  751. . "(?<!\\)(""|')[^\7]*?(?<!\\)(?-1)|[+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:,|$|\})", x, p))
  752. Return
  753. Else If (x2 == q2 or q2 == "*") {
  754. j = %x3%
  755. z += p + StrLen(x2) - 2
  756. If (q3 != "" and InStr(j, "[") == 1) {
  757. StringTrimRight, q3, q3, 1
  758. Loop, Parse, q3, ], [
  759. {
  760. z += 1 + RegExMatch(SubStr(j, 2, -1), "^(?:\s*((\[(?:[^[\]]++|(?-1))*\])|(\{(?:[^{\}]++|(?-1))*\})|[^,]*?)\s*(?:,|$)){" . SubStr(A_LoopField, 1) + 1 . "}", x)
  761. j = %x1%
  762. }
  763. }
  764. Break
  765. }
  766. Else p += StrLen(x)
  767. }
  768. }
  769. If v !=
  770. {
  771. vs = "
  772. If (RegExMatch(v, "^\s*(?:""|')*\s*([+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:""|')*\s*$", vx)
  773. and (vx1 + 0 or vx1 == 0 or vx1 == "true" or vx1 == "false" or vx1 == "null" or vx1 == "nul"))
  774. vs := "", v := vx1
  775. StringReplace, v, v, ", \", All
  776. js := SubStr(js, 1, z := RegExMatch(js, ":\s*", zx, z) + StrLen(zx) - 1) . vs . v . vs . SubStr(js, z + StrLen(x3) + 1)
  777. }
  778. Return, j == "false" ? 0 : j == "true" ? 1 : j == "null" or j == "nul"
  779. ? "" : SubStr(j, 1, 1) == """" ? SubStr(j, 2, -1) : j
  780. }
  781.  
  782. ; ########################## XInput.ahk ################################
  783. /*
  784. Function: XInput_Init
  785.  
  786. Initializes XInput.ahk with the given XInput DLL.
  787.  
  788. Parameters:
  789. dll - The path or name of the XInput DLL to load.
  790. */
  791. XInput_Init(dll="xinput1_3")
  792. {
  793. global
  794. if _XInput_hm
  795. return
  796.  
  797. ;======== CONSTANTS DEFINED IN XINPUT.H ========
  798.  
  799. ; NOTE: These are based on my outdated copy of the DirectX SDK.
  800. ; Newer versions of XInput may require additional constants.
  801.  
  802. ; Device types available in XINPUT_CAPABILITIES
  803. XINPUT_DEVTYPE_GAMEPAD = 0x01
  804.  
  805. ; Device subtypes available in XINPUT_CAPABILITIES
  806. XINPUT_DEVSUBTYPE_GAMEPAD = 0x01
  807.  
  808. ; Flags for XINPUT_CAPABILITIES
  809. XINPUT_CAPS_VOICE_SUPPORTED = 0x0004
  810.  
  811. ; Constants for gamepad buttons
  812. XINPUT_GAMEPAD_DPAD_UP = 0x0001
  813. XINPUT_GAMEPAD_DPAD_DOWN = 0x0002
  814. XINPUT_GAMEPAD_DPAD_LEFT = 0x0004
  815. XINPUT_GAMEPAD_DPAD_RIGHT = 0x0008
  816. XINPUT_GAMEPAD_START = 0x0010
  817. XINPUT_GAMEPAD_BACK = 0x0020
  818. XINPUT_GAMEPAD_LEFT_THUMB = 0x0040
  819. XINPUT_GAMEPAD_RIGHT_THUMB = 0x0080
  820. XINPUT_GAMEPAD_LEFT_SHOULDER = 0x0100
  821. XINPUT_GAMEPAD_RIGHT_SHOULDER = 0x0200
  822. XINPUT_GAMEPAD_A = 0x1000
  823. XINPUT_GAMEPAD_B = 0x2000
  824. XINPUT_GAMEPAD_X = 0x4000
  825. XINPUT_GAMEPAD_Y = 0x8000
  826.  
  827. ; Gamepad thresholds
  828. XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE = 7849
  829. XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE = 8689
  830. XINPUT_GAMEPAD_TRIGGER_THRESHOLD = 30
  831.  
  832. ; Flags to pass to XInputGetCapabilities
  833. XINPUT_FLAG_GAMEPAD = 0x00000001
  834.  
  835. ;=============== END CONSTANTS =================
  836.  
  837. _XInput_hm := DllCall("LoadLibrary" ,"str",dll)
  838.  
  839. if !_XInput_hm
  840. {
  841. MsgBox, Failed to initialize XInput: %dll%.dll not found.
  842. return
  843. }
  844.  
  845. _XInput_GetState := DllCall("GetProcAddress" ,"uint",_XInput_hm ,"str","XInputGetState")
  846. _XInput_SetState := DllCall("GetProcAddress" ,"uint",_XInput_hm ,"str","XInputSetState")
  847. _XInput_GetCapabilities := DllCall("GetProcAddress" ,"uint",_XInput_hm ,"str","XInputGetCapabilities")
  848.  
  849. if !(_XInput_GetState && _XInput_SetState && _XInput_GetCapabilities)
  850. {
  851. XInput_Term()
  852. MsgBox, Failed to initialize XInput: function not found.
  853. return
  854. }
  855. }
  856.  
  857. /*
  858. Function: XInput_GetState
  859.  
  860. Retrieves the current state of the specified controller.
  861.  
  862. Parameters:
  863. UserIndex - [in] Index of the user's controller. Can be a value from 0 to 3.
  864. State - [out] Receives the current state of the controller.
  865.  
  866. Returns:
  867. If the function succeeds, the return value is ERROR_SUCCESS (zero).
  868. If the controller is not connected, the return value is ERROR_DEVICE_NOT_CONNECTED (1167).
  869. If the function fails, the return value is an error code defined in Winerror.h.
  870. http://msdn.microsoft.com/en-us/library/ms681381.aspx
  871.  
  872. Remarks:
  873. XInput.dll returns controller state as a binary structure:
  874. http://msdn.microsoft.com/en-us/library/bb174834.aspx
  875. XInput.ahk converts this structure to a string, compatible with Titan's json():
  876. http://www.autohotkey.com/forum/topic34565.html
  877. */
  878. XInput_GetState(UserIndex, ByRef State)
  879. {
  880. global _XInput_GetState
  881.  
  882. VarSetCapacity(xiState,16)
  883.  
  884. if ErrorLevel := DllCall(_XInput_GetState ,"uint",UserIndex ,"uint",&xiState)
  885. State =
  886. else
  887. State := "{
  888. ( C LTrim Join
  889. 'dwPacketNumber':" NumGet(xiState,0) ",
  890. ; Seems less convenient - though more technically accurate - to require
  891. ; "Gamepad." prefix to access any of the useful properties with json().
  892. ;'Gamepad':{
  893. 'wButtons':" NumGet(xiState,4,"UShort") ",
  894. 'bLeftTrigger':" NumGet(xiState,6,"UChar") ",
  895. 'bRightTrigger':" NumGet(xiState,7,"UChar") ",
  896. 'sThumbLX':" NumGet(xiState,8,"Short") ",
  897. 'sThumbLY':" NumGet(xiState,10,"Short") ",
  898. 'sThumbRX':" NumGet(xiState,12,"Short") ",
  899. 'sThumbRY':" NumGet(xiState,14,"Short") "
  900. ;}
  901. )}"
  902.  
  903. return ErrorLevel
  904. }
  905.  
  906. /*
  907. Function: XInput_SetState
  908.  
  909. Sends data to a connected controller. This function is used to activate the vibration
  910. function of a controller.
  911.  
  912. Parameters:
  913. UserIndex - [in] Index of the user's controller. Can be a value from 0 to 3.
  914. LeftMotorSpeed - [in] Speed of the left motor, between 0 and 65535.
  915. RightMotorSpeed - [in] Speed of the right motor, between 0 and 65535.
  916.  
  917. Returns:
  918. If the function succeeds, the return value is 0 (ERROR_SUCCESS).
  919. If the controller is not connected, the return value is 1167 (ERROR_DEVICE_NOT_CONNECTED).
  920. If the function fails, the return value is an error code defined in Winerror.h.
  921. http://msdn.microsoft.com/en-us/library/ms681381.aspx
  922.  
  923. Remarks:
  924. The left motor is the low-frequency rumble motor. The right motor is the
  925. high-frequency rumble motor. The two motors are not the same, and they create
  926. different vibration effects.
  927. */
  928. XInput_SetState(UserIndex, LeftMotorSpeed, RightMotorSpeed)
  929. {
  930. global _XInput_SetState
  931. return DllCall(_XInput_SetState ,"uint",UserIndex ,"uint*",LeftMotorSpeed|RightMotorSpeed<<16)
  932. }
  933.  
  934. /*
  935. Function: XInput_GetCapabilities
  936.  
  937. Retrieves the capabilities and features of a connected controller.
  938.  
  939. Parameters:
  940. UserIndex - [in] Index of the user's controller. Can be a value in the range 0–3.
  941. Flags - [in] Input flags that identify the controller type.
  942. 0 - All controllers.
  943. 1 - XINPUT_FLAG_GAMEPAD: Xbox 360 Controllers only.
  944. Caps - [out] Receives the controller capabilities.
  945.  
  946. Returns:
  947. If the function succeeds, the return value is 0 (ERROR_SUCCESS).
  948. If the controller is not connected, the return value is 1167 (ERROR_DEVICE_NOT_CONNECTED).
  949. If the function fails, the return value is an error code defined in Winerror.h.
  950. http://msdn.microsoft.com/en-us/library/ms681381.aspx
  951. */
  952. XInput_GetCapabilities(UserIndex, Flags, ByRef Caps)
  953. {
  954. global _XInput_GetCapabilities
  955.  
  956. VarSetCapacity(xiCaps,20)
  957.  
  958. if ErrorLevel := DllCall(_XInput_GetCapabilities ,"uint",UserIndex ,"uint",Flags ,"uint",&xiCaps)
  959. Caps =
  960. else
  961. Caps := "{
  962. ( LTrim Join
  963. 'Type':" NumGet(xiCaps,0,"UChar") ",
  964. 'SubType':" NumGet(xiCaps,1,"UChar") ",
  965. 'Flags':" NumGet(xiCaps,2,"UShort") ",
  966. 'Gamepad':{
  967. 'wButtons':" NumGet(xiCaps,4,"UShort") ",
  968. 'bLeftTrigger':" NumGet(xiCaps,6,"UChar") ",
  969. 'bRightTrigger':" NumGet(xiCaps,7,"UChar") ",
  970. 'sThumbLX':" NumGet(xiCaps,8,"Short") ",
  971. 'sThumbLY':" NumGet(xiCaps,10,"Short") ",
  972. 'sThumbRX':" NumGet(xiCaps,12,"Short") ",
  973. 'sThumbRY':" NumGet(xiCaps,14,"Short") "
  974. },
  975. 'Vibration':{
  976. 'wLeftMotorSpeed':" NumGet(xiCaps,16,"UShort") ",
  977. 'wRightMotorSpeed':" NumGet(xiCaps,18,"UShort") "
  978. }
  979. )}"
  980.  
  981. return ErrorLevel
  982. }
  983.  
  984. /*
  985. Function: XInput_Term
  986. Unloads the previously loaded XInput DLL.
  987. */
  988. XInput_Term() {
  989. global
  990. if _XInput_hm
  991. DllCall("FreeLibrary","uint",_XInput_hm), _XInput_hm :=_XInput_GetState :=_XInput_SetState :=_XInput_GetCapabilities :=0
  992. }
  993.  
  994. ; TODO: XInputEnable, 'GetBatteryInformation and 'GetKeystroke.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement