Sign in to follow this  
Followers 0
mud409

Recording inputs from HID interface for button assignments

2 posts in this topic

#1 ·  Posted (edited)

I'm trying to modify Ejoc's HID interface program to record the inputs of a gamepad, so it will record the first 6 inputs hit, and write those to cfg files (the cfg files are to assign keys to other programs). I've got it working for the most part (I think). The problem is, when I hit a button from the gamepad it records that button to all 6 fields, because the HID interface program repeatably reads the button very quickly. Is there a way I can slow this down, or how would I modify my write or read method to wait till the button is released?

;____________________________________________________________________           
;       Original HID interface program by Ejoc                                  ;
;       Improved by Adam1213 (autoit 3.2 compatiblity + improved labels         ;
;____________________________________________________________________

#include <GUIConstants.au3>
;_________________ SETUP_____________________________________
Local $joy,$coor,$h,$s,$msg,$whatbutton

$joy    = _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]
;__________ CONFIG ____________________________________________
;---------- 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

GUICreate('Joystick Config', 200, 200)

    GUICtrlCreateLabel("Press Buttons 1 through 6", 40, 40, 200, 15)
;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($joy,0)
    $whatbut = "Button"
$butnum = 1
$p = 0
Do
    
    If $coord[7] = "1" Then _WriteIni($whatbut, $p, "1")
    If $coord[7] = "2" Then _WriteIni($whatbut, $p, "2")
    If  $coord[7] = "4" Then _WriteIni($whatbut, $p, "3")
    If  $coord[7] = "8" Then _WriteIni($whatbut, $p, "4")
    If  $coord[7] = "16" Then _WriteIni($whatbut, $p, "5")
    If  $coord[7] = "32" Then _WriteIni($whatbut, $p, "6")
    If  $coord[7] = "64" Then _WriteIni($whatbut, $p, "7")
    If  $coord[7] = "128" Then _WriteIni($whatbut, $p, "8")
    If  $coord[7] = "256" Then _WriteIni($whatbut, $p, "9")
    If  $coord[7] = "512" Then _WriteIni($whatbut, $p, "10")
    If  $coord[7] = "1024" Then _WriteIni($whatbut, $p, "11")
    If  $coord[7] = "2048" Then _WriteIni($whatbut, $p, "12") 
        $p = $p + 1
        Until $p = 7
   for $i=0 to UBound($coord)-1
        if $coord[$i]<>$labels_value[$i] then
            GUICtrlSetData($labels[$i], $coord[$i])
            $labels_value[$i]=$coord[$i]
        endif
    next
    
    sleep(10)
    $msg =GUIGetMSG()
    if $msg = $GUI_EVENT_CLOSE Then Exitloop
        
WEnd

$lpJoy=0 ; Joyclose
;EndFunc

;======================================
;   _WriteIni()
;======================================
Func _WriteIni($whatbut, $p, $writenum)
    Iniwrite ("buttons.ini", "buttons", $whatbut & $p, $writenum)
    EndFunc
;======================================
;   _JoyInit()
;======================================
Func _JoyInit()
    Local $joy
    Global $JOYINFOEX_struct    = "dword[13]"

    $joy=DllStructCreate($JOYINFOEX_struct)
    if @error Then Return 0
    DllStructSetData($joy, 1, DllStructGetSize($joy), 1);dwSize = sizeof(struct)
    DllStructSetData($joy, 1, 255, 2)              ;dwFlags = GetAll
    return $joy
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)
        $coor[1]    = DllStructGetData($lpJoy,1,4)
        $coor[2]    = DllStructGetData($lpJoy,1,5)
        $coor[3]    = DllStructGetData($lpJoy,1,6)
        $coor[4]    = DllStructGetData($lpJoy,1,7)
        $coor[5]    = DllStructGetData($lpJoy,1,8)
        $coor[6]    = DllStructGetData($lpJoy,1,11)
        $coor[7]    = DllStructGetData($lpJoy,1,9)
    EndIf

    return $coor
EndFunc
Edited by mud409

Share this post


Link to post
Share on other sites



Thanks! Works perfect.

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