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. 知识库
  3. 二维积木
  4. 检测某个键是否被按下

检测某个键是否被按下

Scheduled Pinned Locked Moved 二维积木
1 Posts 1 Posters 760 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.
  • C Offline
    C Offline
    creaticodeteacher2-mail
    wrote on last edited by admin
    #1

    介绍

    可以用如下2个侦测类别中的布尔积木检测某个键是否被按下:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/215322c1-4847-41b1-9965-fc6423eac079.png" width="400" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    积木:“【请选择】键被按下?”

    当这个积木运行的时候,如果指定的键被按下,积木就报告“true(真)”;否则就报告“false(假)”。

    Animation1.gif

    在下拉菜单中点击任意一个键即可选中该键:

    Animation2.gif

     
    假如你选择“任意”键,如果任意一个键被按下,这个积木就报告“true(真)”。

     
     

    积木:“(请输入)键被按下?”

    对于这个积木,你可以输入指定的键,而不是在下拉菜单中选择。还可以把变量作为输入值:

    Animation3.gif !

     
    注意:这个积木的输入值不区分大小写。

     
     

    一个常见错误

    一个常见错误就是用单个积木“如果( )那么”来检测是否按下键:

    Animation4.gif

    这行不通,因为当你点击绿旗按钮时,程序就迅速运行完这2个积木——“如果( )那么”和“( )键被按下?”。因此,当你点击绿旗之后再按下向右箭头,程序就没有反应,因为程序早已经结束了。

     
     

    结合“重复执行”、“如果( )那么”

    大多数情况下,正确方式就是将侦测积木“( )键被按下?”与积木“重复执行”、“如果( )那么”一起使用。这样的话,程序会重复不停地检测是否按下某个键。

    例如,当按下向左箭头时球向左转,当按下向右箭头时球向右转。

    Animation5.gif

     
     

    同时检测2个键

    还可以检测2个键是否同时被按下。例如,当同时按下“a”和“w”,球就转动。如果只按下一个键,球就不转。

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