New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Scratch› Sensing, Input and Operators

Sensing, Input and Operators

💻 Scratch

Read the keyboard, mouse and user answers, then use operators to process them.

Lesson 7 of 9 Tutorial
0 of 9 completed

Sensing blocks let a project react to the player. Operators let it calculate, compare and join values.

By the end of this lesson you can:
  • Detect key presses and mouse clicks.
  • Ask the player for input.
  • Use arithmetic and join blocks.

Keyboard and Mouse

Use key pressed? inside a forever loop for smooth movement. Use mouse down? for clicking and mouse x or mouse y to follow the pointer.

when green flag clicked
forever
  if <key [right arrow v] pressed?> then
    change x by (5)
  end
  if <key [left arrow v] pressed?> then
    change x by (-5)
  end
end

Ask and Answer

The ask ... and wait block shows an input box. The player's answer is stored in the answer reporter.

when green flag clicked
ask [What is your name?] and wait
say (join [Hello, ] (answer))

Operators

Use +, -, * and / for maths. Use pick random for chance, round for whole numbers and join to combine text.

Tip Store the answer in a variable if you need it later. The answer reporter changes the next time the player is asked.

Practice

  1. Ask the player for two numbers and say their sum.
  2. Move the sprite with the arrow keys.
  3. Use pick random to place the sprite at a random x position.

Hands-on exercise

Write a script that:
1. Asks the player for a number.
2. Stores the answer in a variable.
3. Says the number doubled.

Recap

Sensing blocks read the player and the environment. Operators process the values into answers the project can use.

Example

when green flag clicked
forever
  if <key [right arrow v] pressed?> then
    change x by (5)
  end
  if <key [left arrow v] pressed?> then
    change x by (-5)
  end
end

Exercises

Exercise 1. Ask and calculate Easy
Write a script that:
1. Asks the player for a number.
2. Stores the answer in a variable.
3. Says the number doubled.
Scratch exercise-1.txt
Expected Output
Entering 7 produces the message: Double is 14

Lesson Nav

Lesson sections