Sign in to follow this  
Followers 0

UDF: _IsPressed360.au3 (Xbox360 controller)

3 posts in this topic

Posted (edited)

Hey all i found >this old script by Oxin8 for the X-box controller, but i just needed something more simple, so i decided to make a UDF simmilair to the _IsPressed function by ezzetabi and Jon.
Hope its helpfull to someone :-)

Functions:

_IsPressed360


Example:

#include <_IsPressed360.au3>
$dll = DllOpen("xinput9_1_0.dll")
 
While 1
    Sleep(250)
    If _IsPressed360(16, $dll) Then
        MsgBox(0, "_IsPressed360", "Start Button Pressed")
        ExitLoop
    EndIf
WEnd
DllClose($dll)


UDF:

#include-once
#Region _IsPressed360
; #FUNCTION# ====================================================================================================================
; Name...........: _IsPressed360
; Description ...: Checks if key on Xbox360 controller has been pressed
; Syntax.........: _IsPressed360($iKey[, $vDLL = 'user32.dll'])
; Parameters ....: $iKey        - Key to check for
;                 $vDLL     - Handle to dll or default to user32.dll
; Return values .: True      - 1
;                 False     - 0
; Author ........: Carlo Westmaas (with the help of scripts made by Oxin8, ezzetabi and Jon)
; Modified.......:
; Remarks .......: If calling this function repeatidly, you should open 'xinput9_1_0.dll' and pass in handle.
;                 Make sure to close at end of script
;                 If requesting state of one of the Joysticks or triggers its press value will be returned as @extended
;                 -32768  Y
;                 1    Up
;                 2    Down
;                 4    Left
;                 8    Right
;                 16      Start
;                 32      Back
;                 64      LeftJoyStick
;                 128    RightJoyStick
;                 256    LB
;                 512    RB
;                 4096  A
;                 8192  B
;                 16384   X
;                 32768   LeftTrigger  (@extended 0 - 255)
;                 65536   RightTrigger (@extended 0 - 255)
;                 131072  LeftJoyStick-X (@extended -32768 - 32767)
;                 262144  LeftJoyStick-Y (@extended -32768 - 32767)
;                 524288  RightJoyStick-X (@extended -32768 - 32767)
;                 1048576 RightJoyStick-Y (@extended -32768 - 32767)
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/133663-udf-ispressed360au3-xbox360-controller/
; Example .......: Yes
; ===============================================================================================================================
Func _IsPressed360($iKey, $vDLL = 'xinput9_1_0.dll')
    Local $hStruct, $iValue = 0
    $hStruct = DllStructCreate("dword;short;ubyte;ubyte;short;short;short;short")
    If DllCall($vDLL, "long", "XInputGetState", "long", 0, "ptr", DllStructGetPtr($hStruct)) = 0 Then Return SetError(5, 0, False)
    If @error Then Return SetError(@error, @extended, False)
    Select
        Case $iKey < 16385
            Return Number(BitAND($iKey, DllStructGetData($hStruct, 2)) <> 0)
        Case $iKey = 32768
            $iValue = DllStructGetData($hStruct, 3)
            If $iValue > 10 Then Return SetError(0, $iValue, 1)
        Case $iKey = 65536
            $iValue = DllStructGetData($hStruct, 4)
            If $iValue > 10 Then Return SetError(0, $iValue, 1)
        Case $iKey = 131072
            $iValue = DllStructGetData($hStruct, 5)
            If $iValue > 10000 Or $iValue < -10000 Then Return SetError(0, $iValue, 1)
        Case $iKey = 262144
            $iValue = DllStructGetData($hStruct, 6)
            If $iValue > 10000 Or $iValue < -10000 Then Return SetError(0, $iValue, 1)
        Case $iKey = 524288
            $iValue = DllStructGetData($hStruct, 7)
            If $iValue > 10000 Or $iValue < -10000 Then Return SetError(0, $iValue, 1)
        Case $iKey = 1048576
            $iValue = DllStructGetData($hStruct, 8)
            If $iValue > 10000 Or $iValue < -10000 Then Return SetError(0, $iValue, 1)
    EndSelect
    Return SetError(0, $iValue, 0)
EndFunc   ;==>_IsPressed360
#EndRegion _IsPressed360

Edited by Carlo84

Share this post


Link to post
Share on other sites



Posted (edited)

Heres a better example.

Script uses Controller as mouse.

Press Escape to exit script.

Check here for button layout

Posted Image

#include "_IsPressed360.au3"
HotKeySet('{ESC}', '_Exit')
Global Const $hDll = DllOpen("xinput9_1_0.dll")
Global $iAdown = 0, $iBdown = 0, $iYDown = 0
Global $iLeftJoyDown = 0
Global $iLeftDown = 0, $iRightDown = 0, $iUpDown = 0, $iDownDown = 0
Global $iRightJoyRightDown = 0, $iRightJoyLeftDown = 0, $iRightJoyDown = 0, $iRightJoyUpDown = 0, $iRightJoyDownDown = 0

