Jump to content
Ejoc

Joystick UDF

Recommended Posts

A joystick UDF, requires beta >= 3.1.1.18

#include <GUIConstants.au3>

Local $joy,$coor,$h,$s,$msg

$joy    = _JoyInit()

GUICreate("Joystick Test",300,300)
$h= GuiCtrlCreatelabel("",10,10,290,290)
GUISetState()

while 1
    $msg    = GUIGetMSG()
    $coor   = _GetJoy($joy,0)
    $s      = "Joystick(0):" & @CRLF & _
                "X: " & $coor[0] & @CRLF & _
                "Y: " & $coor[1] & @CRLF & _
                "Z: " & $coor[2] & @CRLF & _
                "R: " & $coor[3] & @CRLF & _
                "U: " & $coor[4] & @CRLF & _
                "V: " & $coor[5] & @CRLF & _
                "POV: " & $coor[6] & @CRLF & _
                "Buttons: " & $coor[7]
    GUICtrlSetData($h,$s,1)
    sleep(10)
    if $msg = $GUI_EVENT_CLOSE Then Exitloop
WEnd

_JoyClose($joy)

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

    $joy    = DllStructCreate($JOYINFOEX_struct)
    if @error Then Return 0
    DllStructSet($joy,1,DllStructSize($joy),1);dwSize = sizeof(struct)
    DllStructSet($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",DllStructPtr($lpJoy))

    if Not @error Then
        $coor[0]    = DllStructGet($lpJoy,1,3)
        $coor[1]    = DllStructGet($lpJoy,1,4)
        $coor[2]    = DllStructGet($lpJoy,1,5)
        $coor[3]    = DllStructGet($lpJoy,1,6)
        $coor[4]    = DllStructGet($lpJoy,1,7)
        $coor[5]    = DllStructGet($lpJoy,1,8)
        $coor[6]    = DllStructGet($lpJoy,1,11)
        $coor[7]    = DllStructGet($lpJoy,1,9)
    EndIf

    return $coor
EndFunc

;======================================
;   _JoyClose($lpJoy)
;   Free the memory allocated for the joystick struct
;======================================
Func _JoyClose($lpJoy)
    DllStructFree($lpJoy)
EndFunc

_Joystick.au3

Edited by Ejoc

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs

Share this post


Link to post
Share on other sites

A joystick UDF, requires beta >= 3.1.1.18

(code was here)

Nice program but it did not work with Autoit 3.2, I fixed this. When I made it work its blinking (info on GUI) was really bad (fixed)

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

#include <GUIConstants.au3>

;_________________ SETUP_____________________________________
Local $joy,$coor,$h,$s,$msg

$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
;_____________ 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($joy,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]
        endif
    next
    sleep(10)
    $msg =GUIGetMSG()
    if $msg = $GUI_EVENT_CLOSE Then Exitloop
WEnd

$lpJoy=0 ; Joyclose

;======================================
;   _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

Share this post


Link to post
Share on other sites

What is this script supposed to do?? I just get a empty MsgBox....

EDIT: wait i just got it, i had to go to controlpanel>game devices and then choose my joystick to be the one used for older apps.....

Edited by TzarAlkex

Share this post


Link to post
Share on other sites

thaaaaaaaanx very much, something like that was what i was looking for starting an Joy2Midi script for use in music secuencer programs. :)

Share this post


Link to post
Share on other sites

Thank you both Ejoc and Adam, this is exactly what I need. I will be using it detect the movements of my n52te gamepad, and send additional keystrokes along with my movements in order to overcome some of the limitations in razor's programming software.

Edited by eden

Share this post


Link to post
Share on other sites

I can get this to read my joystick, and return button presses, but what would I need to script in order to translate joystick button presses into key presses?

I am using a USB Rock band drumkit to control a game I've created in Flash, in an autoit container.. pretty awesome if this will work.

