Jump to content

Looping by pressing the joystick button


mutleey
 Share

Recommended Posts

Hello guys, I have a problem here and needed your help.

I need this script to be looping when pressed a button joystick, and then loose the button to perform a function

in the case when pressed any button on the joystick enter the loop and when you release the button to appear the message.

thank you.

#Include <GUIConstants.au3>

Local $Joystick, $coor, $h, $s, $msg
Local $num = 0

$Joystick = _JoyInit()

Dim $labels_text[8] = ['X', 'Y', 'Z', 'R', 'U', 'V', 'POV', 'Buttons']
Dim $labels_no = UBound($labels_text)
Dim $labels[$labels_no]
Dim $labels_value[$labels_no]

; Find the max length of the longest label
$label_len = 0
For $text In $labels_text
    $len = StringLen($text)
    If $len > $label_len Then
        $label_len = $len
    EndIf
Next
$label_len *= 6

; GUI
GUICreate('Joystick Test', 200, 200)
GUICtrlCreateLabel('Joystick', 40, 20, 100, 20)

For $i = 0 To $labels_no - 1
    GUICtrlCreateLabel($labels_text[$i]&':', 10, 60 + $i * 12, $label_len, 12)
    $labels[$i] = GUICtrlCreateLabel('', 10 + $label_len, 60 + $i * 12, 70, 12)
    $labels_value[$i] = ''
Next
GUISetState()

While 1
    $coord = _GetJoy($Joystick, 0)
    For $i = 0 To UBound($coord) - 1
        If $coord[$i] <> $labels_value[$i] Then
            GUICtrlSetData($labels[$i], $coord[$i])
            $labels_value[$i] = $coord[$i]
;-----------------------------------------------------
            While $coord[7]
                Sleep(10)
            WEnd
            MsgBox(0, "Joy", "loose button", 1)
;----------------------------------------------------
        EndIf
   Next
    Sleep(10)
    $msg =GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

$lpJoy=0 ; Joyclose

;   _JoyInit()
Func _JoyInit()
    Local $Joystick
    Global $JOYINFOEX_struct    = "dword[13]"

    $Joystick=DllStructCreate($JOYINFOEX_struct)
    If @Error Then Return 0
    DllStructSetData($Joystick, 1, DllStructGetSize($Joystick), 1);dwSize = sizeof(struct)
    DllStructSetData($Joystick, 1, 255, 2)            ;dwFlags = GetAll
    Return $Joystick
EndFunc

;======================================
;   _GetJoy($lpJoy,$iJoy)
;   $lpJoy Return from _JoyInit()
;   $iJoy  Joystick # 0-15
;   Return Array containing X-Pos, Y-Pos, Z-Pos, R-Pos, U-Pos, V-Pos,POV
;         Buttons down
;
;         *POV This is a digital game pad, not analog joystick
;         65535  = Not pressed
;         0    = U
;         4500   = UR
;         9000   = R
;         Goes around clockwise increasing 4500 for each position
;======================================
Func _GetJoy($lpJoy, $iJoy)
    Local $coor,$ret

    Dim $coor[8]
    DllCall("Winmm.dll","int","joyGetPosEx", "int",$iJoy, "ptr",DllStructGetPtr($lpJoy))

    If NOT @Error Then
        $coor[0] = DllStructGetData($lpJoy,1,3) ; X Axis
        $coor[1] = DllStructGetData($lpJoy,1,4) ; Y Axis
        $coor[2] = DllStructGetData($lpJoy,1,5) ; Z Axis
        $coor[3] = DllStructGetData($lpJoy,1,6) ; R Axis
        $coor[4] = DllStructGetData($lpJoy,1,7) ; U Axis
        $coor[5] = DllStructGetData($lpJoy,1,8) ; V Axis
        $coor[6] = DllStructGetData($lpJoy,1,11); POV Value
        $coor[7] = DllStructGetData($lpJoy,1,9) ; Buttons Mask
    EndIf

    Return $coor
EndFunc

Edit:

contrary to what you might think is not automation games, but a mechanical arm controlled by a joystick (analog) only by pressing the function assigned to it is called several times, even giving a touch very quickly on the button.

I do not know if it violates the rules of the forum, anyway thanks.

Edited by mutleey
Link to comment
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
 Share

×
×
  • Create New...