AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

XInput - Xbox 360 Controller API
Goto page Previous  1, 2, 3  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Matze
Guest





PostPosted: Mon Oct 12, 2009 1:28 pm    Post subject: Reply with quote

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
Back to top
constell
Guest





PostPosted: Mon Oct 12, 2009 5:45 pm    Post subject: Reply with quote

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.
Back to top
Matze
Guest





PostPosted: Mon Oct 12, 2009 10:12 pm    Post subject: Reply with quote

I'll wait for that script constell. Hope u'll make a step by step guide for ppl like me Smile
Thanks
Back to top
The Naked General



Joined: 22 Feb 2009
Posts: 21
Location: Dallas TX

PostPosted: Wed Oct 14, 2009 6:53 am    Post subject: Reply with quote

Here is a working demonstration of using the xinput to read each trigger as a modifier for other joy buttons

Code:
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
Code:
; 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 Very Happy
_________________
"lol, i made this thing, but it didn't work... so I read the forums and now it does!"


Last edited by The Naked General on Wed Oct 14, 2009 5:04 pm; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
Matze
Guest





PostPosted: Wed Oct 14, 2009 9:43 am    Post subject: Reply with quote

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 Smile
Back to top
constell
Guest





PostPosted: Wed Oct 14, 2009 5:00 pm    Post subject: Reply with quote

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.autohotkey.com/docs/misc/RemapJoystick.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.
Back to top
The Naked General



Joined: 22 Feb 2009
Posts: 21
Location: Dallas TX

PostPosted: Wed Oct 14, 2009 5:40 pm    Post subject: Reply with quote

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:
Code:
#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:
Code:
#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 Very Happy
_________________
"lol, i made this thing, but it didn't work... so I read the forums and now it does!"
Back to top
View user's profile Send private message MSN Messenger
k9burn
Guest





PostPosted: Fri Oct 16, 2009 6:38 am    Post subject: dll Reply with quote

Where does the dll have to be? In the same directory as the scripts or somewhere else?

Do you need the directX SDK installed?
Back to top
Lexikos



Joined: 17 Oct 2006
Posts: 7299
Location: Australia

PostPosted: Fri Oct 16, 2009 8:41 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
darkangael



Joined: 18 Jun 2007
Posts: 4

PostPosted: Wed Feb 03, 2010 10:48 pm    Post subject: Reply with quote

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
Code:
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).
Back to top
View user's profile Send private message
LinuxDolt



Joined: 31 Aug 2010
Posts: 2

PostPosted: Tue Aug 31, 2010 1:23 am    Post subject: XInputButtonMath Reply with quote

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
Code:
; 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
}
Back to top
View user's profile Send private message
musketball
Guest





PostPosted: Sat Feb 26, 2011 10:21 pm    Post subject: Reply with quote

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

Code:
#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
Back to top
AmazingArjan



Joined: 10 Jun 2011
Posts: 2
Location: The Netherlands

PostPosted: Fri Jun 10, 2011 9:25 am    Post subject: Error message from XInput... Reply with quote

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 ?
Back to top
View user's profile Send private message
Daniel_



Joined: 15 May 2011
Posts: 16

PostPosted: Tue Jun 21, 2011 2:13 am    Post subject: Re: Error message from XInput... Reply with quote

Quote:

AmazingArjan wrote:
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?
Back to top
View user's profile Send private message
JPFlash
Guest





PostPosted: Tue Jun 21, 2011 8:18 am    Post subject: GetProcAddress Does Not Work In Unicode Reply with quote

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.
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group