GetJoystick() with Xbox controllers

General FreeBASIC programming questions.
Post Reply
Auios
Posts: 11
Joined: Jul 18, 2011 16:50
Location: USA
Contact:

GetJoystick() with Xbox controllers

Post by Auios »

I received some Xbox controllers in the mail today. I was interested in making an application that moves the mouse pointer on the screen or whatever I want. I thought FreeBasic would be a fun language to use. The wording in the documentation wasn't exactly clear about how to achieve this - so after some messing around I wrote a small program to demonstrate how to use getJoystick() with Xbox controllers.

This example was built from the getJoystick() example in the manual. I included comments to explain things. As well as a map to each button on the controller.

Enjoy!

Code: Select all

' Example code for using getJoystick() with Xbox controllers by Auios

type XboxController
    as integer buttons
    as single lax, lay
    as single rax, ray
    as single dpadx, dpady
    as single triggers
end type

function getXboxController(ID as long, xbct as xboxController) as integer
    return getJoystick(ID, xbct.buttons, xbct.lax, xbct.lay, xbct.triggers, xbct.ray, xbct.rax, , xbct.dpadx, xbct.dpady)
end function

const joystickID = 0

' Check to see if the joystick is okay.
if getJoystick(joystickID) Then
    Print "Joystick doesn't exist or joystick error."
    Print
    Print "Press any key to continue."
    Sleep
    End
end if

screenres 800, 600

dim as XboxController myController
dim as integer result

do
    screenLock()
    cls()
    result = getXboxController(JoystickID, myController)
    
    ' If you want to turn your analog input into a straight dpad input you can try this:
    ' x = cast(integer, x)
    ' y = cast(integer, y)
    
    print("Result       -> " & result)
    print("Buttons      -> " & myController.buttons)
    print("Triggers     -> " & myController.triggers)
    print("Left analog  -> (" & myController.lax & ", " & myController.lay & ")")
    print("Right analog -> (" & myController.rax & ", " & myController.ray & ")")
    print("DPad         -> (" & myController.dpadx & ", " & myController.dpady & ")")
    print("")
    
    for i as integer = 0 to 31
        If (myController.buttons And (1 Shl i)) Then
            Print "Button ";i;" pressed.    "
        Else
            Print "Button ";i;" not pressed."
        End If
    next i
    screenUnlock()
    
    sleep(1,1)
loop until(inkey = chr(27))

'   0   A
'   1   B
'   2   X
'   3   Y
'   4   Left
'   5   Right
'   6   Back
'   7   Start
'   8   Left Analog Button
'   9   Right Analog Button
Post Reply