Code archives/User Input/Xbox 360 Controller Input

This code has been declared by its author to be Public Domain code.

Download source code

Xbox 360 Controller Input by xtremegamr
(Posted 19 years ago)
It works pretty well, but there are a few drawbacks:

-If you hold down both triggers, the functions ReturnTrigger() and TriggerDown() with both return neutral.

-The Right Stick functions don't really work that well. I'm open to suggestions on how to make it work.
;Xbox 360 Controller Library
;Programmed by xtremegamr

;All Functions have X360_ before them

;LeftStickX#()
;returns the x position of the Left Stick
Function X360_LeftStickX#(port)

If port=-1 Then
	returnvalue#=JoyX()
Else
	returnvalue#=JoyX(port)
End If

Return returnvalue#

End Function

;LeftStickY#()
;returns the y position of the Left Stick
Function X360_LeftStickY#(port)

If port=-1 Then
	returnvalue#=JoyY()
Else
	returnvalue#=JoyY(port)
End If

Return returnvalue#

End Function

;LeftStickXDir()
;returns the x direction of the Left Stick
Function X360_LeftStickXDir(port)

If port=-1 Then
	returnvalue=JoyXDir()
Else
	returnvalue=JoyXDir(port)
End If

Return returnvalue

End Function

;LeftStickYDir()
;return the y direction of the Left Stick
Function X360_LeftStickYDir(port)

If port=-1 Then
	returnvalue=JoyYDir()
Else
	returnvalue=JoyYDir(port)
End If

Return returnvalue

End Function

;RightStickX#()
;returns the x position of the Right Stick
Function X360_RightStickX#(port)

If port=-1 Then
	returnvalue=JoyPitch()
Else
	returnvalue=JoyPitch(port)
End If

Return returnvalue

End Function

;RightStickY#()
;returns the y position of the Right Stick
Function X360_RightStickY#(port)

If port=-1 Then
	returnvalue=JoyYaw()
Else
	returnvalue=JoyYaw(port)
End If

Return returnvalue

End Function

;RightStickXDir#()
;returns the x direction of the Right Stick
Function X360_RightStickXDir(neutral,port)

If port=-1 Then
	If JoyPitch()<neutral Then returnvalue=-1
	If JoyPitch()>neutral Then returnvalue=1
Else
	If JoyPitch(port)<neutral Then returnvalue=-1
	If JoyPitch(port)>neutral Then returnvalue=1
End If

Return returnvalue

End Function

;RightStickYDir#()
;returns the y direction of the Right Stick
Function X360_RightStickYDir(neutral,port)

If port=-1 Then
	If JoyYaw()<neutral Then returnvalue=-1
	If JoyYaw()>neutral Then returnvalue=1
Else
	If JoyYaw(port)<neutral Then returnvalue=-1
	If JoyYaw(port)>neutral Then returnvalue=1
End If

Return returnvalue

End Function

;ReturnTrigger#()
;returns trigger value (joyz)
Function X360_ReturnTrigger#(port)

If port=-1 Then
	returnvalue#=JoyZ()
Else
	returnvalue#=JoyZ(port)
End If

Return returnvalue#

End Function

;TriggerDown()
;returns the value of the current trigger that is down
Function X360_TriggerDown(port)

If port=-1 Then
	z=JoyZDir()
Else
	z=JoyZDir(port)
End If

;return
Return z

End Function

;AButtonHit()
;returns true is the a button was hit
Function X360_AButtonHit(port)

If port=-1 Then
	If JoyHit(1) Then Return True
Else
	If JoyHit(1,port) Then Return True
End If

Return False

End Function

;BButtonHit()
;returns true is the b button was hit
Function X360_BButtonHit(port)

If port=-1 Then
	If JoyHit(2) Then Return True
Else
	If JoyHit(2,port) Then Return True
End If

Return False

End Function

;YButtonHit()
;returns true is the y button was hit
Function X360_YButtonHit(port)

If port=-1 Then
	If JoyHit(4) Then Return True
Else
	If JoyHit(4,port) Then Return True
End If

Return False

End Function

;XButtonHit()
;returns true is the x button was hit
Function X360_XButtonHit(port)

If port=-1 Then
	If JoyHit(3) Then Return True
Else
	If JoyHit(3,port) Then Return True
End If

Return False

End Function

;RBButtonHit()
;returns true is the rbumper was hit
Function X360_RBButtonHit(port)

If port=-1 Then
	If JoyHit(6) Then Return True
Else
	If JoyHit(6,port) Then Return True
End If

Return False

End Function

;LBButtonHit()
;returns true is the lbumper was hit
Function X360_LBButtonHit(port)

If port=-1 Then
	If JoyHit(5) Then Return True
Else
	If JoyHit(5,port) Then Return True
End If

Return False

End Function

;StartButtonHit()
;returns true is the start button was hit
Function X360_StartButtonHit(port)

If port=-1 Then
	If JoyHit(8) Then Return True
Else
	If JoyHit(8,port) Then Return True
End If

Return False

End Function

