Last active
September 14, 2021 16:59
-
-
Save poisonborz/33a75f43f186e6e6b17be39be13aeb15 to your computer and use it in GitHub Desktop.
Triggering keyboard shortcuts with an XBox (XInput compatible) controller on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Triggers a keyboard shortcut (defined on ln41, currently Ctrl-Alt-F4) | |
when a specific XBox controller key combination (defined on ln40, currently LB-RB-LT-RT) | |
on any of a max of 4 controller is pressed. Polls and fires (if controller keys held down) every 3 seconds. | |
You need 2 additional files for this to work, place them besides this file: | |
- copy the text here and save it as a file named XInput.ahk: https://www.dropbox.com/s/3ky3wx5stj0uoj9/XInput.ahk?dl=0 | |
- copy xinput1_3.dll from your C:\Windows\System32 folder (should be there if you have DirectX installed) | |
Download Ahk2Exe from here: https://github.com/AutoHotkey/Ahk2Exe/releases/download/Ahk2Exe__v1.1.34.00_Beta_4/Ahk2Exe1.1.34.00_Beta_4.zip | |
And use to compile this file to an .exe file. | |
Running it creates a green tray icon (with which the app can be closed), starting the listening for keypresses immediately. | |
*/ | |
#include xinput.ahk | |
XInput_Init() | |
Global aInput := {UP: 0x0001 | |
, DOWN: 0x0002 | |
, LEFT: 0x0004 | |
, RIGHT: 0x0008 | |
, START: 0x0010 | |
, BACK: 0x0020 | |
, LB: 0x0040 | |
, RB: 0x0080 | |
, LT: 0x0100 | |
, RT: 0x0200 | |
, GUIDE: 0x0400 ; Undocumented, the XBox logo key | |
, A: 0x1000 | |
, B: 0x2000 | |
, X: 0x4000 | |
, Y: 0x8000 } | |
Loop { | |
Loop, 4 { ; loop through 4 controllers | |
if State := XInput_GetState(A_Index-1) { | |
buttons := getKeyList(State) | |
if (buttons.IndexOf("LB") && buttons.IndexOf("RB") && buttons.IndexOf("LT") && buttons.IndexOf("RT")) { | |
Send {Ctrl down}{Alt down}{F4 down}{Ctrl up}{Alt up}{F4 up} | |
} | |
} | |
} | |
Sleep, 3000 | |
} | |
getButtonList(value) { | |
Global aInput | |
keylist := [] | |
for name, bit in aInput { | |
if (bit & value) { | |
keylist.Push(name) | |
} | |
} | |
return keylist | |
} | |
getKeyList(State) { | |
keys := getButtonList(State.wButtons) ; returns buttons, shoulders and guide state | |
if ((State.bLeftTrigger > 10) ? State.bLeftTrigger : 0) { | |
keys.Push("LT") | |
} | |
if ((State.brightTrigger > 10) ? State.brightTrigger : 0) { | |
keys.Push("RT") | |
} | |
return keys | |
} | |
Array(items*) | |
{ | |
items.base := ArrayEx | |
return items | |
} | |
; add index search in arrays | |
class ArrayEx { | |
IsArray[] { | |
get { | |
return true | |
} | |
} | |
IndexOf(item, case_sensitive:=false) { | |
for i, val in this { | |
if (case_sensitive ? (val == item) : (val = item)) | |
return i | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment