CreateVirtualGamepad ( [preset$][,port] )

Parameters

preset: standard (default) or classic. port: -1 (lowest free, default), or 0..15.

Description

Creates visible controls on the primary graphics surface and returns a handle, or 0 with MobileInputError$. Explicit ports must be free. The pad feeds Joy and Gamepad commands immediately, including short taps. Standard has two sticks, d-pad, face/shoulder buttons, analog triggers, back/start and stick clicks. Classic adds d-pad movement to left axes.

Native overlays are supported on iOS, Android, macOS and Windows, including IDE run presets and saved layouts, and on the web, where the page draws them over the canvas. Controls use presentation coordinates, remain responsive during blocking input waits, and are excluded from SaveBuffer. See the mobile input guide.

Example

; CreateVirtualGamepad Example
; Virtual pad: existing Joy and Gamepad APIs, no touch update/draw commands.
Graphics 800,480,0,2
SetBuffer BackBuffer()
Local pad=CreateVirtualGamepad("standard")
If pad=0 Then RuntimeError MobileInputError$()
; Keep only the controls used by this game.
VirtualControlVisible VirtualControl(pad,"east"),0
VirtualControlVisible VirtualControl(pad,"west"),0
VirtualControlVisible VirtualControl(pad,"north"),0
VirtualControlVisible VirtualControl(pad,"left-shoulder"),0
VirtualControlVisible VirtualControl(pad,"right-shoulder"),0
VirtualControlVisible VirtualControl(pad,"left-click"),0
VirtualControlVisible VirtualControl(pad,"right-click"),0
Local port=VirtualGamepadPort(pad),x#=400,y#=210,score=0
Local target_x#=530,target_y#=210,previous=MilliSecs()
While Not KeyHit(1)
    If GamepadHit(7,port) Then Exit
    If GamepadHit(8,port) Then score=0
    Local now=MilliSecs(),dt#=Float(now-previous)/1000
    previous=now
    If dt<0 Then dt=0
    If dt>0.05 Then dt=0.05
    Local speed#=180*(1-0.75*GamepadAxis(5,port)+GamepadAxis(6,port))
    x=x+(GamepadAxis(1,port)+GamepadDown(14,port)-GamepadDown(13,port))*speed*dt
    y=y+(GamepadAxis(2,port)+GamepadDown(12,port)-GamepadDown(11,port))*speed*dt
    If x<15 Then x=15
    If x>785 Then x=785
    If y<85 Then y=85
    If y>465 Then y=465
    If GamepadHit(1,port)
        If (x-target_x)^2+(y-target_y)^2<65^2
            score=score+1
            target_x=Rnd(180,610)
            target_y=Rnd(120,280)
        EndIf
    EndIf
    Cls
    Color 255,255,255
    Text 150,48,"Move L/d-pad; aim R; LT slows, RT boosts. Score: "+score
    Text 150,68,"Tap A near gold. Start resets; Back exits. Hat: "+JoyHat(port)
    Text 150,88,"Triggers: "+GamepadAxis(5,port)+", "+GamepadAxis(6,port)
    Color 255,200,40
    Oval target_x-15,target_y-15,30,30,True
    Color 70,200,255
    Oval x-12,y-12,24,24,True
    Line x,y,x+GamepadAxis(3,port)*55,y+GamepadAxis(4,port)*55
    Flip
Wend
FreeVirtualControls pad
End

Open example ยท Command index