Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 Ninita · Mar 19, 2013 at 03:07 PM · inputkeygetkeydowninput.getkey

Get numeric key

How I get a numeric key pressed? I can't with the KeyCode.KeyPad neither with KeyCode.JoystickButton.

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

4 Replies

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

Answer by Ninita · May 10, 2013 at 02:33 PM

SOLVED. is if(Input.GetKeyDown(KeyCode.Alpha1)) what I wanted but I hasn't implementing this on a Monobehaviour class, that was the problem, although I'm using library UnityEngine and although the method that implementing this is called on a Update() from another class. thanks

Comment
Add comment · 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
15

Answer by Anxo · Dec 03, 2015 at 07:59 PM

Here is another Option if someone else runs into this.

 private KeyCode[] keyCodes = {
         KeyCode.Alpha1,
         KeyCode.Alpha2,
         KeyCode.Alpha3,
         KeyCode.Alpha4,
         KeyCode.Alpha5,
         KeyCode.Alpha6,
         KeyCode.Alpha7,
         KeyCode.Alpha8,
         KeyCode.Alpha9,
     };
 
 for(int i = 0 ; i < keyCodes.Length; i ++ ){
   if(Input.GetKeyDown(keyCodes[i])){
          int numberPressed = i+1;
          Debug.Log(numberPressed);
   }
 
 // WUCC check for errors. 
 }
Comment
Add comment · Show 5 · 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 Erdrako · Jun 28, 2016 at 04:38 PM 0
Share

This is what i was exactly looking for! Thanks!

avatar image Itzemii · Jul 31, 2017 at 11:19 AM 0
Share

I hadn't thought of this nice, extensible way. Thank you!

avatar image Bamalam69 · Nov 09, 2018 at 08:17 AM 0
Share

I know this is old, but I love this so much.

avatar image Gaming32 · Dec 28, 2019 at 02:30 PM 0
Share

Also, $$anonymous$$eyCode.Alpha0 should be included.

avatar image RemusRain · Apr 23, 2021 at 08:30 PM 0
Share

You forgot the 0! :D

avatar image
3

Answer by roojerry · Mar 19, 2013 at 03:16 PM

How about KeyCode.

  Alpha0   The '0' key on the top of the alphanumeric keyboard.
 Alpha1     The '1' key on the top of the alphanumeric keyboard.
 Alpha2     The '2' key on the top of the alphanumeric keyboard.
 Alpha3     The '3' key on the top of the alphanumeric keyboard.
 Alpha4     The '4' key on the top of the alphanumeric keyboard.
 Alpha5     The '5' key on the top of the alphanumeric keyboard.
 Alpha6     The '6' key on the top of the alphanumeric keyboard.
 Alpha7     The '7' key on the top of the alphanumeric keyboard.
 Alpha8     The '8' key on the top of the alphanumeric keyboard.
 Alpha9     The '9' key on the top of the alphanumeric keyboard.


next time take a look at the reference material first before asking on UA

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 Ninita · Mar 19, 2013 at 04:29 PM 0
Share

I saw that material and tested all the possibilities. with $$anonymous$$eyCode.Alpha I also don't get numerical keys. I'm testing as follows:

         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$eypad1))
             Debug.Log ("keypad...");
         
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.JoystickButton1))
             Debug.Log ("joystick...");
         
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha1))
             Debug.Log ("alpha1...");

and it don't show me anything

avatar image Ninita · Mar 19, 2013 at 04:32 PM 0
Share

and the next time ask me if I saw that yet

avatar image roojerry · Mar 19, 2013 at 04:34 PM 0
Share

odd. you are checking for Input inside your Update function?

avatar image roojerry · Mar 19, 2013 at 04:47 PM 0
Share

I see no reason for those calls you have shown not to work for keyboard/joystick input. Is this being run in the editor?

avatar image
2

Answer by Nimzo93 · Oct 24, 2020 at 06:22 PM

For future reference,

Here's an alternative to detect keycodes 0-9, avoiding a block of if statements or setting up enums/arrays and forloops!

 Using System;
 if(Input.inputString != ""){
     int number;
     bool is_a_number = Int32.TryParse(Input.inputString, out number);
     if (is_a_number && number >= 0 && number < 10){
         // do some work
     }
 }
           
Comment
Add comment · 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

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

22 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

Related Questions

GetKey/GetButton not working if 2 Adjacent keys are being pressed at the same time 1 Answer

How to check which key i have pressed? 3 Answers

Input.GetKeyDown doesn't work with several keys 1 Answer

Why is GetKeyDown returning false when provided with string from .inputString? 2 Answers

Toggle bool with keypress -2 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