Mouse and Keyboard
MOUSE BUTTON? SETMOUSESHAPE MOUSESHAPE READCHAR
This section shows you how to communicate with the mouse and keyboard.
MOUSE
MOUSE
reports the coordinates of the mouse pointer. See the section about Coordinates if you need to learn about them.
If the tip of the mouse pointer is exactly in the center of the screen, MOUSE
will report [0 0].
You can type MOUSE
in the Listener panel, move the mouse to where you want it to be, and then press
If you want to easily move the turtle to anywhere on the screen, use this set of instructions:
PU SETPOS MOUSE PD
You could even draw with the mouse pointer using this procedure:
TO CHASE
SETPOS MOUSE
IF BUTTON? [STOP]
CHASE
END
You could also write the same program like this, which is more efficient for the computer:
TO CHASE
WHEN [MOUSE MOVED] [SETXY MOUSE]
WHEN [MOUSE CLICKED] [STOP]
WAIT -1
END
» Things to Try
Experiment with the MOUSE
command and find interesting ways to use it.
BUTTON?
BUTTON?
reports TRUE if the left mouse button is pressed down and FALSE if it is not.
You could draw some straight lines by clicking the mouse using this procedure:
TO LINES
IF BUTTON? [SETPOS MOUSE]
LINES
END
» Things to Try
Think of other ways to use BUTTON?
.
You can add it in programs you write.
SETMOUSESHAPE number
(SETMOUSESHAPE)
SETMOUSESHAPE
takes a number as input and changes the shape of the mouse pointer. The mouse still works normally, but will look different. You have probably seen most of these shapes as you have used your computer in other programs.
Try this command to turn the mouse pointer into an hourglass:
SETMOUSESHAPE 2
To restore the original mouseshape use one of these commands:
SETMOUSESHAPE 1
or
(SETMOUSESHAPE)
Note that the mouseshape will only be changed when it is on top of the Graphics panel. This allows you to keep writing and editing your programs with the standard mouseshape.
» Things to Try
Have fun with your friends, writing programs that change the mouse pointer shape, which they won’t expect.
MOUSESHAPE
MOUSESHAPE
reports the number of the current mouse pointer shape.
SETMOUSESHAPE 5
MOUSESHAPE
Result: 5
» Things to Try
Can you write a program that randomly changes the mouse pointer shape?
Can you write a program that goes through all the shapes in order, waiting a half-second between them?
READCHAR
RC
Use READCHAR
to capture a key that the user types.
TO KEYS
PR READCHAR
KEYS
END
To stop the program, you need to press the red X (stop) icon in the menu bar or the red square (stop) at the top of the Listener panel.
This more complicated program accepts any key, but stops when you press the Esc key.
TO KEYS
MAKE "THISKEY RC
IF :THISKEY = "ESC THEN STOP ELSE PR :THISKEY
KEYS
END
You’ll learn how to accept words and sentences from the user in the Getting User Input section.
» Things to Try
Did you try pressing all the keys on the keyboard?
The READCHAR
reporter can tell if the user pressed BACKSPACE, or SPACE, ESC, or any other key.<br/
You can write programs that use this information to make decisions.