Jump to content


Photo

XInput - Xbox 360 Controller API


  • Please log in to reply
40 replies to this topic

#1 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 21 December 2008 - 01:28 PM

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.

#2 Frankie

Frankie
  • Members
  • 2930 posts

Posted 24 December 2008 - 07:51 PM

Im getting a wireless adapter for the XBox 360 controller sometime soon. Is there any reason it won't work with wireless?

#3 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 25 December 2008 - 09:55 AM

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.

#4 Guests

  • Guests

Posted 29 May 2009 - 03:24 PM

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

#5 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 29 May 2009 - 03:49 PM

I doubt it. I don't own a chatpad.

#6 mtgtopdeck

mtgtopdeck
  • Members
  • 17 posts

Posted 29 May 2009 - 06:26 PM

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

#7 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 30 May 2009 - 05:23 AM

I'm not familiar with that community, where or what to post. Perhaps you'd better do it?

#8 TheLeO

TheLeO
  • Members
  • 264 posts

Posted 19 June 2009 - 04:10 PM

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 ..

#9 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 19 June 2009 - 07:07 PM

I'm not aware of any way.

#10 constell

constell
  • Guests

Posted 12 October 2009 - 02:56 AM

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?

#11 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 12 October 2009 - 07:38 AM

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.

#12 constell

constell
  • Guests

Posted 12 October 2009 - 08:05 AM

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?

#13 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 12 October 2009 - 08:49 AM

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.

#14 Matze

Matze
  • Guests

Posted 12 October 2009 - 12:41 PM

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.

#15 Lexikos

Lexikos
  • Administrators
  • 8855 posts

Posted 12 October 2009 - 01:00 PM

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).