;BackButtonHit()
;returns true is the x button was hit
Function X360_BackButtonHit(port)

If port=-1 Then
	If JoyHit(7) Then Return True
Else
	If JoyHit(7,port) Then Return True
End If

Return False

End Function

;LStickButtonHit()
;returns true is the left stick pressed in (hit)
Function X360_LStickButtonHit(port)

If port=-1 Then
	If JoyHit(9) Then Return True
Else
	If JoyHit(9,port) Then Return True
End If

Return False

End Function

;RStickButtonHit()
;returns true is the right stick pressed in (hit)
Function X360_RStickButtonHit(port)

If port=-1 Then
	If JoyHit(10) Then Return True
Else
	If JoyHit(10,port) Then Return True
End If

Return False

End Function

;AButtonDown()
;returns true is the a button was Down
Function X360_AButtonDown(port)

If port=-1 Then
	If JoyDown(1) Then Return True
Else
	If JoyDown(1,port) Then Return True
End If

Return False

End Function

;BButtonDown()
;returns true is the b button was Down
Function X360_BButtonDown(port)

If port=-1 Then
	If JoyDown(2) Then Return True
Else
	If JoyDown(2,port) Then Return True
End If

Return False

End Function

;YButtonDown()
;returns true is the y button was Down
Function X360_YButtonDown(port)

If port=-1 Then
	If JoyDown(4) Then Return True
Else
	If JoyDown(4,port) Then Return True
End If

Return False

End Function

;XButtonDown()
;returns true is the x button was Down
Function X360_XButtonDown(port)

If port=-1 Then
	If JoyDown(3) Then Return True
Else
	If JoyDown(3,port) Then Return True
End If

Return False

End Function

;RBButtonDown()
;returns true is the rbumper was Down
Function X360_RBButtonDown(port)

If port=-1 Then
	If JoyDown(6) Then Return True
Else
	If JoyDown(6,port) Then Return True
End If

Return False

End Function

;LBButtonDown()
;returns true is the lbumper was Down
Function X360_LBButtonDown(port)

If port=-1 Then
	If JoyDown(5) Then Return True
Else
	If JoyDown(5,port) Then Return True
End If

Return False

End Function

;StartButtonDown()
;returns true is the start button was Down
Function X360_StartButtonDown(port)

If port=-1 Then
	If JoyDown(8) Then Return True
Else
	If JoyDown(8,port) Then Return True
End If

Return False

End Function

;BackButtonDown()
;returns true is the x button was Down
Function X360_BackButtonDown(port)

If port=-1 Then
	If JoyDown(7) Then Return True
Else
	If JoyDown(7,port) Then Return True
End If

Return False

End Function

;LStickButtonDown()
;returns true is the left stick pressed in (Down)
Function X360_LStickButtonDown(port)

If port=-1 Then
	If JoyDown(9) Then Return True
Else
	If JoyDown(9,port) Then Return True
End If

Return False

End Function

;RStickButtonDown()
;returns true is the right stick pressed in (Down)
Function X360_RStickButtonDown(port)

If port=-1 Then
	If JoyDown(10) Then Return True
Else
	If JoyDown(10,port) Then Return True
End If

Return False

End Function

Comments

TaskMaster
(Posted 19 years ago)
All of the buttons on the 360 controller are analog. Is it not possible to retrieve that info?

In other words, you can determine how hard a person is pushing the A button.

xtremegamr
(Posted 19 years ago)
I dunno. I'm just pretty much using the Joy functions in blitz.

xtremegamr
(Posted 19 years ago)
I was playing around with the Joy functions and I found out you can test the D-Pad for input, too! Here's the code:

-------------------------------------------------------------------------------
;DPadDir()
;returns the direction of the d-pad
Function X360_DPadDir(port)

If port=-1 Then
dir=JoyHat()
Else
dir=JoyHat(port)
End If

;check direction
Select dir
Case 0 ;up
Return 1
Case 45 ;up-right
Return 2
Case 90 ;right
Return 3
Case 135 ;down-right
Return 4
Case 180 ;down
Return 5
Case 225 ;down-left
Return 6
Case 270 ;left
Return 7
Case 315 ;up-left
Return 8
Case -1 ;no direction pressed
Return 0
Default ;just in case something goes VERY wrong
Return -1
End Select

End Function

TaskMaster
(Posted 19 years ago)
Nice to see the D-Pad. But, why not just return the 0, 45, 90, etc...

Then your function could Return dir.

And what is dir equal to when you return -1, or have you seen that happen?

Stevie G
(Posted 19 years ago)
http://www.blitzbasic.com/codearcs/codearcs.php?code=1592

xtremegamr
(Posted 19 years ago)
I've never seen anything that would make this function return -1; that was just in case anything went very wrong.

Also, I just realized, the RightStickX() and RightStickY() functions work. It's the RightStickXDir() and RightStickYDir() that is the problem.

Mortifer Nex
(Posted 19 years ago)
Probably if someone pushes down three buttons and pops one off, you're still pushing down the buttons, right?

Hmmm...

Code Archives Forum