-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJoyToKey.ahk
136 lines (118 loc) · 3.33 KB
/
JoyToKey.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#Persistent
#NoEnv
SetBatchLines -1
ListLines Off
; -------------------------------------------------------
; JoyToKeys: convert joystick movement to key presses
;
; settings!
; change these to fit your needs
; default is for Cyborg V.1
; --------------------------
HatUp := "Space"
HatDown := "c"
HatLeft := "a"
HatRight := "d"
XPlus := "Right"
XMinus := "Left"
YPlus := "Up"
YMinus := "Down"
RPlus := "e"
RMinus := "q"
Button1 := "LButton"
Button7 := "w" ;forward
Button2 := "l" ;lights
Button4 := "t" ;use
Button5 := "y" ;toggle power
Button6 := "p" ;lock landing legs
;comment in to test which button is which
;Loop, 32
; Button%a_index% := a_index
DeadZone := 7 ; percent of axis deadzone
Coarseness := 3 ; ms resolution of key dithering
SampleRate := 1 ; ms between samples
JoystickNum := 1 ; if you have more than one Joystick, select the right one here
; YOU SHOULD NOT CHANGE ANYTHING PAST THIS POINT
; ==============================================
SetTimer, SampleJoyEvent, %SampleRate%
return
MakeKey(Key, DownMode = -1) {
local Result
if (DownMode == true)
Result := "{" . Key . " down}"
else if (DownMode == false)
Result := "{" . Key . " up}"
else
Result := "{" . Key . "}"
return Result
}
Update(Key, NewState, ByRef KeyState) {
if (KeyState and not NewState) {
SendEvent % MakeKey(Key, false)
KeyState := false
}
if (not KeyState and NewState) {
SendEvent % MakeKey(Key, true)
KeyState := true
}
}
Dither(Key, Rate, ByRef KeyState) {
local TimeSet := Mod(A_TickCount, Coarseness)
if (TimeSet < Rate * Coarseness) {
Update(Key, true, KeyState)
}
if (TimeSet >= Rate * Coarseness) {
Update(Key, false, KeyState)
}
}
HatMode(Dir_) {
local Dir := Floor(Dir_)
Update(HatUp , Dir==7 or Dir==0 or Dir==1, HatUpState )
Update(HatRight, Dir==1 or Dir==2 or Dir==3, HatRightState)
Update(HatDown , Dir==3 or Dir==4 or Dir==5, HatDownState )
Update(HatLeft , Dir==5 or Dir==6 or Dir==7, HatLeftState )
}
CheckHalfAxis(VarName, Value, Plus) {
local Rate := 0
local HighBegin = 50 + DeadZone
local LowBegin = 50 - DeadZone
if not %VarName% == "" {
if Plus and Value >= HighBegin {
Rate := (Value - HighBegin) / (100 - HighBegin)
}
if not Plus and Value <= LowBegin {
Rate := (LowBegin - Value) / LowBegin
}
;MsgBox % VarName . " [" . %VarName% . "] " . Plus . " " . Value . " rate " . Rate
Dither(%VarName%, Rate, %VarName%State)
}
}
CheckAxis(VarName, Value) {
CheckHalfAxis(VarName . "Plus", Value, true)
CheckHalfAxis(VarName . "Minus", value, false)
}
UpdateJoy() {
local JoyX, JoyY, JoyZ, JoyR, JoyPOV
GetKeyState, JoyX, %JoystickNum%JoyX
GetKeyState, JoyY, %JoystickNum%JoyY
GetKeyState, JoyZ, %JoystickNum%JoyZ
GetKeyState, JoyR, %JoystickNum%JoyR
GetKeyState, JoyPOV, %JoystickNum%JoyPOV
CheckAxis("X", JoyX)
CheckAxis("Y", JoyY)
CheckAxis("Z", JoyZ)
CheckAxis("R", JoyR)
Loop, 32 {
if Button%a_index% {
local Joy
GetKeyState, Joy, %JoystickNum%Joy%a_index%
Update(Button%a_index%, Joy == "D", Joy%a_index%State)
}
}
if (JoyPOV == -1)
HatMode(-1)
else
HatMode(JoyPOV / 4500)
}
SampleJoyEvent:
UpdateJoy()