- "Key () Pressed" redirects here. For other uses, see Key Pressed (disambiguation).
Key () Pressed? | |
key (space v) pressed? | |
Category | Sensing |
Type | Boolean |
The Key () Pressed? block is a Sensing block and a Boolean block. The block checks if the specified key is pressed. If the key is being pressed, the block returns "true"; if it is not, it returns "false".
The keys available to be used in this block include the entire English 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 could also be chosen. After an update,[when?] this block now includes an any option, which allows one to press any key to operate the block.
Example Uses
As this block checks if a key is being pressed or not, it is useful for controlling objects, especially with One Sprite One Script Projects. If the project requires key input, this block can be used to serve as a replacement for the When () Key Pressed block. The Hat block cannot be used in the middle of a script.
Some common uses for the Key () Pressed? block:
- Controlling a character
forever if <key (down arrow v) pressed?> then change y by (-10) end if <key (up arrow v) pressed?> then change y by (10) end if <key (left arrow v) pressed?> then change x by (-10) end if <key (right arrow v) pressed?> then change x by (10) end
- Word processors
- Moving objects
- Changing slides in slideshows or changing what characters say
say [Hello!] wait until <key (any v) pressed?> wait until <not <key (any v) pressed?>> say [Welcome to my game!]
Sensing Scrolling
The When () Key Pressed block, the counterpart to the Key () Pressed? block senses the scroll wheel as well as the up/down keys. Combining these two blocks, it is possible to sense when somebody is using the scroll wheel. The following script is one way of doing this.
when [up arrow v] key 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.
Detecting More Keys
This block has the ability to detect keys that are not available in the dropdown menu. This can be done by inserting a block which reports a key's name into the dropdown:
<key (join [KEY NAME] []) pressed?>
This allows for the detection of special characters using ⇧ Shift or Alt; these keys may only be detected if another key is also being pressed.
Examples
The following detects if both ⇧ Shift and 1 are pressed (which creates an exclamation point in most text editors):
<key (join [] [!]) pressed?>
Similarly, this detects if both ⌥ Option and p are pressed (the keys for typing the pi symbol on macOS):
<key (join [] [π]) pressed?>
Individual characters may be placed in either section of the join()() block. The following code blocks perform the same function as those shown above:
<key (join [!] []) pressed?> <key (join [π] []) pressed?>
The letters for the ↵ Enter key may be divided among each section of the join()() block:
<key (join [enter] []) pressed?> <key (join [en] [ter]) pressed?> <key (join [] [enter]) pressed?>
Alternatively, a variable can be inserted into the dropdown:
<key (variable name) pressed?>
Another method of obtaining additional keys is by editing the project JSON file to point the input to a new value with a text editor, specifying an arbitrary value instead of one of the drop-down options. This can be used to make blocks like this:
<key (+ v) pressed>
To learn more about JSON and editing projects, read this article.
Note: | This does not work with keys such as Ctrl, Alt, Tab ⇆, ← Backspace, and ⇧ Shift, although it does work with ↵ Enter. |
Suggestions
There are three somewhat popular suggestions related to this block:
- Add more keys[1]
- Make a reporter block that says which keys are being pressed down. It would look like:
key pressed::sensing reporter
[2] - Make this a Stage Monitor
Some Scratch Modifications have incorporated these suggestions.