You can detect mouse movement in Scratch by using the mouse x and mouse y sensing blocks and comparing their current values to stored values in variables. The simplest working method is to create two variables (for example lastX and lastY), set them to the mouse position once at the start, then in a forever loop check whether the mouse has moved away from those stored values, update them, and trigger your action. Scratch has no built-in "mouse moved" event block, so detection is always done by comparing positions frame by frame.

What You'll Need

  • A free Scratch account at scratch.mit.edu (or the offline Scratch Desktop app) — the online editor is the easiest option.
  • A project with at least one sprite, or you can use the Stage's backdrop scripts.
  • A mouse or trackpad. Scratch's mouse sensing blocks respond to trackpads and touchscreens too, but the values behave slightly differently on touch devices.
  • Basic familiarity with variables, loops and the Sensing category.

Step-by-Step

  1. Open your project. Go to scratch.mit.edu, sign in, and click Create to start a new project, or open an existing one. If you prefer offline editing, launch Scratch Desktop.
  2. Pick a sprite to hold the scripts. Click a sprite in the Sprite pane at the bottom right. Any sprite works; the mouse blocks are global, so the sprite does not need to be the one you want to react.
  3. Create two variables. Open the Variables category, click Make a Variable, and name the first one lastX. Repeat and name the second lastY. Leave both as "For all sprites" unless you specifically want per-sprite tracking.
  4. Initialise the stored position. Drag a when green flag clicked block from Events. Under it, place set [lastX] to (mouse x) and set [lastY] to (mouse y). The mouse x and mouse y reporters live in the Sensing category. This gives you a baseline so the first loop pass does not fire a false "moved" signal.
  5. Build the detection loop. Still under the green flag hat, add a forever block from Control. Inside it, place an if…then block. For the condition, drag an or operator from Operators, then drop two ( ) > ( ) comparison blocks into it. In the first comparison, put mouse x on the left and lastX on the right. In the second, put mouse y on the left and lastY on the right. This condition is true whenever the pointer has moved on either axis.
  6. Add the action and the update. Inside the if…then block, put whatever should happen when movement is detected — play a sound, change a costume, broadcast a message, move another sprite. Then, still inside the if-block, add set [lastX] to (mouse x) and set [lastY] to (mouse y) so the stored values catch up and the action does not repeat every frame while the mouse sits still.
  7. Test it. Click the green flag, then move the pointer over the Stage. Your action should fire. Stop moving and it should go quiet. Click the red stop sign to end the script.
  8. Add a sensitivity threshold (optional but recommended). A single-pixel jitter from a trackpad or a shaky hand will trigger the basic version constantly. Replace each > comparison with an abs block from Operators wrapped around a subtraction: abs of ((mouse x) - (lastX)) > 3, and the same for Y. This ignores tiny movements and only reacts to deliberate ones. Raise the number for less sensitivity, lower it for more.
  9. Detect direction if you need it. To know which way the mouse moved, compare the current and stored values separately. If mouse x > lastX the pointer moved right; if mouse x < lastX it moved left; the same logic on Y gives up and down. Store the result in a direction variable before you update lastX and lastY, otherwise the comparison will always be false.
  10. Reset cleanly. If your project has multiple green-flag scripts, make sure only one script owns the lastX and lastY variables. Two scripts writing to the same variables will fight each other and produce erratic detection.

Common Mistakes

  • Forgetting to update the stored values. If you check for movement but never set lastX and lastY to the new position, the condition stays true forever and your action fires every frame — usually visible as a sound that stutters or a sprite that vibrates.
  • Updating the variables before the check. If you set lastX to mouse x at the top of the loop and compare afterwards, the values are always equal and nothing ever triggers.
  • Using a single variable for both axes. Storing only mouse x means vertical movement is invisible. Use both.
  • Putting the loop in the wrong sprite. Mouse sensing is global, but the script runs only in the sprite that owns it. If nothing happens, check which sprite is selected.
  • Expecting an event block. There is no "when mouse moves" hat block in Scratch. Every detection is a poll inside a loop, which is why the forever block is essential.
  • Ignoring the Stage edge. Scratch's mouse x and y report the pointer position relative to the Stage, not the browser window. Moving the mouse over the script area does not change the values.

Tips

  • Use a threshold of 2–5 for a natural feel. Below 2, trackpad noise triggers constantly; above about 10, small deliberate movements are missed.
  • If you only care about horizontal movement (a slider, a paddle), skip the Y comparison entirely to keep the script readable.
  • Broadcast a message from inside the if-block instead of putting all your logic there. Other sprites can then react with their own when I receive hats, which keeps scripts modular.
  • For a "mouse idle" timer, invert the logic: if the position has not changed for N seconds, do something. Use a timer variable that resets whenever movement is detected.
  • Test with the project in full-screen mode. The Stage coordinates are the same, but the pointer behaves more predictably without the editor panels in the way.
  • Name variables clearly (lastMouseX rather than a) — you will thank yourself when the project grows.

Troubleshooting

Nothing happens at all. Check that the green flag script is attached to the sprite you selected, that the forever loop actually contains the if-block, and that the project is running (green flag highlighted, not stopped).

The action fires constantly. You are almost certainly missing the update step, or your threshold is too low. Add the set lastX/lastY blocks inside the if-block and raise the threshold.

It works once and then stops. The stored values were updated but the comparison was placed outside the loop, or the loop was accidentally removed. Rebuild the script from the green flag hat downwards.

It triggers when the mouse is still. Trackpads and some mice report tiny position changes. The abs threshold method in step 8 fixes this.

It triggers when you click buttons in the editor. Clicking the green flag or dragging blocks moves the pointer over the Stage area briefly. Test in full-screen mode to confirm the script itself is fine.

Movement over the sprite does not register. Mouse position is global, but if you are using touching mouse-pointer? instead of position comparison, the pointer must actually overlap the sprite's costume. Switch to the variable method for area-independent detection.

Frequently Asked Questions

Does Scratch have a built-in mouse movement event?

No. Scratch provides mouse x, mouse y, mouse down? and touching mouse-pointer? in Sensing, but no "when mouse moves" hat block. You detect movement by comparing current and stored positions in a loop.

Can I detect mouse movement on a touchscreen?

Yes, but only while a finger is touching the Stage. Scratch reports the touch position through the same mouse blocks, so a tap-and-drag registers as movement; a finger hovering above the screen does not.

Why does my detection fire every frame?

The stored position is not being updated after the check. Move the set lastX and set lastY blocks inside the if-block, after your action.

How do I make it ignore small movements?

Use an absolute-value threshold: abs of ((mouse x) - (lastX)) > 3. Any movement smaller than the threshold is ignored.

Can I detect movement direction?

Yes. Compare mouse x with lastX before updating: greater means right, smaller means left. Do the same on Y for up and down.

Does this work in Scratch 3.0 and Scratch Desktop?

Yes. The sensing and variable blocks used here are identical in the online editor and Scratch Desktop. Menu labels may differ slightly between versions.