From Test-Scratch-Wiki

当 () 键被按下
2.0 When () Key Pressed.png
Category 事件类积木
Type 半圆形积木

The When () Key Pressed 积木 is an 事件类积木 and a 半圆形积木. 程式 placed underneath this block will activate when the specified key is pressed.

The keys that can be sensed with 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. After an update, this block now includes an any option, which allows one to press any key to operate the block.

In the 实验查看器, the ↵ Enter key (or return key on Macs) could also be sensed.

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
當 [空白 v] 鍵被按下
廣播訊息 [Fire! v]
重複 (5) 次 
  y 改變 (5)
  • Typing on a word processor
當 [a v] 鍵被按下
造型換成 [a v]
x 改變 (20)
當 [空白 v] 鍵被按下
廣播訊息 [Animation starts! v]
播放音效 [Intro v]
  • Moving around a map
當 [向上 v] 鍵被按下
y 改變 (15)

Note Note: Because of the built in delay, the <key [ v] pressed?> Boolean is often used for movement, as it will execute its script more rapidly and make movement smoother.


Main article: List of Block Workarounds

This block can be partially replicated with the following code:

當 @greenflag 被點擊
  如果 <[某 v] 鍵被按下?> 那麼 
    . . .

Note Note: This workaround is not exact, as the hat block will break off in the middle of a 程式, 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 does not require the green flag to be pressed.

A more precise workaround:

when gf clicked
  如果 <[某 v] 鍵被按下?> 那麼 
    廣播訊息 [ready v]
當收到訊息 [ready v]
. . .


Cookies help us deliver our services. By using our services, you agree to our use of cookies.