GetJoy ( [port] )
Parameters
| port (optional) - which joystick to read; 0 is the first stick (default) |
Description
|
Takes the next joystick button press out of the queue and returns its number. Unlike JoyDown and JoyHit you do not have to say which button you are interested in. GetJoy hands back the number of whichever button was pressed, or 0 when nothing is waiting, which makes it a tidy fit for a Select/Case block that handles the whole pad in one place. It is also the easiest way to build a "press a button to assign it" control-remapping screen. Presses are queued in order and each call removes one, so drain the queue each frame with a loop that keeps calling GetJoy until it returns 0 and nothing is ever missed. FlushJoy empties it. Heads up: the current DirectX 12 runtime does not enumerate joysticks - it reports zero attached devices - so GetJoy always returns 0, and an out-of-range port returns 0 rather than raising an error. Guard your joystick code with JoyType and keep a keyboard or mouse route to everything. See also: JoyHit, JoyDown, WaitJoy, FlushJoy, JoyType, GetKey. |
Example
; GetJoy Example ; -------------- Graphics 640,480,0,2 SetBuffer BackBuffer() last_btn=0 While Not KeyDown(1) Cls ; GetJoy polls ALL the joystick buttons at once and returns the ; number of the one pressed (0 if none) - no need to test each ; button separately as with JoyDown/JoyHit. b=GetJoy() If b>0 Then last_btn=b If JoyType()=0 Then Text 0,0,"Esc: exit" Text 0,60,"No joystick detected - plug in a gamepad to try this example." Text 0,80,"With one attached, pressing any button lights the lamp below." Else Text 0,0,"Press any joystick button Esc: exit" End If ; A big lamp for the most recent button If last_btn>0 Then Color 255,220,0 Oval 295,200,50,50,True Color 255,255,255 Text 320,270,"Button "+last_btn,True End If Text 0,20,"GetJoy() now: "+b+" last button: "+last_btn Flip Wend End
Index