AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

Post your working scripts, libraries and tools
edipoReboucas
Posts: 4
Joined: 02 Dec 2015, 10:19
GitHub: edipoReboucas

AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

02 Dec 2015, 10:24

AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

GitHub: https://github.com/edipoReboucas/AHKXbox360Lib

Usage:

Code: Select all

;Include the Library
#Include ../XBOX360.ahk

;Instance the Controller Manager
manager := new Xbox360LibControllerManager()

;Initialize Controller (0-3), you can use 4 controllers
player1 := manager.InitializeController(0)


;Make something useful
Loop {
    msg := ""
;Update state of controller

    player1.Update()

    if (player1.GUIDE) {
        msg .= "Player One Press Guide`n"
    }

    if (player1.BACK) {
        msg .= "Player One Press Back`n"
    }

    if (player1.START) {
        msg .= "Player One Press Start`n"
    }

    if (player1.UP) {
        msg .= "Player One Press Up`n"
    }

    if (player1.DOWN) {
        msg .= "Player One Press Down`n"
    }

    if (player1.LEFT) {
        msg .= "Player One Press Left`n"
    }

    if (player1.RIGHT) {
        msg .= "Player One Press Right`n"
    }

    if (player1.A) {
        msg .= "Player One Press A`n"
    }

    if (player1.B) {
        msg .= "Player One Press B`n"
    }

    if (player1.X) {
        msg .= "Player One Press X`n"
    }

    if (player1.Y) {
        msg .= "Player One Press Y`n"
    }

    if (player1.LB) {
        msg .= "Player One Press LB`n"
    }

    if (player1.RB) {
        msg .= "Player One Press RB`n"
    }

    if (value := player1.LT) {
        msg .= "Player One Press LT, Analog Value : " . value . " (0-255)`n"
    }

    if (value := player1.RT) {
        msg .= "Player One Press RT`, Analog Value : " . value . " (0-255)`n"
    }

    if (value := player1.LV) {
        msg .= "Left Motor Speed : " . value . "`n"
    }

    if (value := player1.RV) {
        msg .= "Right Motor Speed : " . value . "`n"
    }

    if (value := player1.BV && (value[0] || value[1])) {
        msg .= " Both Motor Speed, Left: " . value[1] . ", Right: " . value[2] . "`n"
    }

;Left Stick
    if (player1.LS) {
        msg .= "Player One Press LS`n"
    }

;Right Stick
    if (player1.RS) {
        msg .= "Player One Press RS`n"
    }

;Left Stick X(Left/Right) Movement
    if (value := player1.LSX) {
        asDigital := player1.LSX < 0 ? "Left" : "Right"
        msg .= "Player One Move Left Sick In X Axis, Analog Value : " . value . ", Digital: " . asDigital . "`n"
    }

;Left Stick Y(Up/Down) Movement
    if (value := player1.LSY) {
        asDigital := player1.LSY > 0 ? "Up" : "Down"
        msg .= "Player One Move Left Sick In Y Axis, Analog Value : " . value . ", Digital: " . asDigital . "`n"
    }

;Right Stick X(Left/Right) Movement
    if (value := player1.RSX) {
        asDigital := player1.RSX < 0 ? "Left" : "Right"
        msg .= "Player One Move Right Sick In X Axis, Analog Value : " . value . ", Digital: " . asDigital . "`n"
    }

;Right Stick Y(Up/Down) Movement
    if (value := player1.RSY) {
        asDigital := player1.RSY > 0 ? "Up" : "Down"
        msg .= "Player One Move Right Sick In Y Axis, Analog Value : " . value . ", Digital: " . asDigital . "`n"
    }

;Vibration, Move Left And Right sticks
    leftMotorSpeed := Ceil((Abs(player1.LSY) + Abs(player1.LSX)) / 64932 * 65532)
    rightMotorSpeed := Ceil((Abs(player1.RSY) + Abs(player1.RSX)) / 64932 * 65532)
    player1.BV := [leftMotorSpeed, rightMotorSpeed]

    ToolTip, %msg%
    Sleep 5
}
See examples/debug.ahk for a detailed example of GitHub repository.
User avatar
evilC
Posts: 4697
Joined: 27 Feb 2014, 12:30

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

04 Dec 2015, 12:28

Is there any difference in feature set between this and Lexikos' XInput library?

Also, would it maybe be possible add alternate properties that map to the AHK WinMM equivalent?

