Revision as of 00:27, 21 September 2013 by Turkey3 (talk | contribs) (Updated and fixed some.)

When () Key Pressed
When Key Pressed.png
Category Event
Type Hat
The When () Key Pressed block is a Control block and a Hat block. Scripts placed underneath this block will activate when the specified key is pressed.

The keys available to be used in this block include the entire alphabet ( a b c etc.), the number keys ( 0 1 2 etc.), the arrow keys ( ), and the space key. In the Experimental Viewer, the ↵ Enter key (or ↵ Return key on Macs) could also be chosen.


Main article: List of Block Workarounds

This block can be partially replicated with the following code:

when flag clicked
 wait until <key [wanted key v] pressed?>
 . . .

Note Note: This workaround is not exact, as the hat block will break off in the middle of a script, but the workaround will not. The built-in delay in the hat block (if you hold down the key, there will be a slight pause, followed by a shorter pause) is not inherent in the workaround. Also, the hat block doesn't require the green flag to be pressed.

A more precise workaround:

when gf clicked
if <key [key v] pressed?> then
broadcast [ready v]
when I receive [ready v]
. . .

Example Uses

Keys are often used to control things — this block can be very useful here. Some common uses:

  • Controlling an object
when [space v] key pressed
broadcast [Fire! v]
repeat (5)
 change y by (5)

  • Typing on a word processor
when [a v] key pressed
switch to costume [a v]
change x by (20)

when [space v] key pressed
broadcast [Animation starts! v]
play sound [Intro v]

  • Moving around a map
when [up arrow v] key pressed
change y by (15)
Note Note: Because of the built in delay, the "( ) key pressed?" Boolean is often used for movement, as it will execute its script more rapidly and make movement smoother.

Sensing Scrolling

Note Warning: This only works on the offline version!

Using the combination of the "key pressed" reporter and its hat counterpart, it is possible to sense when somebody is using the scroll wheel. The following script is one way of doing this.

when key [up arrow v] pressed
if <not <key [up arrow v] pressed?>> then
 . . .

The action is done when the scroll wheel is scrolled up. If the key pressed blocks are changed to the down arrow, it will sense when it is scrolled down. This works because the hat script senses the scroll wheel but the boolean does not. It can also work with right clicking as shown below.

when [Sprite1] clicked
if <not <mouse down?>> then
 . . .

Note Note: This would have to be on a sprite that was covering the whole screen, unless you want to only sense the right click in a certain part of the screen.

Multiple Keys

Many projects would benefit from using the boolean version of the block, as the hat block only reports one key at a time. For example if you press the up and right arrow key at the same time, the hat block would report the first key pressed of the two, then report the second key pressed, but never at the same time, whereas the boolean reacts to multiple key presses at once.

See Also