m (Workaround)
(Trying out a template...)
Line 8: Line 8:
 
}}The '''When () Key Pressed''' [[Blocks|block]] is a [[Control Blocks|Control block]] and a [[Hat Block|Hat block]]. [[Script|Scripts]] that wear this block will activate when the specified key is pressed.
 
}}The '''When () Key Pressed''' [[Blocks|block]] is a [[Control Blocks|Control block]] and a [[Hat Block|Hat block]]. [[Script|Scripts]] that wear this block will activate when the specified key is pressed.
  
The keys available to be used in this block include the entire alphabet, the number keys, the arrow keys, and the space key.
+
The keys available to be used in this block include the entire alphabet ( {{key press|a}} {{key press|b}} {{key press|c}} etc.), the number keys ( {{key press|0}} {{key press|1}} {{key press|2}} etc.), the arrow keys ( {{key press|left}} {{key press|up}} {{key press|right}} {{key press|down}} ), and the {{key press|space}} key.
In the [[Experimental Viewer]], the enter key can also be chosen.
+
In the [[Experimental Viewer]], the {{key press|enter}} key can also be chosen.
  
 
==Workaround==
 
==Workaround==

Revision as of 00:55, 5 May 2012

When () Key Pressed
When Key Pressed.png
Category Control
Type Hat
The When () Key Pressed block is a Control block and a Hat block. Scripts that wear 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 can also be chosen.

Workaround

This block can be partially replicated with the following code:

when flag clicked
forever
 wait until <key [wanted key v] pressed?>
 action goes here


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.

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]
stamp
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.

See Also