Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. 2D Blocks
  4. Check If a Key is Pressed

Check If a Key is Pressed

Scheduled Pinned Locked Moved 2D Blocks
1 Posts 1 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

    Introduction

    You can check if a key is pressed using the following 2 boolean blocks in the sensing category:

    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;">

     
     

    The “Key [Selection] pressed?” Block

    If the specified key is pressed down at the moment this block is played, then it will report “true”; otherwise, it will report “false”.

    p.gif

    You can click any key in the dropdown to select it:

    p.gif

     
    If you select the “Any” key, then this block will report “true” if any key is pressed.

     
     

    The “Key [Input] pressed?” Block

    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.gif

     
    Note that upper case and lower case letters are treated as the same for this block.

     
     

    A Common Mistake

    One common mistake is to check for the key press in a single “if” block like this:

    p.gif

    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, the program will not respond to you, because it has already ended.

     
     

    Combined with Forever + If

    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, the program will repeatedly check if a key is pressed.

    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.gif

     
     

    Check for 2 Keys

    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.gif

    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode