Blitz3D+ Command Reference

MouseDown ( button )

Parameters

button - which mouse button to test
  1: left button
  2: right button
  3: middle button

Description

Returns True while the given mouse button is being held down.

This is the mouse twin of KeyDown: it reports the live state of one button, 1 while it is held and 0 while it is not. Reach for it whenever "still pressed" is the thing that matters - dragging a unit across the map, holding to keep firing, painting with a brush in a level editor, spinning the camera while the right button is down.

Each button is tested separately, so a drag with two buttons down is just two MouseDown calls. Because it reads state rather than consuming an event, several parts of your code can check the same button in the same frame and all of them see it - unlike MouseHit, which hands out a press once and then forgets it. Use MouseHit for a click that should do something once, MouseDown for anything continuous.

Pair it with MouseX and MouseY to know where the button is being held. The usual drag idiom is to remember the position on the first frame the button goes down and compare it with the current position every frame after.

Gotcha: losing the window focus clears the held state, so a button you were holding reads 0 after an alt-tab until it is pressed again. FlushMouse does not affect MouseDown either - it clears counted clicks, not the live button state.

See also: MouseHit, GetMouse, MouseX, MouseY, FlushMouse, KeyDown.

Example

; MouseDown Example
; -----------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; Hide the Windows pointer - the example draws its own crosshair
HidePointer

While Not KeyDown(1)

    Cls
    mx=MouseX()
    my=MouseY()

    ; MouseDown returns 1 for as long as a button is HELD.
    ; Button numbers: 1=left, 2=right, 3=middle.
    ; (Compare MouseHit, which reports each click only once.)
    If MouseDown(1) Then
        ; A continuous laser beam streams from the turret while the
        ; left button stays held
        Color 255,60,60
        Line 318,460,mx,my
        Line 322,460,mx,my
    End If

    ; Turret base
    Color 200,200,200
    Rect 300,460,40,20,True

    ; Crosshair
    Color 255,255,0
    Oval mx-10,my-10,20,20,False
    Line mx-14,my,mx+14,my
    Line mx,my-14,mx,my+14

    Color 255,255,255
    Text 0,0,"Hold the left button to fire the laser   Esc: exit"
    Text 0,20,"MouseDown(1)="+MouseDown(1)+"  MouseDown(2)="+MouseDown(2)+"  MouseDown(3)="+MouseDown(3)

    Flip

Wend

End

Index