|When () Key Pressed|
The keys available to be used in this block include the entire alphabet ( Experimental Viewer, the key (or key on Macs) could also be chosen.etc.), the number keys ( etc.), the arrow keys ( ), and the key. In the
- Main article: List of Block Workarounds
This block can be partially replicated with the following code:
when flag clicked forever wait until <key [wanted key v] pressed?> . . .
|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 forever if <key [key v] pressed?> then broadcast [ready v]
when I receive [ready v] . . .
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] stamp change x by (20)
- Starting animations
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:||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.|
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: 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.
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.