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
Matze
  • Guests
  • Last active:
  • Joined: --
But in the game cotnroller properties tab in the control panel it show that there is just one axis.

I must do something wrong.
Can someone explain it step by step, like how should I load the xinput and stuff?

Thank u guys

constell
  • Guests
  • Last active:
  • Joined: --
As Lexikos said, these scripts won't make the game recognize the 2 triggers as different axes. Hence, it won't change anything in game controller properties.

Thanks a lot for that script, Lexikos. Unfortunately, the game doesn't allow mixing keyboard and controller input. Right now, I'm working on making triggers translate into the thumbstick clicks keys as an alternative. I'm just figuring out how to do that, since I think the 360 buttons are not mapped into Joy1, Joy2, etc.

Anyway, hold on Matze. I'll share the script once it works. I'm sure it'll work for NBA 2K, since that game doesn't use the thumbstick clicks that often.

Matze
  • Guests
  • Last active:
  • Joined: --
I'll wait for that script constell. Hope u'll make a step by step guide for ppl like me :)
Thanks

The Naked General
  • Members
  • 21 posts
  • Last active: Jan 27 2012 02:41 AM
  • Joined: 22 Feb 2009
Here is a working demonstration of using the xinput to read each trigger as a modifier for other joy buttons

Joy1::
if (LT = 1 and RT = 1) ;Both triggers are held
AVAR = 7
if (LT = 0 and RT = 0) ;No triggers are held
AVAR = 1
if (LT = 1 and RT = 0) ;Left trigger is held
AVAR = 5
if (LT = 0 and RT = 1) ;Right trigger is held
AVAR = 9
Send {%AVAR% down}
SetTimer, WaitForButtonUpA, 30
return

WaitForButtonUpA:
if not GetKeyState("Joy1")  ; The button has been released.
{
    Send {%AVAR% up}  ; Release the button.
    SetTimer, WaitForButtonUpA, off  ; Stop monitoring the button.
    return
}
Send {%AVAR% down}  ; Send another keystroke. This is for physical key emulation.
return
This example is mapping the A button to four different number keys, also you will have to define the trigger code before this executes like with Lexikos' example earlier. But in my case I set the send keys to null
; Keys to bind to triggers.
LT_Key = Null
RT_Key = Null
And then used the bool value of RT and LT rather than mapping it to a key and then trying to remap it.

Thanks to Lexikos btw for this awesome script :D
"lol, i made this thing, but it didn't work... so I read the forums and now it does!"

Matze
  • Guests
  • Last active:
  • Joined: --
What this actually means? Should I paste it on the xinput script together with the rest of the Lexikos stuff posted in the forum? Should I add some values in the script? Lets dont forget that I'm a noob :)

constell
  • Guests
  • Last active:
  • Joined: --
Hey General, does that code actually work in remapping the triggers to other buttons? I can't test it right now since I'm at work. I tried working on the script a couple of days ago, and followed the guidelines on this page as Lexikos suggested.

http://www.autohotke...mapJoystick.htm

At the top of the page it states "Although a joystick button or axis can be remapped to become a key or mouse button, it cannot be remapped to some other joystick button or axis. That would be possible only with the help of a joystick emulator such as PPJoy."

This made me think that what I was planning to do was impossible so I kind of gave up on it. (trying to map LT and RT to buttons 9 and 10). If your script works, though then there shouldn't be any problems, though.

The Naked General
  • Members
  • 21 posts
  • Last active: Jan 27 2012 02:41 AM
  • Joined: 22 Feb 2009
Hi Constell,
My post may have been a little misleading, it is not for remapping the joy buttons, but rather more for a keyboard control layout in which the trigger buttons become modifier keys for the other joy buttons. Where in my example if you press the A button on the controller with no triggers held, it will send key one. Then if holding the left trigger it will send key five, and key nine for right and seven for both triggers. The difference with my example from the one Lexikos posted, is mine is not actually mapping the triggers to a button, but using them as sort of shift keys for the rest of the buttons giving you four variations for each button.

Edit: I also cleaned a syntax error in my first post.

