Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

XInput - Xbox 360 Controller API


  • Please log in to reply
57 replies to this topic
Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
XInput

XInput.ahk wraps a few key functions of XInput, a.k.a. the Microsoft Common Controller API. It provides the following capabilities not otherwise accessible to scripts:
  • The official drivers report to Windows one axis for both analog triggers, making it impossible to detect when both triggers are pulled at once. XInput allows one to retrieve the state of each analog trigger individually.
  • Set level of controller vibration - left and right motor speeds.
Other capabilities of the XInput API that this script does not currently support include:
  • Detect input on attached messenger kits (mini keyboards which can be attached to a controller).
  • Retrieve battery levels of wireless controllers.
  • Access the audio input/output of a given controller's headset.
Required: xinput1_3.dll
This should be included in recent DirectX run-times.
Usage notes for each function are included in XInput.ahk.

XInput.ahk for AutoHotkey 1.1
This version uses objects, so AutoHotkey 1.1 (AutoHotkey_L) is required.
; Example: Control the vibration motors using the analog triggers of each controller.
XInput_Init()
Loop {
Loop, 4 {
if State := XInput_GetState(A_Index-1) {
LT := State.bLeftTrigger
RT := State.bRightTrigger
XInput_SetState(A_Index-1, LT*257, RT*257)
}
}
Sleep, 100
}
XInput.ahk for AutoHotkey 1.0
This version uses JSON strings, and should work on any version of AutoHotkey. The example below requires json().
; Example: Control the vibration motors using the analog triggers of each controller.
XInput_Init()
Loop {
Loop, 4 {
if XInput_GetState(A_Index-1, State)=0 {
LT := json(State,"bLeftTrigger")
RT := json(State,"bRightTrigger")
XInput_SetState(A_Index-1, LT*257, RT*257)
}
}
Sleep, 100
}
XInput.ahk is basically public domain.

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
Im getting a wireless adapter for the XBox 360 controller sometime soon. Is there any reason it won't work with wireless?
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I originally tested the script with one wireless controller. After your post, I also connected a wired controller - the script worked correctly with both controllers connected. It's easy to identify the index of a controller as it directly relates to the lit quadrant on the guide button.

  • Guests
  • Last active:
  • Joined: --
hi, is there anybody still working on it? would love to have the abbility to connect a xbox 360 controller wth my pc and use the chatpad and output sound to headset

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I doubt it. I don't own a chatpad.

mtgtopdeck
  • Members
  • 17 posts
  • Last active: Nov 01 2010 10:03 PM
  • Joined: 25 Mar 2009
There is a community of xbox users that might find this very useful.
If you get the time could you post about this script on the forums at xim360.com?
thank you

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I'm not familiar with that community, where or what to post. Perhaps you'd better do it?

TheLeO
  • Members
  • 264 posts
  • Last active: Jan 02 2012 01:51 AM
  • Joined: 11 Jun 2005
Thank you so much for this, I've been trying to figure out how to make xinput work with ahk for quite a while.

BTW, is there anyway to determine if the user presses the big "X" button on the controller? (or taps it)//

thanks again, I'll be testing it ..
::
I Have Spoken
::

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I'm not aware of any way.

constell
  • Guests
  • Last active:
  • Joined: --
For some reason, this doesn't work for me. Running the xinput.ahk file with the dll in the same directory. It just shows the tray icon for a split second then quits with no error message. What's up?

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
XInput.ahk doesn't do anything on its own; it's a library of functions for use in other scripts such as the example in my first post.

constell
  • Guests
  • Last active:
  • Joined: --
Ah, thanks for that. Sorry for bumping a pretty old post. Anyway, my original intention was to somehow find a way to overcome the current limitation of the 360 driver to detect when both triggers are pressed at once.

Looks like it isn't actually a limitation considering Xinput can actually detect when both triggers are pressed. There's a game that I wanna play that has moves where you have to press both triggers at once. I can download a 3rd party driver, but that would affect games that detect Xinput devices (GRiD, GTA4) and I prefer to still have that.

What's the simplest way I can do that? I'm thinking of maybe scripting something like joy2key, where it maps the controller input to the keyboard, hopefully being able to detect both triggers pressed at once. Then passing that onto the game. Problem is, I don't know anything about AutoHotKey scripting. I can learn, though. Anyway, is there an easier way you can think of other than this?

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
That limitation is the main reason I wrote XInput.ahk. If the game supports mixing keyboard and game controller input, you may use this to remap just the triggers:
; ~ Config:

; Minimum to be considered "pressed", between 1 (0.39%) and 255 (100%).
Threshold = 64

; Keys to bind to triggers.
LT_Key = [
RT_Key = ]

; ~

LastRT := LastLT := 0
XInput_Init()
Loop {
    Loop, 4 {
        if XInput_GetState(A_Index-1, State)=0 {
            LT := json(State,"bLeftTrigger") >= Threshold
            RT := json(State,"bRightTrigger") >= Threshold
            if (LT != LastLT) {
                Send % "{" . LT_Key . (LT ? " Down}" : " Up}")
                LastLT := LT
            }
            if (RT != LastRT) {
                Send % "{" . RT_Key . (RT ? " Down}" : " Up}")
                LastRT := RT
            }
        }
    }
    Sleep, 100
}
If you need to remap other buttons/axes, the methods outlined here may be sufficient.

Matze
  • Guests
  • Last active:
  • Joined: --
I play NBA 2K for PC with xbox 360 Wireless contoller. The isomotion on the game is executed if the two triggers are pressed simultaneously, which because of the one axis doesnt do nothing actually.
Well I sense this could be a solution for it, but tried to run the script after I installe AutoHotkey but nothing changes.

Is there something I need to do in order to make it work, change something in the script, add some values or..? I'm just regular PC user and haven't been working with these kind of stuff till now.

PS. I have the xinput1_3.dll, I've searched for it.

Thank u guys, ur help would be very appreciated.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
Obviously, the script can't make the game recognize the two triggers as separate axes. It only translates the triggers into two keys: [ and ]. You need to either bind these keys to something in your game, or change the keys in the script (simply replace the [ and/or ] characters).