|
-
Jan 31st, 2017, 02:04 AM
#1
Thread Starter
Addicted Member
Easy XBox Controller Support (No XNA)
Hi! Here's a quick guide for stubborn people like me who don't use XNA but still want to incorporate an XBox controller into their application or game. I was surprised at how easy it was.
Step 1. Download a package off of NuGet that contains XInput. The package I found was called SharpDX.
Step 2. Import it into the file that will be recieving data. Imports SharpDX.XInput
Step 3. Use this handy function:
vb Code:
Private Function CheckController(PlayerIndex As UserIndex) As String
If Not New Controller(PlayerIndex).IsConnected Then Return "Disconnected" ' GamePad is not connected.
Dim GPS As Gamepad = New Controller(PlayerIndex).GetState.Gamepad ' GamePad State
' Registers a whole bunch of data about the current state of the controller.
Dim Str As String = $"Left Thumb: {GPS.LeftThumbX}, {GPS.LeftThumbY}{vbNewLine}" &
$"Right Thumb: {GPS.RightThumbX}, {GPS.RightThumbY}{vbNewLine}" &
$"LTrigger: {GPS.LeftTrigger} RTrigger: {GPS.RightTrigger}{vbNewLine}" &
$"Buttons Currently Pressed: {GPS.Buttons.ToString}"
' Checks if the A button is pressed.
If GPS.Buttons.HasFlag(GPS.Buttons.A) Then
' Do something when A is pressed.
Else
' Do something when A is not pressed.
End If
' Copy that if-block as many times as needed to cover all the buttons used in the game.
Return Str
End Function
And it's done! Now, you'd probably want to put CheckController(0) in some kind of timed loop so it constantly updates, and copy/paste that if-statement to check for more buttons that are pressed, but hey, I'm not going to tell you how to live your life.
I hope this helped! Thanks,
~Nic
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|