Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ashtynj · Feb 23, 2020 at 06:58 PM · optimizationif-statementskeyboard input

How to check each key to see which ones are being pressed

I'm making a game with a 3D keyboard that squashes the keys down when you press them, however I have run into an issue. to check that a key is being pressed, I have a script in a parent empty that checks every single letter to see if it's being pressed and scales the associated child. I use the following code in void Update()

  if (Input.GetKeyDown("q"))
             {
                 temp = transform.localScale;
                 temp.x = 1.5f;
                 temp.y -= .5f;
                 temp.z = 1.5f;
                 parent.transform.GetChild(0).transform.localScale = temp;
             }
  if (Input.GetKeyUp("q"))
             {
                 temp = transform.localScale;
                 temp.x = 1.5f;
                 temp.y += .5f;
                 temp.z = 1.5f;
                 parent.transform.GetChild(0).transform.localScale = temp;
             }

  if (Input.GetKeyDown("w"))
             {
                 temp = transform.localScale;
                 temp.x = 1.5f;
                 temp.y -= .5f;
                 temp.z = 1.5f;
                 parent.transform.GetChild(1).transform.localScale = temp;
             }
  if (Input.GetKeyUp("w"))
             {
                 temp = transform.localScale;
                 temp.x = 1.5f;
                 temp.y += .5f;
                 temp.z = 1.5f;
                 parent.transform.GetChild(1).transform.localScale = temp;
             }

This does work for every letter I add, but it's tedious and seriously long. I'd like to know if there's an alternative way other than checking every single key individually. I'm a beginner to C# so if you could use easier-to-understand terms it'd be very much appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hellium · Feb 23, 2020 at 07:17 PM

If all the keys have the same behaviour:

 void Update()
 {
     for ( KeyCode key = KeyCode.A ; key <= KeyCode.Z ; ++key )
     {
         if ( Input.GetKeyDown( key ) )
         {
             temp = transform.localScale;
             temp.x = 1.5f;
             temp.y -= .5f;
             temp.z = 1.5f;
             parent.transform.GetChild( 0 ).transform.localScale = temp;
         }

         if ( Input.GetKeyUp( key ) )
         {
             temp = transform.localScale;
             temp.x = 1.5f;
             temp.y += .5f;
             temp.z = 1.5f;
             parent.transform.GetChild( 0 ).transform.localScale = temp;
         }
     }
 }


If you want to prevent two keys from being pressed at the same time:

 private KeyCode pressedKey = KeyCode.None;

 void Update()
 {
     for ( KeyCode key = KeyCode.A ; key <= KeyCode.Z ; ++key )
     {
         if ( pressedKey == KeyCode.None && Input.GetKeyDown( key ) )
         {
             temp = transform.localScale;
             temp.x = 1.5f;
             temp.y -= .5f;
             temp.z = 1.5f;
             parent.transform.GetChild( 0 ).transform.localScale = temp;
             pressedKey = key;
         }

         if ( pressedKey == key && Input.GetKeyUp( key ) )
         {
             temp = transform.localScale;
             temp.x = 1.5f;
             temp.y += .5f;
             temp.z = 1.5f;
             parent.transform.GetChild( 0 ).transform.localScale = temp;
             pressedKey = KeyCode.None;
         }
     }
 }


According to the details you gave in your comment:

 private KeyCode pressedKey = KeyCode.None;

 // Order it the way you want
 private KeyCode[] keys = new KeyCode[]
 {
     KeyCode.A, // Will change child #0
     KeyCode.B,
     KeyCode.C,
     KeyCode.D,
     KeyCode.E,
     KeyCode.F,
     KeyCode.G,
     KeyCode.H,
     KeyCode.I,
     KeyCode.J,
     KeyCode.K, // Will change child #10
     KeyCode.L,
     KeyCode.M,
     KeyCode.N,
     KeyCode.O,
     KeyCode.P,
     KeyCode.Q,
     KeyCode.R,
     KeyCode.S,
     KeyCode.T,
     KeyCode.U,
     KeyCode.V,
     KeyCode.W,
     KeyCode.X,
     KeyCode.Y,
     KeyCode.Z, // Will change child #25
 };

 void Update()
 {
     for ( int keyIndex = 0 ; keyIndex < keys.Length ; ++keyIndex )
     {
         KeyCode key = keys[keyIndex];
         if ( pressedKey == KeyCode.None && Input.GetKeyDown( key ) )
         {
             temp = transform.localScale;
             temp.x = 1.5f;
             temp.y -= .5f;
             temp.z = 1.5f;
             parent.transform.GetChild( keyIndex ).transform.localScale = temp;
             pressedKey = key;
         }

         if ( pressedKey == key && Input.GetKeyUp( key ) )
         {
             temp = transform.localScale;
             temp.x = 1.5f;
             temp.y += .5f;
             temp.z = 1.5f;
             parent.transform.GetChild( keyIndex ).transform.localScale = temp;
             pressedKey = KeyCode.None;
         }
     }
 }

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ashtynj · Feb 23, 2020 at 07:21 PM 0
Share

The issue with that is GetChild(num) is based on the order of the children of the parent. Basically if I use this then every input would just go to Q since that's GetChild(0). I need a corresponding number to go in place of 0. Something like an array that matches q to 0, w to 1, etc.

avatar image ashtynj ashtynj · Feb 23, 2020 at 07:59 PM 0
Share

Added some clarification in my original code snippet. Hope that will help.

avatar image Hellium ashtynj · Feb 23, 2020 at 08:00 PM 0
Share

I've updated my answer

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Same Code, Different Variables (Optimization) 1 Answer

What- if anything- is wrong with this shader (alpha/reflective/unlit)? 1 Answer

What is a fragment? 1 Answer

Differentiate an overlapping collision from just touching 1 Answer

Performance gain through recycling Objects? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges