From Test-Scratch-Wiki

Translate: - English - 中文 
当 () 键被按下
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)
end
  • 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] 鍵被按下?> 那麼 
    . . .
  end
end


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]
  end
end
當收到訊息 [ready v]
. . .

参见