eg
Joy1 is A, so Buttons[1] would be the equivalent of A.
If JoyX is Left Stick X axis (I dont have a controller to hand, so can't tell if it is), so Axes[X] would be equivalent to LSX.

That way, it would be a little less work to be able to support "normal" sticks and XInput sticks in one app.

What does getKeystrokeAddress do?

I am looking into implementing XInput support in my new system that lets you run AHK scripts as plugins, so I will certainly need a library for that.
edipoReboucas
Posts: 4
Joined: 02 Dec 2015, 10:19
GitHub: edipoReboucas

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

04 Dec 2015, 19:49

Is there any difference in feature set between this and Lexikos' XInput library?

We have the same features, the advantage of my lib is the most simple way to do the things, without call functions and use bitwise operations to find the currently button state.

Also, would it maybe be possible add alternate properties that map to the AHK WinMM equivalent?

Yes, We have a Enum called Xbox360Lib.Enum.Buttons that map A, B, X, Y, LS, LSX etc to one code.

To add one alternate map, only you have to do is this:

Code: Select all

	Xbox360Lib.Enum.Buttons[1] := Xbox360Lib.Enum.Buttons.UP
A complete code:

Code: Select all

;Include the Library
#Include ../XBOX360.ahk

;Instance the Controller Manager
manager := new Xbox360LibControllerManager()

;Initialize Controller (0-3), you can use 4 controllers
player1 := manager.InitializeController(0)

Xbox360Lib.Enum.Buttons[1] := Xbox360Lib.Enum.Buttons.UP

Loop {
    msg := ""

    player1.Update()

    if(player1[1]) {
        msg := "Player One Press UP"
    }

    ToolTip, %msg%

    Sleep, 10
}

What does getKeystrokeAddress do?

Store one reference to DLL XInput function XInputGetKeystroke (https://msdn.microsoft.com/en-us/librar ... s.85).aspx), but I not use this at moment.


I am looking into implementing XInput support in my new system that lets you run AHK scripts as plugins, so I will certainly need a library for that.
User avatar
evilC
Posts: 4697
Joined: 27 Feb 2014, 12:30

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

05 Dec 2015, 10:05

Ooh interesting, you can get events for the thumbsticks as if they were digital inputs, that may come in handy.
By the way, Shaul (maker of vJoy) is currently looking into the possibility of writing an xbox version of vJoy - ie an API that would let you control a virtual xbox controller. If he can get that going, then I could add support for that to my project, providing a much more flexible alternative to x360ce
edipoReboucas
Posts: 4
Joined: 02 Dec 2015, 10:19
GitHub: edipoReboucas

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

09 Dec 2015, 10:58

x360ce is open source, maybe we can incorporate or port the code to your project. I'm intesting in some way to manipulate/red raw data (like Windows Raw Api) to get more options to change/read the state of controller (like leds, chatpad, etc...).

Maybe we can simulate one HID device and produce fake raw and produce fake inputs do xinput dll, It's can be a good feature to my project that can help a lot in your project. I suspect that this lib https://github.com/x360ce/x360ce/tree/master/MinHook make part of this work.
User avatar
evilC
Posts: 4697
Joined: 27 Feb 2014, 12:30

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

09 Dec 2015, 11:38

The problem with the x360ce technique is that it requires putting a DLL in the folder of any game that you want to remap the controller in. Some games detect the presence of this DLL and refuse to run (eg Rocket League).

The approach that Shaul is looking at is to do for XBOX pads what he has done for DirectX sticks - an API that can create virtual devices.

The downside to this approach of course is that games continue to see input from the physical device, but we are also looking into the possibility of writing custom filter drivers etc to make the device either not exist from the game's point of view, or be inactive.

Please feel free to post your thoughts on the relevant thread on the vJoy forum.
C0NTi

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

27 Mar 2016, 06:18

I wonder if anyone can help me/advise me this is possible. I have searched but to no avail

Im only au fait with very basic autohotkey scripting so am not having much luck with making a script to use the AHKXbox360Lib script.

Windows 10 does not appear to have a convienient way of seeing how much charge my xbox controller has,.

At the most basic level I would like to add the battery percentage to the tray icon menu and maybe get it to warn me when it gets low etc. which would periodically update every x mins

Im ok manipulating tray tips etc what I cant get to work is just to get the script to determine if the controller is plugged in and if it is simply return the percentage charge to a variable.

I can see "getBatteryInformation" is in the script but I have absolutely no clue as to how to leverage it.

Any help would be greatly appreciated.
User avatar
evilC
Posts: 4697
Joined: 27 Feb 2014, 12:30

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

29 Mar 2016, 06:38

XInputGetBatteryInformation

When you call getBatteryInformation, the 3rd param (batteryOutAddress) is a windows STRUCT of type _XINPUT_BATTERY_INFORMATION
This is two bytes, with the 2nd being the BatteryLevel. You will need to use NumGet to pull the 2nd byte from the struct.
It will be one of a number of constants: BATTERY_LEVEL_EMPTY, BATTERY_LEVEL_LOW, BATTERY_LEVEL_MEDIUM or BATTERY_LEVEL_FULL.
Somoene will need to fire up visual studio or something to find out the values for these, but it is quite likely they are 0x1, 0x2, 0x3 etc.

Oh, and interesting development regarding AHK and XBox controllers: Soon we will be able to control virtual xbox controllers using AHK: https://autohotkey.com/boards/viewtopic ... 13&t=15408
User avatar
evilC
Posts: 4697
Joined: 27 Feb 2014, 12:30

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

29 Mar 2016, 13:48

Constants used with XInput:

Code: Select all

//
// Device types available in XINPUT_CAPABILITIES
//
#define XINPUT_DEVTYPE_GAMEPAD          0x01

//
// Device subtypes available in XINPUT_CAPABILITIES
//
#define XINPUT_DEVSUBTYPE_GAMEPAD       0x01

#ifndef XINPUT_USE_9_1_0

#define XINPUT_DEVSUBTYPE_WHEEL         0x02
#define XINPUT_DEVSUBTYPE_ARCADE_STICK  0x03
#define XINPUT_DEVSUBTYPE_FLIGHT_SICK   0x04
#define XINPUT_DEVSUBTYPE_DANCE_PAD     0x05
#define XINPUT_DEVSUBTYPE_GUITAR        0x06
#define XINPUT_DEVSUBTYPE_DRUM_KIT      0x08

#endif // !XINPUT_USE_9_1_0



//
// Flags for XINPUT_CAPABILITIES
//
#define XINPUT_CAPS_VOICE_SUPPORTED     0x0004

//
// Constants for gamepad buttons
//
#define XINPUT_GAMEPAD_DPAD_UP          0x0001
#define XINPUT_GAMEPAD_DPAD_DOWN        0x0002
#define XINPUT_GAMEPAD_DPAD_LEFT        0x0004
#define XINPUT_GAMEPAD_DPAD_RIGHT       0x0008
#define XINPUT_GAMEPAD_START            0x0010
#define XINPUT_GAMEPAD_BACK             0x0020
#define XINPUT_GAMEPAD_LEFT_THUMB       0x0040
#define XINPUT_GAMEPAD_RIGHT_THUMB      0x0080
#define XINPUT_GAMEPAD_LEFT_SHOULDER    0x0100
#define XINPUT_GAMEPAD_RIGHT_SHOULDER   0x0200
#define XINPUT_GAMEPAD_A                0x1000
#define XINPUT_GAMEPAD_B                0x2000
#define XINPUT_GAMEPAD_X                0x4000
#define XINPUT_GAMEPAD_Y                0x8000


//
// Gamepad thresholds
//
#define XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE  7849
#define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689
#define XINPUT_GAMEPAD_TRIGGER_THRESHOLD    30

//
// Flags to pass to XInputGetCapabilities
//
#define XINPUT_FLAG_GAMEPAD             0x00000001


#ifndef XINPUT_USE_9_1_0

//
// Devices that support batteries
//
#define BATTERY_DEVTYPE_GAMEPAD         0x00
#define BATTERY_DEVTYPE_HEADSET         0x01

//
// Flags for battery status level
//
#define BATTERY_TYPE_DISCONNECTED       0x00    // This device is not connected
#define BATTERY_TYPE_WIRED              0x01    // Wired device, no battery
#define BATTERY_TYPE_ALKALINE           0x02    // Alkaline battery source
#define BATTERY_TYPE_NIMH               0x03    // Nickel Metal Hydride battery source
#define BATTERY_TYPE_UNKNOWN            0xFF    // Cannot determine the battery type

// These are only valid for wireless, connected devices, with known battery types
// The amount of use time remaining depends on the type of device.
#define BATTERY_LEVEL_EMPTY             0x00
#define BATTERY_LEVEL_LOW               0x01
#define BATTERY_LEVEL_MEDIUM            0x02
#define BATTERY_LEVEL_FULL              0x03

// User index definitions
#define XUSER_MAX_COUNT                 4

#define XUSER_INDEX_ANY                 0x000000FF


//
// Codes returned for the gamepad keystroke
//

#define VK_PAD_A                        0x5800
#define VK_PAD_B                        0x5801
#define VK_PAD_X                        0x5802
#define VK_PAD_Y                        0x5803
#define VK_PAD_RSHOULDER                0x5804
#define VK_PAD_LSHOULDER                0x5805
#define VK_PAD_LTRIGGER                 0x5806
#define VK_PAD_RTRIGGER                 0x5807

#define VK_PAD_DPAD_UP                  0x5810
#define VK_PAD_DPAD_DOWN                0x5811
#define VK_PAD_DPAD_LEFT                0x5812
#define VK_PAD_DPAD_RIGHT               0x5813
#define VK_PAD_START                    0x5814
#define VK_PAD_BACK                     0x5815
#define VK_PAD_LTHUMB_PRESS             0x5816
#define VK_PAD_RTHUMB_PRESS             0x5817

#define VK_PAD_LTHUMB_UP                0x5820
#define VK_PAD_LTHUMB_DOWN              0x5821
#define VK_PAD_LTHUMB_RIGHT             0x5822
#define VK_PAD_LTHUMB_LEFT              0x5823
#define VK_PAD_LTHUMB_UPLEFT            0x5824
#define VK_PAD_LTHUMB_UPRIGHT           0x5825
#define VK_PAD_LTHUMB_DOWNRIGHT         0x5826
#define VK_PAD_LTHUMB_DOWNLEFT          0x5827

#define VK_PAD_RTHUMB_UP                0x5830
#define VK_PAD_RTHUMB_DOWN              0x5831
#define VK_PAD_RTHUMB_RIGHT             0x5832
#define VK_PAD_RTHUMB_LEFT              0x5833
#define VK_PAD_RTHUMB_UPLEFT            0x5834
#define VK_PAD_RTHUMB_UPRIGHT           0x5835
#define VK_PAD_RTHUMB_DOWNRIGHT         0x5836
#define VK_PAD_RTHUMB_DOWNLEFT          0x5837

//
// Flags used in XINPUT_KEYSTROKE
//
#define XINPUT_KEYSTROKE_KEYDOWN        0x0001
#define XINPUT_KEYSTROKE_KEYUP          0x0002
#define XINPUT_KEYSTROKE_REPEAT         0x0004

User avatar
evilC
Posts: 4697
Joined: 27 Feb 2014, 12:30

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

29 Mar 2016, 14:49

I implemented getting battery levels for the gamepad only (You can also get it for the headset) by adding two new functions:

GetGamepadBatteryLevelValue
Reports a numerical value for the gamepad's battery level.
-1 : "Unknown" (Could not get power level. Gamepad is off maybe)
0: "Empty" (Got battery level, but reporting empty)
1: "Low"
2: "Medium"
3: "Full"

GetGamepadBatteryLevelName
Returns the name (eg "Low", "Medium") of the battery level

Having -1 for "Unknown" and 0 for empty may at first seem pointless, but I am thinking not. From the constants:

Code: Select all

// These are only valid for wireless, connected devices, with known battery types
// The amount of use time remaining depends on the type of device.
#define BATTERY_LEVEL_EMPTY             0x00
#define BATTERY_LEVEL_LOW               0x01
#define BATTERY_LEVEL_MEDIUM            0x02
#define BATTERY_LEVEL_FULL              0x03
"only valid for wireless, connected devices". So I am guessing "Empty" does not mean fully empty, just "about to run out". I was unable to test this though, so I will leave it as-is for the moment.

I submitted a pull request to edipoReboucas' repo, so he may or may not merge, but until then you can use the files from my fork, which is here: https://github.com/evilC/AHKXbox360Lib/
edipoReboucas
Posts: 4
Joined: 02 Dec 2015, 10:19
GitHub: edipoReboucas

Re: AHKXbox360Lib: OOP Library of Xbox360 Controller for AHK

06 Apr 2016, 12:56

Merged! thanks!

Return to “Scripts and Functions”

Who is online

Users browsing this forum: robodesign and 33 guests