Matze, what you want to do to get this script to work is download the xinput.ahk AND the JSON.ahk, then put them in the same directory with the script that YOU write. At the top of your script put:
#include xinput.ahk
#include JSON.ahk
And what you should know is, these scripts do not remap any of the buttons/axis/triggers but simply give you the ability to access them as they were intended to. You can get the button information from the controller with these scripts as well, but they are represented as an accumulating value, which is why using the remap joystick script is a good idea for the actual buttons. You should look over the remapping joystick and joystick as a mouse scripts for the best way to remap the actual buttons. The xinput is best used for the axis controls like the left and right joysticks, and the triggers, and also gives you access to the vibration feature in the controller as demonstrated in Lexikos first post. To give you some idea of how it goes, try this in your script that is in the same folder as xinput and JSON:
#include xinput.ahk
#include JSON.ahk

XInput_Init()
Loop {
    Loop, 4 {
        if XInput_GetState(A_Index-1, State)=0 {
            LT := json(State,"bLeftTrigger")
            RT := json(State,"bRightTrigger")
            BT := json(State,"wButtons")
            XInput_SetState(A_Index-1, LT*257, RT*257)
        }
    }
Tooltip LTrigger-%LT%     RTrigger-%RT%`n  Button Info-%BT%
    Sleep, 100
}
This is a small script that generates a tooltip to display information about the buttons and triggers being pressed, it also makes the respective motors vibrate when the trigger is pressed.

Lexikos, I have been looking over the GetKeystroke functions on MSDN and will try to follow your examples to see if I can get those funtions to work. I was also wondering if you have yet to see any info on the chatpad ever being supported at all in windows?

Thanks :D
"lol, i made this thing, but it didn't work... so I read the forums and now it does!"

k9burn
  • Guests
  • Last active:
  • Joined: --
Where does the dll have to be? In the same directory as the scripts or somewhere else?

Do you need the directX SDK installed?

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I think the DirectX Run-time should be sufficient. xinput1_3.dll may be anywhere the script can find it; i.e. any of the locations listed in the PATH environment variable (usually \Windows\System32) or the script's own directory.

darkangael
  • Members
  • 4 posts
  • Last active: Feb 03 2010 10:48 PM
  • Joined: 18 Jun 2007
The button info is actually more of a BIT register than an accumulating value. On my XBOX controller the A,B,X,Y buttons are bits 12, 13, 14 and 15 of the register.

Example:
To discover if the A button is pressed, store the value of the register as variable buttoninfo then
a_pressed := buttoninfo & 4096


a_pressed will equal zero if the button is not pressed and 4096 if it is, regardless of what other things are going on in the register.

Having said this, remap joystick may be easier (I haven't used that one as my only difficulty was getting the shoulder buttons to work in devil may cry 3).

LinuxDolt
  • Members
  • 2 posts
  • Last active: Aug 31 2010 04:54 AM
  • Joined: 31 Aug 2010
I made a little helper library with two new functions that make it easier to determine if certain buttons are down using some easily remembered button names in your code instead of trying to remember all the constants all the time. The following code is Public Domain. I encourage it's use for whatever you want, I'm using it for my own collection of game control scripts I'm in process of making.

XInputButtonMath.ahk
; Constants for gamepad buttons
PovUp       = 0x0001
PovDown     = 0x0002
PovLeft     = 0x0004
PovRight    = 0x0008
Start       = 0x0010
Back        = 0x0020
LStick      = 0x0040
RStick      = 0x0080
LB          = 0x0100
RB          = 0x0200
A           = 0x1000
B           = 0x2000
X           = 0x4000
Y           = 0x8000

XInputButtonIsDown( ButtonName, bidButtonState )
{
   isDown := false  ; If something screws up, we want to return false.
   
   If ( bidButtonState & %ButtonName% )
      isDown := true  ; Return true if bidButtonState matches ButtonName
   Else isDown := false  ; Return false otherwise
   
   Return %isDown%
}

XInputButtonState( bsButtonState )
{
   Status := ""
   
   If XInputButtonIsDown( "A", bsButtonState )
      Status .= "A,"
   If XInputButtonIsDown( "B", bsButtonState )
      Status .= "B,"
   If XInputButtonIsDown( "X", bsButtonState )
      Status .= "X,"
   If XInputButtonIsDown( "Y", bsButtonState )
      Status .= "Y,"
   If XInputButtonIsDown( "LB", bsButtonState )
      Status .= "LB,"
   If XInputButtonIsDown( "RB", bsButtonState )
      Status .= "RB,"
   If XInputButtonIsDown( "LStick", bsButtonState )
      Status .= "LStick,"
   If XInputButtonIsDown( "RStick", bsButtonState )
      Status .= "RStick,"
   If XInputButtonIsDown( "Back", bsButtonState )
      Status .= "Back,"
   If XInputButtonIsDown( "Start", bsButtonState )
      Status .= "Start,"
   If XInputButtonIsDown( "PovUp", bsButtonState )
      Status .= "PovUp,"
   If XInputButtonIsDown( "PovDown", bsButtonState )
      Status .= "PovDown,"
   If XInputButtonIsDown( "PovLeft", bsButtonState )
      Status .= "PovLeft,"
   If XInputButtonIsDown( "PovRight", bsButtonState )
      Status .= "PovRight,"
   
   Return SubStr( Status, 1, -1 ) ; Omit the trailing comma
}


musketball
  • Guests
  • Last active:
  • Joined: --
Thanks to the users here (polyethene and Lexikos), I made a 2-slider GUI that lets you set the speed of the left and right motors independently. It's a Xbox 360 personal massager. :lol: You can leave it on for as long as you want, although I don't know how long the motor can run continuously without becoming damaged. Some people have written XNA games for Xbox Live to do this. It's exactly what I was hoping to find here. You gave me the tools to create it easily.

#include json.ahk
#include XInput.ahk


Gui, Add, Slider, w200 vaa gsave AltSubmit Page16 Range0-255 TickInterval16 ToolTip, 1
Gui, Add, Slider, w200 vbb gsave AltSubmit Page16 Range0-255 TickInterval16 ToolTip, 1

Gui, Show,, Nerd Vibes
Gosub save
return

save:
Gui, Submit, Nohide
DoControl(aa,bb)
return

DoControl(vibr1, vibr2) {
XInput_Init()
    Loop, 4 {
        if XInput_GetState(A_Index-1, State)=0 {
            XInput_SetState(A_Index-1, vibr1*257, vibr2*257)
        }
    }

return
}

GuiClose:
ExitApp


AmazingArjan
  • Members
  • 2 posts
  • Last active: Jun 11 2011 01:11 PM
  • Joined: 10 Jun 2011
Hi Lexikos and team,

Thanks for your great work and all the examples.
I'd like to remap some keys on my Xbox360 wired controller, but keep getting this error message from XInput.ahk

"Failed to initialize XInput: function not found."

Do you have any idea what might be causing this message?

I also tried the program "JoyToKey", which works, but off course I'd like to use Ahk for this....


Note:
- I use a wired Original Microsoft Xbox360 controller. (without chatpad etc.)

Question:
- Does anybody know if and how Xinput.ahk could work with the ChatPad connected to the Xbox360 controller ?

Daniel_
  • Members
  • 16 posts
  • Last active: Jun 21 2011 04:10 AM
  • Joined: 15 May 2011

Hi Lexikos and team,

Thanks for your great work and all the examples.
I'd like to remap some keys on my Xbox360 wired controller, but keep getting this error message from XInput.ahk

"Failed to initialize XInput: function not found."

Do you have any idea what might be causing this message?


I'm having the exact same problem, i'm sure i have xinput.dll here ...
Any sugestions?

JPFlash
  • Guests
  • Last active:
  • Joined: --
I too ran into the message:
"Failed to initialize XInput: function not found."

I poured through the code myself and couldn't find anything wrong with it. After doing some heavy research I came across something and the answer hit me like a ton of bricks.

In the XInput_Init() function, GetProcAddress is used to get the function addresses of the XInputGetState, XInputSetState, and XInputGetCapabilities from the xinput1_3.dll. Unfortunately, GetProcAddress doesn't work in unicode.

For those of us that have installed the most recent version of AutoHotKey and/or are running on 64-bit systems are using the unicode version of AutoHotKey.

I installed the 32-bit ANSI version of AHK and XInput ran flawlessly. Looks like we 64-bit users need to find an alternative method instead of GetProcAddress or just use the 32-bit version of AHK.