(Redirected from Mouse Down)

Mouse Down?
mouse down?
Category Sensing
Type Boolean

The mouse down? block is a sensing block and a boolean block. The block returns "True" if any of the computer mouse buttons are being pressed while the cursor is over the stage.

Example Uses

As this block checks if the mouse is being pressed, it is useful as a substitute for click detection. It is frequently used in one sprite one script projects; if the project requires clicking, this block can be used to serve as a replacement for the When () Clicked block, as the hat block cannot be used in the middle of a script.

Some common uses for the mouse down? block are:

  • Sensing when an object should be lifted with the not () block (when the mouse is not held anymore):
when green flag clicked
forever
    wait until <mouse down?>
    wait until < not <mouse down?> >
    repeat until <mouse down?>
        go to (mouse-pointer v)
        wait until < not <mouse down?> >
    end
end
  • Sensing with draggable objects
  • Logging mouse clicks
when green flag clicked
reset timer
forever
    if <mouse down?> then
        set [down v] to (timer)
        wait until <not <mouse down?> >
        set [up v] to (timer)
        set [elapsed v] to ( (up) - (down) )
        add (down) to [mouse-downs v]
        add (up) to [mouse-ups v]
        add (elapsed) to [down times v]
    end
end
  • Detecting if the mouse is clicking the sprite
when green flag clicked
forever
    if < <mouse down?> and <touching (mouse-pointer v)?> > then
        say [You're clicking me!]
    else
        say [You released the mouse!]
    end
end

Limitations

The digital click, or tap to click feature of trackpads won't work with projects that use this block to detect clicking. Instead, when possible, try to detect mouse clicks with the when this sprite clicked block, which does. Note that this block is still preferable when expecting the user to hold down the mouse button, or for applications unlikely to be used by trackpad users, such as mobile interfaces.

Variants

In some Scratch modifications, there are similar blocks which allow the user to detect specific mouse buttons as opposed to any mouse button. Some Scratchers have requested this feature to be implemented in Scratch itself.[1] This was previously available in Scratch 1.x, where the When This Sprite Clicked block would register if the left or right mouse was clicked [2], however, outside of hacked blocks, there is no way to get a block that can sense right mouse clicks separately from left mouse clicks.

See Also

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.