When () Key Pressed (block)
|When () Key Pressed|
The keys that can be sensed with this block include the entire English alphabet (etc.), the number keys ( etc.), the arrow keys ( ), and the key. After an update, this block now includes an option, which allows one to press any key to operate the block.
In the Experimental Viewer, the key (or key on Macs) could also be sensed.
It is possible to use "hack blocks" which allows editing and changing of buttons that this block can detect.
The block will act similarly to typing text in a text box. There will be a delay between when the block first executes and the second time, but after that, there will be no delay until the key is released.
This block is used to get input from the player, for a variety of uses.
Some common uses include:
- 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 costume to [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 |
- Main article: List of Block Workarounds
This block can be partially replicated with the following code:
when flag clicked forever if <key [wanted key v] pressed?> then . . . end
|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 the key is held down, there will be a slight pause, followed by a shorter pause) is not inherent in the workaround. Also, the hat block does not 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] . . .
How to Write it in the Forums or Wiki
when [space v] key pressed