While 1
    If _IsPressed360(131072, $hDll) Or _IsPressed360(262144) Then _MouseMove() ; Left Joystick moved
    _MouseUpDown(64, 'middle', $iLeftJoyDown, $hDll) ; Left Joystick pressed

    _MouseUpDown(4096, 'primary', $iAdown, $hDll) ;A button
    _MouseUpDown(8192, 'secondary', $iBdown, $hDll) ;B button
    _KeyUpDown(-32768, '{LAUNCH_MEDIA down}', $iYDown, $hDll) ;Y button

    If _IsPressed360(16, $hDll) Then ; Start button pressed
        Send('#d')
        Sleep(1000)
    EndIf

    If _IsPressed360(32768, $hDll) Then MouseWheel('down') ; Left trigger pressed
    If _IsPressed360(65536, $hDll) Then MouseWheel('up') ; Right trigger pressed
    If _IsPressed360(256, $hDll) Then Send('{VOLUME_DOWN}') ; LB pressed
    If _IsPressed360(512, $hDll) Then Send('{VOLUME_UP}') ; RB pressed

    _KeyUpDown(1, '{MEDIA_PLAY_PAUSE down}', $iUpDown, $hDll) ;D-Pad Up button
    _KeyUpDown(2, '{MEDIA_STOP down}', $iDownDown, $hDll) ;D-Pad Down button
    _KeyUpDown(4, '{MEDIA_PREV down}', $iLeftDown, $hDll) ;D-Pad Left button
    _KeyUpDown(8, '{MEDIA_NEXT down}', $iRightDown, $hDll) ;D-Pad Right button

    If _IsPressed360(524288, $hDll) And @extended > 0 Then
        _KeyUpDown(524288, '{BROWSER_FORWARD down}', $iRightJoyRightDown, $hDll) ;Right Joystick moved to right
    ElseIf _IsPressed360(524288, $hDll) And @extended < 0 Then
        _KeyUpDown(524288, '{BROWSER_BACK down}', $iRightJoyLeftDown, $hDll) ;Right Joystick moved to left
    Else
        _KeyUpDown(524288, '{BROWSER_BACK down}', $iRightJoyLeftDown, $hDll) ;Right Joystick moved to left
        _KeyUpDown(524288, '{BROWSER_FORWARD down}', $iRightJoyRightDown, $hDll) ;Right Joystick moved to right
    EndIf
    _KeyUpDown(128, '{BROWSER_HOME down}', $iRightJoyDown, $hDll) ;Right Joystick pressed
    If _IsPressed360(1048576, $hDll) And @extended > 0 Then
        _KeyUpDown(1048576, '{BROWSER_FAVORITES down}', $iRightJoyUpDown, $hDll) ;Right Joystick moved up
    ElseIf _IsPressed360(1048576, $hDll) And @extended < 0 Then
        _KeyUpDown(1048576, '{BROWSER_REFRESH down}', $iRightJoyDownDown, $hDll) ;Right Joystick moved down
    Else
        _KeyUpDown(1048576, '{BROWSER_FAVORITES down}', $iRightJoyUpDown, $hDll) ;Right Joystick moved up
        _KeyUpDown(1048576, '{BROWSER_REFRESH down}', $iRightJoyDownDown, $hDll) ;Right Joystick moved down
    EndIf
    If _IsPressed360(32, $hDll) Then _Hibernate(32, 5000, $hDll) ; Back button pressed
    Sleep(25)
WEnd

Func _Hibernate($iKey360, $iTime = 5000, $vDll = "xinput9_1_0.dll")
    Local $iTimer = TimerInit()
    While 1
        If Not _IsPressed360($iKey360, $vDll) Then Return 0
        If TimerDiff($iTimer) > $iTime Then ExitLoop
    WEnd
    If _IsPressed360($iKey360, $vDll) Then
        Shutdown(64)
        Return 0
    EndIf
EndFunc   ;==>_Hibernate

Func _MouseUpDown($iKey360, $sPressKey, ByRef $iVar, $vDll = "xinput9_1_0.dll")
    If _IsPressed360($iKey360, $vDll) And $iVar = 0 Then
        MouseDown($sPressKey)
        $iVar = 1
    ElseIf Not _IsPressed360($iKey360, $vDll) And $iVar = 1 Then
        MouseUp($sPressKey)
        $iVar = 0
    EndIf
EndFunc   ;==>_MouseUpDown

Func _KeyUpDown($iKey360, $sPressKey, ByRef $iVar, $vDll = "xinput9_1_0.dll")
    If _IsPressed360($iKey360, $vDll) And $iVar = 0 Then
        Send($sPressKey)
        $iVar = 1
    ElseIf Not _IsPressed360($iKey360, $vDll) And $iVar = 1 Then
        Send(StringRegExpReplace($sPressKey, '(\ down\})', ' up}'))
        $iVar = 0
    EndIf
EndFunc   ;==>_KeyUpDown

Func _MouseMove()
    Local $iX = MouseGetPos(0)
    Local $iY = MouseGetPos(1)
    If _IsPressed360(131072, $hDll) Then $iX += (@extended / 1000)
    If _IsPressed360(262144, $hDll) Then $iY -= (@extended / 1000)
    MouseMove($iX, $iY)
EndFunc   ;==>_MouseMove
Func _Exit()
    DllClose($hDll)
    Exit
EndFunc   ;==>_Exit
Edited by Djarlo

Share this post


Link to post
Share on other sites

Posted (edited)

Updated second sample for completion, think it shows plenty of diffrent examples of possibilities now, and its actually useful :-)

I stop working on this now ;-)

Edited by Djarlo
1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.