JoyHat ( [port] )
Parameters
| port (optional) - which joystick to read; 0, the first attached stick (default) |
Description
|
Returns the direction of the joystick's POV hat switch, in degrees. The POV ("point of view") hat is the little eight-way thumb switch on top of a flight stick. JoyHat returns -1 while it sits centred, and otherwise the direction it is pressed as degrees clockwise from straight forward: 0 is forward, 90 right, 180 back, 270 left, with the diagonals in between - most hats step in multiples of 45. Classic uses are exactly what the name suggests: glancing the camera around the cockpit, plus menu navigation and weapon cycling. Since the return value doubles as both "a direction" and "not pressed", test for -1 first, then feed the angle to Sin/Cos to get a direction vector - the example's compass needle is one line of that. Don't expect a hat on ordinary gamepads: most expose their d-pad as buttons or as the main x/y axes instead, so JoyHat stays silent even though the pad works fine elsewhere. And note the no-stick reading is 0 - the same value as "pressed forward" - not -1, so check JoyType before trusting it. The modern Blitz3D+ runtime does not implement joystick input yet: JoyType always reports 0 and JoyHat always returns 0 there, whatever is plugged in - so for now the example shows its no-joystick screen. See also: JoyType, JoyX, JoyDown, JoyU, JoyPitch, GetJoy. |
Example
; JoyHat Example ; -------------- Graphics 640,480,0,2 SetBuffer BackBuffer() While Not KeyDown(1) Cls ; JoyHat returns the POV hat switch's direction in degrees ; (0=forward, 90=right, 180=back, 270=left), or -1 when the ; hat is centred. hat=JoyHat() If JoyType()=0 Then Text 0,0,"Esc: exit" Text 0,60,"No joystick detected - plug in a controller with a POV hat." Text 0,80,"With one attached, the compass needle follows the hat." Else Text 0,0,"Press the POV hat in any direction Esc: exit" End If ; A compass rose with a tick every 45 degrees cx=320 cy=280 Color 90,90,90 Oval cx-90,cy-90,180,180,False For a=0 To 315 Step 45 Line cx+Sin(a)*80,cy-Cos(a)*80,cx+Sin(a)*90,cy-Cos(a)*90 Next Text cx,cy-110,"0",True Text cx+105,cy,"90",True Text cx,cy+105,"180",True Text cx-105,cy,"270",True If hat>=0 Then ; The needle points the way the hat is pressed Color 255,220,0 Line cx,cy,cx+Sin(hat)*85,cy-Cos(hat)*85 Oval cx+Sin(hat)*85-4,cy-Cos(hat)*85-4,8,8,True Else Color 120,120,120 Text cx,cy,"centred",True,True End If Color 255,255,255 Text 0,20,"JoyHat() = "+hat+" (-1 = centred, otherwise degrees)" Flip Wend End
Index