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 /
This question was closed Jun 02, 2013 at 03:30 AM by Ochreous for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ochreous · Jun 01, 2013 at 11:21 PM · c#numberkeys

C# Accessing keys 1-9 All at Once

Hi everyone, I'm trying to make a script that checks to see if any of the keys 1-9 have been pressed. But I'm getting an error from the console saying keycode doesn't contain a definition for alpha. Is there a way to access keys 1-9 all at once like this?

     void OnGUI () {
       for (int i = 0; a < 9; i++){
      if(Input.GetKeyDown(KeyCode.Alpha[i])){
     GUI.Box (new Rect (0,Screen.height - 50,100,50), "Item"+ i.ToString());
     GUI.Box (new Rect (Screen.width/2,Screen.height/2,100,50), "Item"+ i.ToString()+1 );
     GUI.Box (new Rect (Screen.width - 100,Screen.height - 50,100,50), "Item "+ i.ToString()+2);
             }
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

  • Sort: 
avatar image
1
Best Answer

Answer by numberkruncher · Jun 01, 2013 at 11:46 PM

Yes this can be achieved easily using the `Event` object instead.

Here is a simple example - Warning, not tested ;)

 // Zero indicates that no number key is pressed.
 private int numDown;

 void OnGUI() {
     // Render boxes if numeric key 1-9 is pressed.
     if (numDown != 0) {
         GUI.Box(
             new Rect(0, Screen.height - 50, 100, 50),
             "Item " + numDown
         );
         GUI.Box(
             new Rect(Screen.width/2, Screen.height/2, 100, 50),
             "Item " + (numDown + 1)
         );
         GUI.Box(
             new Rect(Screen.width - 100, Screen.height - 50, 100, 50),
             "Item " + (numDown + 2)
         );
     }

     // Process keyboard events as needed.
     int num;
     if (Event.current.type == EventType.KeyDown) {
         // Convert to numeric value for convenience :)
         num = Event.current.keyCode - KeyCode.Alpha1 + 1;

         // Avoid having multiple number keys pressed!
         if (numDown == 0 && num >= 1 && num <= 9) {
             numDown = num;
             Event.current.Use();
         }
     }
     else if (Event.current.type == EventType.KeyUp) {
         num = Event.current.keyCode - KeyCode.Alpha1 + 1;
         if (numDown == num) {
             numDown = 0;
             Event.current.Use();
         }
     }
 }

I am unclear as to why you are attempting to place GUI.Box for the split second in which the key is pressed. Perhaps you could comment with greater detail and I will amend my answer for you :)

Comment
Add comment · Show 3 · 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 Ochreous · Jun 01, 2013 at 11:55 PM 0
Share
     void OnGUI() {
      //for (int i = 0; a < 9; i++){
     if (Event.current.type == EventType.$$anonymous$$eyDown) {
     if (Event.current.keyCode >= $$anonymous$$eyCode.Alpha1
     && Event.current.keyCode <= $$anonymous$$eyCode.Alpha9) {
     // Create three GUI.Boxes when one of the keys is pressed
     // and have them each be different depending on which key was pressed
     //GUI.Box (new Rect (0,Screen.height - 50,100,50), "Item"+ i.ToString());
         //GUI.Box (new Rect (Screen.width/2,Screen.height/2,100,50), "Item"+ i.ToString()+1 );
         //GUI.Box (new Rect (Screen.width - 100,Screen.height - 50,100,50), "Item "+ i.ToString()+2);
     Event.current.Use();
       }
     }
   }
 }

avatar image numberkruncher · Jun 02, 2013 at 12:35 AM 0
Share

I've updated my answer for you :)

avatar image Ochreous · Jun 02, 2013 at 12:49 AM 0
Share

Can I possible use a for loop with this? I really want to use a for loop to make it a bit easier to work with.

 public int someint;
     void OnGUI () {
 for (int i = 0; i < 9; i++){
  if (Event.current.type == EventType.$$anonymous$$eyDown) {
 if (Event.current.keyCode >= $$anonymous$$eyCode.Alpha1
 && Event.current.keyCode <= $$anonymous$$eyCode.Alpha9) {
 someint = i;
 Event.current.Use();
                 }
             }
         }
     }
 }

Follow this Question

Answers Answers and Comments

15 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

Related Questions

C# Number Key For Loop 1 Answer

C# Non-Static Member Rigidbody2D.MovePosition 1 Answer

How to make specific text in a string array Bold C# 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Movement Script Help 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