EDIT: I figured it out -- it was a if $coord[7] = "1" Then Send("{RIGHT}") line. Perfect!!

Can use this for the guitar hero guitar as well.

Now, if there were any way of determining the name of each joystick plugged into the computer, that would be swell.

Edited by Justin

Share this post


Link to post
Share on other sites

Reading about Joystick and AI interactions...

Now i'm inoffice so i can't try, but udf is capable to detect also analog stick ? (and their diagonals?)

Thank you for any info,

m.

Share this post


Link to post
Share on other sites

Thank You so much Ejoc and Adam1213 for your posts!

They were a great help pointing me the direction on the creation of an utility for the Logitech G25 H-Shifter.

Now I can use the H-Shifter in old games!

In case anyone be interested: My link

o.

Share this post


Link to post
Share on other sites

Português

Olá pessoal,

preciso saber como enviar o pressionamento de um botão no joystick, pois o jogo "street fighter 4" o player 2 não tem comandos no teclado.

Alguém sabe me ajudar?

Translate by google

Inglês

Hello everybody,

need to know how to send the push of a button on the joystick, because the game "street fighter 4" player 2 has no controls on the keyboard.

Someone help me know?

Share this post


Link to post
Share on other sites

Hi there,

I am trying to create a program that works with my xbox 360 controller. Judging by the replies I am seeing on this thread with people with similar aspirations being helped by this UDF, I am assuming that this UDF is instrumental in the process. Unfortunately, though, I do not understand how this is an aid to me. The UDF is responding to activity on my controller, but I don't know how to apply these numerical values to my own work. If someone could provide some instruction as to how this UDF can help me to write a program that'll work with my controller, it would be greatly appreciated.

Basically, I want to create a program that can:

- Make arrow keys correspond to the left analog stick

- Trigger Mouseclicks at certain xy coordinates by pressing buttons on the controller

Thank you,

-Chris

Share this post


Link to post
Share on other sites

Thank You so much Ejoc and Adam1213 for your posts!

They were a great help pointing me the direction on the creation of an utility for the Logitech G25 H-Shifter.

Now I can use the H-Shifter in old games!

In case anyone be interested: My link

*** Update: Released a new version in 2010-08-10

o.

Share this post


Link to post
Share on other sites

Hi,

I've release a freeware tool made with autoit v3 that allows a basic usage of the Nostromo N50 in Windows7,

without the need of driver installation. You can have a look here.

Thanks.

o.

Share this post


Link to post
Share on other sites

Sorry to bring up and old topic, but I'm trying to get this to work and I'm having some troubles. The script works great, but the numbers seem a little off. The number range I was getting was from 0-65535 (2^16) rather than 0-255 (2^8), so I just took the square root, but when the joystick is at rest, it reads ~180 rather than 128. This is true for all three axes. I have reset the joystick calibration in Windows, but I don't think that's the issue, because other programs read the joystick fine (as 128 when it's at rest). Does anyone know anything about this? Your help would be greatly appreciated.

Thanks!

Share this post


Link to post
Share on other sites

@magician13134

Run the code from post #4. If it works, then whatever code you added is wrong. So either skip that weird conversion you are doing or post a reproducer (example) and I'm sure someone can help you.

Share this post


Link to post
Share on other sites

@magician13134

Run the code from post #4. If it works, then whatever code you added is wrong. So either skip that weird conversion you are doing or post a reproducer (example) and I'm sure someone can help you.

I did run the code exactly as posted in post #4, and that's what gave me weird results. I was just comparing those results to what another program was giving me simultaneously. I don't know if anyone else had this problem, but I compared a bunch of points that I was reading from the script in post #4 to what they should be and linearized it, so this is what I had to do to get it working... It may just be something wrong with my computer, but here's the equation I had to use
$realPoint=10^(2.002*Log($coord[$i])/Log(10) - 2.413)
That yields the proper coordinates for me...

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

×
×
  • Create New...