Skip to content

Instantly share code, notes, and snippets.

@maestrith
Created June 12, 2014 05:55
Show Gist options
  • Save maestrith/9c82e37ea653f149b95f to your computer and use it in GitHub Desktop.
Save maestrith/9c82e37ea653f149b95f to your computer and use it in GitHub Desktop.
Posted using AHK Studio
#Persistent
#SingleInstance,Force
ctrl:=new xbox()
;ctrl.mouse({controller:0,stick:2,invert:-1,R:"RButton"}) ;L for right toggle.
ctrl.Mouse({controller:0,stick:2,invert:-1}) ;stick 1=left 2=right 3=both
buttons:=["A","B","X","Y","Back","Start","LeftShoulder","RightShoulder","LeftThumb","RightThumb","Up","Down","Left","Right"]
axis:={LThumbx:["A","D"],lthumby:["S","W"]}
Alt:={LTrigger:{Up:"R",X:3,Y:4,B:5,A:6},RTrigger:{Up:"R",X:7,Y:8,B:9,A:0}} ;Alternate keys when holding the main
dead:={LThumbX:70,LThumbY:70,LTrigger:50,RTrigger:50}
keys:={Y:"1",B:"2",X:"F",A:"Space",LeftShoulder:"RButton",RightShoulder:"LButton",Left:"Q",Right:"E",Start:"M",Back:"Escape",UP:["shift","RButton"],Down:"S",Left:"A",Right:"D",RightThumb:"Tab"}
Toggle:={leftthumb:"RButton"} ;holds the button/mouse then release on press
return
+Escape::
ExitApp
return
;https://gist.github.com/9c82e37ea653f149b95f
;make it so you can toggle look
;split up dead zone, increase dead zone when look mode
getstate(state){
global keys,mouse,axis,dead,buttons,toggle,ctrl,toggle,alt
static lastrsb,keystate:=[],pressed:=[]
for a,b in toggle{
if (state.1[a]&&keystate.prev[a]="")
keystate.prev[a]:=1
if (state.1[a]=0&&keystate.prev[a])
xbox.Send(b,GetKeyState(b,"P")?"Up":"Down"),keystate.prev[a]:=""
}
for a,b in keystate.prev
if !(state.1[a])
xbox.Send(b,"Up"),keystate.prev.remove(a)
for a,b in axis{
vv:=state.1[a]>dead[a]?2:state.1[a]<-dead[a]?1:""
if keystate.prev[a]!=axis[a,vv]
xbox.Send(keystate.prev[a],"Up"),keystate.prev.Remove(a)
}
for value in axis{
vv:=state.1[value]>dead[value]?2:state.1[value]<-dead[value]?1:""
if (vv){
keystate[value]:=axis[value,vv]
if !(keystate.prev[value])
keystate.prev[value]:=axis[value,vv],xbox.Send(axis[value,vv],"Down")
}
}
for a,b in alt{
if state.1[a]
for c,d in b{
if (state.1[c]&&pressed[c]="")
xbox.Send(d,"Down"),pressed[c]:=1
else if (state.1[c]=0&&pressed[c])
xbox.Send(d,"Up"),pressed[c]:=""
}
}
for a,b in keys{
key:=keys[a]
if pressed[a]
Continue
if (state.1[a]&&keystate.prev[a]="")
xbox.send(b,"Down"),keystate.prev[a]:=b
}
if state.1.back&&state.1.start
ExitApp
}
class xbox{
static mousekeys:={LButton:"left",RButton:"right"},Buttons:={Up:1,Down:2,Left:4,Right:8,Start:16,Back:32,LeftThumb:64,RightThumb:128,LeftShoulder:256,RightShoulder:512,A:0x1000,B:0x2000,X:0x4000,Y:0x8000}
__New(count=1){
static
dll:=InStr(A_OSVersion,8)?"Xinput1_4":"Xinput1_3",this.library:=DllCall("LoadLibrary","str",dll),this.ctrl:=[],VarSetCapacity(move,28),this.count:=count,main:=this,this.move:=&move,VarSetCapacity(State,16)
NumPut(0,move,8),NumPut(0,move,12),NumPut(0x0001,move,16)
if !(this.library){
m("Error loading the DLL")
ExitApp
}
for a,b in {xGetState:"XInputGetState",xBattery:"XInputGetBatteryInformation",xSetState:"XInputSetState",xinputid:"XInputGetKeystroke"}
this[a]:=DllCall("GetProcAddress","ptr",this.library,"astr",b)
SetTimer,getstate,30
return this
getstate:
Loop,% main.count{
DllCall(main.xGetState,"uint",A_Index-1,uptr,&State),index:=A_Index
for a,b in {LTrigger:6,RTrigger:7}
main.ctrl[Index,a]:=NumGet(State,b,"uchar")
for a,b in {LThumbX:8,LThumbY:10,RThumbX:12,RThumbY:14}
value:=NumGet(state,b,"short"),main.ctrl[index,a]:=Floor(value>0?value/32767*100:value/32768*100)
buttons:=NumGet(State,4),total:=1
for a,b in main.Buttons
main.ctrl[Index,a]:=buttons&b?1:0
getstate(main.ctrl)
if main.mousectrl[A_Index-1].stick
main.xMouse(A_Index)
}
Return
}
xinput(controller){
VarSetCapacity(input,12)
DllCall(this.xinputid,uint,controller,int,0,uptr,&input)
if number:=NumGet(input,0,"uint")&0xff=6
m(number&0xff,NumGet(input,4,"uint"))
}
Battery(Controller){
VarSetCapacity(batt,4),DllCall(this.xBattery,"uint",Controller,"uint",0,"uptr",&batt)
Return NumGet(batt,1)
}
Vibrate(Controller=0,large=0,small=0){
DllCall(this.xSetState,"uint",controller,"uint*",large|small<<16)
}
Mouse(info){
this.mousectrl[info.controller]:=info
}
Send(key,state){
if (key="")
return
if IsObject(key){
if (state="Down"){
for a,b in key{
if this.mousekeys[b]{
MouseClick,% this.mousekeys[b]
;m(this.mousekeys[b])
}
Else{
ControlSend,,{%b% Down},A
;m(b)
Sleep,100
}
}
for a,b in key
if !this.mousekeys[b]
ControlSend,,{%b% Up},A
}
}
else{
if this.mousekeys[key]
MouseClick,% this.mousekeys[key],,,,,%state%
else{
ControlSend,,{%key% %state%},A
}
}
}
xMouse(controller){
static toggle:=[],axis:={1:["LThumbX","LThumbY"],2:["RThumbX","RThumbY"],3:["LThumbX","LThumbY","RThumbX","RThumbY"]}
main:=this.ctrl[controller],ctrl:=this.mousectrl[controller-1],dd:=[]
for a,b in axis[ctrl.stick]
if (Abs(main[b])>25)
dd[SubStr(b,0,1)]:={value:main[b],which:SubStr(b,1,1)}
offsetx:=dd.x.value,offsety:=dd.y.value
stick:=dd.x.which?dd.x.which:dd.y.which?dd.y.which:""
if (ctrl[stick]&&(toggle[ctrl[stick]]!=1))
key:=ctrl[stick],this.Send(key,"Down"),toggle[ctrl[stick]]:=1
if (ctrl[stick]=""&&toggle.prev)
this.Send(toggle.prev,"up"),toggle[toggle.prev]:=""
if (Abs(offsetx)>25||Abs(offsety)>25){
movex:=Floor(offsetx/6),movey:=(Floor(offsety/6)*ctrl.invert)
NumPut(movex,this.move,4),NumPut(movey,this.move,8)
DllCall("User32\SendInput",uint,1,uptr,this.move,uint,28)
}
toggle.prev:=ctrl[stick]
}
}
m(x*){
for a,b in x
list.=b "`n"
msgbox %list%
}
t(x*){
for a,b in x
list.=b "`n"
tooltip %list%
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment