BlitzMax/Modules/User input/Joystick

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The BlitzMax freejoy module contains commands that report the status of any joysticks and game controllers connected to the system.

Functions[edit | edit source]

JoyCount[edit | edit source]

Function JoyCount()

Description: Counts the number of joysticks.

Returns: The number of joysticks and gamecontrollers connected to the system.

Example:

' testjoy.bmx

Import Pub.FreeJoy

Strict

If Not JoyCount() RuntimeError "No joystick found!"

Graphics 640,480

Function drawprop(n$,p#,y)
	Local	w
	DrawText n$,0,y
	w=Abs(p)*256
	If p<0
		DrawRect 320-w,y,w,16
	Else
		DrawRect 320,y,w,16
	EndIf
End Function		

Local t=0

While Not KeyHit(KEY_ESCAPE)
	Cls
	
	SetColor 255,255,255
	Local n=JoyCount()
	DrawText "joycount="+n,0,0
	DrawText "JoyName(0)="+JoyName(0),0,20
	DrawText "JoyButtonCaps(0)="+Bin$(JoyButtonCaps(0)),0,40
	DrawText "JoyAxisCaps(0)="+Bin$(JoyAxisCaps(0)),0,60

	For Local i=0 To 31
		SetColor 255,255,255
		If JoyDown(i) SetColor 255,0,0
		DrawOval i*16,80,14,14
	Next
	
	SetColor 255,255,0
	drawprop "JoyX=",JoyX(0),100
	drawprop "JoyY:",JoyY(0),120
	drawprop "JoyZ:",JoyZ(0),140
	drawprop "JoyR:",JoyR(0),160
	drawprop "JoyU:",JoyU(0),180
	drawprop "JoyV:",JoyV(0),200
	drawprop "JoyHat:",JoyHat(0),220
	drawprop "JoyWheel:",JoyWheel(0),240
	
	DrawRect 0,280,t,10
	t=(t+1)&511
	
	Flip	
Wend

End

JoyButtonCaps[edit | edit source]

Function JoyButtonCaps(port)

Description: Available buttons (on/off controls) on a joystick.

Returns: A bitfield representing which buttons are present.

JoyAxisCaps[edit | edit source]

Function JoyAxisCaps(port)

Description: Available axis (proportional controls) on a joystick.

Returns: A bitfield representing which axis are available.

Information: The bit positions of the returned value correspond to the following constants defined in the FreeJoy module:

  • Const JOY_X=0
  • Const JOY_Y=1
  • Const JOY_Z=2
  • Const JOY_R=3
  • Const JOY_U=4
  • Const JOY_V=5
  • Const JOY_YAW=6
  • Const JOY_PITCH=7
  • Const JOY_ROLL=8
  • Const JOY_HAT=9
  • Const JOY_WHEEL=10

JoyName[edit | edit source]

Function JoyName$(port)

Description: Get the name of the joysticks connected to the specified port.

Returns: The system name of the joystick.

JoyDown[edit | edit source]

Function JoyDown( button,port=0 )

Description: Test the status of a joystick button.

Returns: True if the button is pressed.

JoyHit[edit | edit source]

Function JoyHit( button,port=0 )

Description: Check for a joystick button press

Returns: Number of times button has been hit.

Information: The returned value represents the number of the times button has been hit since the last call to JoyHit with the same specified button.

JoyX[edit | edit source]

Function JoyX#( port=0 )

Description: Reports the horizontal position of the joystick.

Returns: Zero if the joystick is centered, -1 if Left, 1 if Right or a value in between.

JoyY[edit | edit source]

Function JoyY#( port=0 )

Description: Reports the vertical position of the joystick.

Returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value in between.

JoyZ[edit | edit source]

Function JoyZ#( port=0 )

Description: Reports the position of the joystick's Z axis if supported.

Returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value in between.

JoyR[edit | edit source]

Function JoyR#( port=0 )

Description: Reports the position of the joystick's R axis if supported.

Returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value in between.

JoyU[edit | edit source]

Function JoyU#( port=0 )

Description: Reports the position of the joystick's U axis if supported.

Returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value in between.

Information: The U value of a joystick usually corresponds to a joystick's 'slider' or 'throttle' feature, although this may vary depending on the joystick, and will not be available with all joysticks.

JoyV[edit | edit source]

Function JoyV#( port=0 )

Description: Reports the position of the joystick's V axis if supported.

Returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value in between.

Information: The V value of a joystick usually corresponds to a joystick's 'slider' or 'throttle' feature, although this may vary depending on the joystick, and will not be available with all joysticks.

JoyYaw[edit | edit source]

Function JoyYaw#( port=0 )

Description: Reports the position of the joystick's YAW axis if supported.

Returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value in between.

JoyPitch[edit | edit source]

Function JoyPitch#( port=0 )

Description: Reports the position of the joystick's PITCH axis if supported.

Returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value in between.

JoyRoll[edit | edit source]

Function JoyRoll#( port=0 )

Description: Reports the position of the joystick's ROLL axis if supported.

Returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value in between.

JoyHat[edit | edit source]

Function JoyHat#( port=0 )

Description: Reports the position of the joystick's HAT controller if supported.

Returns: -1.0 if the joystick is centered, and values between 0.0, 0.25, 0.5 and 0.75 for the directions Up, Right, Down, Left respectively.

JoyWheel[edit | edit source]

Function JoyWheel#( port=0 )

Description: Reports the position of the joystick's WHEEL controller if supported.

Returns: Zero if the joystick is centered, -1.0 if Left, 1.0 if Right or a value in between.

FlushJoy[edit | edit source]

Function FlushJoy( port_mask=~0 )

Description: Flush joystick button states.