<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Check If a Key is Pressed]]></title><description><![CDATA[<h2><a class="anchor-offset" name="introduction"></a>Introduction</h2>
<p dir="auto">You can check if a key is pressed using the following 2 boolean blocks in the sensing category:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8e2d4b02-9857-455e-acee-90ed6c591410.png" width="400" style="border-radius:5px;border:1px solid #29622d" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="the-key-selection-pressed-block"></a>The “Key [Selection] pressed?” Block</h2>
<p dir="auto">If the specified key is pressed down <strong>at the moment this block is played</strong>, then it will report “true”; otherwise, it will report “false”.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4419eb9c-2d66-4e8d-88af-47307225af65.gif" alt="p.gif" class=" img-responsive img-markdown" width="332" height="178" /></p>
<p dir="auto">You can click any key in the dropdown to select it:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/dcb80809-4485-4fe4-917b-5f1a440f02a0.gif" alt="p.gif" class=" img-responsive img-markdown" width="794" height="437" /></p>
<p dir="auto"> <br />
If you select the “Any” key, then this block will report “true” if any key is pressed.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="the-key-input-pressed-block"></a>The “Key [Input] pressed?” Block</h2>
<p dir="auto">With this block, you can specify the key by typing instead of selecting from the dropdown. You can also use a variable as the input:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1ef647b8-189e-4861-b4cd-fcf2c3c0bd12.gif" alt="p.gif" class=" img-responsive img-markdown" width="541" height="320" /></p>
<p dir="auto"> <br />
Note that upper case and lower case letters are treated as the same for this block.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="a-common-mistake"></a>A Common Mistake</h2>
<p dir="auto">One common mistake is to check for the key press in a single “if” block like this:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/515a5f3d-49fa-490d-bd26-c6cbf1363f88.gif" alt="p.gif" class=" img-responsive img-markdown" width="608" height="322" /></p>
<p dir="auto">This won’t work because the program will run through the “if” and “key pressed?” blocks very quickly when you click the green flag button. Therefore, if you press the right arrow after clicking the green flag, <strong><span style="color:#fa0000">the program will not respond to you, because it has already ended</span></strong>.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="combined-with-forever-if"></a>Combined with Forever + If</h2>
<p dir="auto">Most of the time, the correct way is to use the “key pressed” sensing block together with a forever block and an “if” block. This way, <strong>the program will repeatedly check if a key is pressed</strong>.</p>
<p dir="auto">For example, we can turn the ball sprite left when the left arrow key is pressed, and turn it right when the right arrow key is pressed.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/cb91b975-256d-4f9b-ad41-b03d7145d37d.gif" alt="p.gif" class=" img-responsive img-markdown" width="576" height="450" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="check-for-2-keys"></a>Check for 2 Keys</h2>
<p dir="auto">You can also check if 2 keys are both pressed at the same time. For example, when both the “a” and “w” keys are pressed, turn the ball. If you only press one key, then the ball will not turn.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f70651b9-d051-420e-90e3-430ac15ea333.gif" alt="p.gif" class=" img-responsive img-markdown" width="739" height="347" /></p>
]]></description><link>https://forum.creaticode.com/topic/667/check-if-a-key-is-pressed</link><generator>RSS for Node</generator><lastBuildDate>Sun, 07 Jun 2026 04:50:08 GMT</lastBuildDate><atom:link href="https://forum.creaticode.com/topic/667.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Sep 2022 13:18:11 GMT</pubDate><ttl>60</ttl></channel></rss>