Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jpc133 · Sep 11, 2014 at 08:40 PM · javascriptinputefficiency

Detecting and saving keyboard presses without using more than 20 if statements.

My question is if its possible to make more efficient code to handle keyboard press detection than what I already have. This is the code I use at the moment:

 if(Input.GetKeyDown(KeyCode.Alpha1)){
 screen_data[0] = screen_data[0] + "1";
 }
 if(Input.GetKeyDown(KeyCode.Alpha2)){
 screen_data[0] = screen_data[0] + "2";
 }
 if(Input.GetKeyDown(KeyCode.Alpha3)){
 screen_data[0] = screen_data[0] + "3";
 }
 if(Input.GetKeyDown(KeyCode.Alpha4)){
 screen_data[0] = screen_data[0] + "4";
 }
 if(Input.GetKeyDown(KeyCode.Alpha5)){
 screen_data[0] = screen_data[0] + "5";
 }
 if(Input.GetKeyDown(KeyCode.Alpha6)){
 screen_data[0] = screen_data[0] + "6";
 }
 if(Input.GetKeyDown(KeyCode.Alpha7)){
 screen_data[0] = screen_data[0] + "7";
 }
 if(Input.GetKeyDown(KeyCode.Alpha8)){
 screen_data[0] = screen_data[0] + "8";
 }
 if(Input.GetKeyDown(KeyCode.Alpha9)){
 screen_data[0] = screen_data[0] + "9";
 }
 if(Input.GetKeyDown(KeyCode.Alpha0)){
 screen_data[0] = screen_data[0] + "0";
 }
 if(Input.GetKeyDown(KeyCode.Q)){
 screen_data[0] = screen_data[0] + "q";
 }
 if(Input.GetKeyDown(KeyCode.W)){
 screen_data[0] = screen_data[0] + "w";
 }
 if(Input.GetKeyDown(KeyCode.E)){
 screen_data[0] = screen_data[0] + "e";
 }
 if(Input.GetKeyDown(KeyCode.R)){
 screen_data[0] = screen_data[0] + "r";
 }
 if(Input.GetKeyDown(KeyCode.T)){
 screen_data[0] = screen_data[0] + "t";
 }
 if(Input.GetKeyDown(KeyCode.Y)){
 screen_data[0] = screen_data[0] + "y";
 }
 if(Input.GetKeyDown(KeyCode.U)){
 screen_data[0] = screen_data[0] + "u";
 }
 if(Input.GetKeyDown(KeyCode.I)){
 screen_data[0] = screen_data[0] + "i";
 }
 if(Input.GetKeyDown(KeyCode.O)){
 screen_data[0] = screen_data[0] + "o";
 }
 if(Input.GetKeyDown(KeyCode.P)){
 screen_data[0] = screen_data[0] + "p";
 }
 if(Input.GetKeyDown(KeyCode.A)){
 screen_data[0] = screen_data[0] + "a";
 }
 if(Input.GetKeyDown(KeyCode.S)){
 screen_data[0] = screen_data[0] + "s";
 }
 if(Input.GetKeyDown(KeyCode.D)){
 screen_data[0] = screen_data[0] + "d";
 }
 if(Input.GetKeyDown(KeyCode.F)){
 screen_data[0] = screen_data[0] + "f";
 }
 if(Input.GetKeyDown(KeyCode.G)){
 screen_data[0] = screen_data[0] + "g";
 }
 if(Input.GetKeyDown(KeyCode.H)){
 screen_data[0] = screen_data[0] + "h";
 }
 if(Input.GetKeyDown(KeyCode.J)){
 screen_data[0] = screen_data[0] + "j";
 }
 if(Input.GetKeyDown(KeyCode.K)){
 screen_data[0] = screen_data[0] + "k";
 }
 if(Input.GetKeyDown(KeyCode.L)){
 screen_data[0] = screen_data[0] + "l";
 }
 if(Input.GetKeyDown(KeyCode.Z)){
 screen_data[0] = screen_data[0] + "z";
 }
 if(Input.GetKeyDown(KeyCode.X)){
 screen_data[0] = screen_data[0] + "x";
 }
 if(Input.GetKeyDown(KeyCode.C)){
 screen_data[0] = screen_data[0] + "c";
 }
 if(Input.GetKeyDown(KeyCode.V)){
 screen_data[0] = screen_data[0] + "v";
 }
 if(Input.GetKeyDown(KeyCode.B)){
 screen_data[0] = screen_data[0] + "b";
 }
 if(Input.GetKeyDown(KeyCode.N)){
 screen_data[0] = screen_data[0] + "n";
 }
 if(Input.GetKeyDown(KeyCode.M)){
 screen_data[0] = screen_data[0] + "m";
 }
 if(Input.GetKeyDown(KeyCode.Period)){
 screen_data[0] = screen_data[0] + ".";
 }
 if(Input.GetKeyDown(KeyCode.Backspace)){
 if(screen_data[0] == ""){
 }
 else{
 screen_data[0] = screen_data[0].Substring(0, screen_data[0].length - 1);
 }
 

Comment
Add comment · Show 1
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 robertbu · Sep 12, 2014 at 09:33 AM 1
Share

Don't have time for a long answer or example code, but take a look at Input.inputString. In addition you might take a look at GUI and the Event class. You can get both the keycode and the character just typed inside of OnGUI() using Event.current.

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

How to code input. 1 Answer

Play a simple animation once on key press 3 Answers

displaying playerprefs 1 Answer

Get touch position? 0 Answers

Text.Field erratically updating 0 Answers


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