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
7
Question by Larry-Dietz · May 16, 2010 at 10:38 PM · inputkeypress

What key was pressed?

Is there any way to determine what key was pressed? Basically, what I want to do is this...

if (Input.anyKeyDown) {
     Debug.Log(keypress);
}

where keypress is the key that was pressed. I am only interested in alpha numeric keys, so I know I can do this by mapping the A key to "A" in the input preferences, etc... and a total of 36 if checks for all possible alpha numeric keys, but it seems there should be an easier way to do this.

So far I haven't come up with anything :(

Any help would be appreciated.

Thanks, -Larry

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
22
Best Answer

Answer by Eric5h5 · May 16, 2010 at 10:56 PM

Use Input.inputString, and check that the chars are >= 'a' and <= 'z', and >= '0' and <= '9'.

Comment
Add comment · Show 1 · 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 Larry-Dietz · May 16, 2010 at 11:29 PM 0
Share

Perfect. Exactly what I was looking for. Don't know how I missed that. Believe it or not, I actually went through the reference before posting this question :)

Thanks, -Larry

avatar image
21

Answer by ds44 · Jun 28, 2015 at 06:39 PM

Used for debug purposes... to see which joypad button/mouse key/keyboard key was pressed. Axis detection needs other approach.

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 public void detectPressedKeyOrButton()
 {
     foreach(KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
     {
         if (Input.GetKeyDown(kcode))
             Debug.Log("KeyCode down: " + kcode);
     }
 }
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
1

Answer by Maxfunkey · Jun 27, 2013 at 03:41 PM

 // You can use this FetchKey() Method if you like, 
 // it's from the top of my head but It 
 // should return which key is currently pressed down, 
 // although it will only return the first
 // key it finds, you can modify it to return 
 // KeyCode[] containing all keys currently held 
 // down if you need to.
     
     KeyCode FetchKey()
         {
             e = System.Enum.GetNames(typeof(KeyCode)).Length;
             for(int i = 0; i < e; i++)
             {
                 if(Input.GetKey((KeyCode)i))
                 {
                     return (KeyCode)i;
                 }
             }
             
             return KeyCode.None;
Comment
Add comment · Show 1 · 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 EvanTreborn · Feb 25, 2014 at 01:56 PM 1
Share

$$anonymous$$eyCode is no ordinary enum. If you do ($$anonymous$$eyCode)int you will end up with a lot unused integer values and only a fraction of valid ones. $$anonymous$$g. this will not check for mouse buttons or function buttons because their integer value is beyond the names.Length. To have a valid list of all available keycodes you need something like this:

 static private $$anonymous$$eyCode[] valid$$anonymous$$eyCodes;
 private void Init() {
   if(valid$$anonymous$$eyCodes!=null) return;
   valid$$anonymous$$eyCodes=($$anonymous$$eyCode[])System.Enum.GetValues(typeof($$anonymous$$eyCode));
 }

if you want the valid integer value you just have to use an integer array and add the conversion.

avatar image
0

Answer by miro-bartos · May 20, 2014 at 12:33 PM

Debug.Log((string)Input.inputString);

Comment
Add comment · Show 2 · 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 Jonesy19 · Apr 01, 2015 at 12:46 PM 0
Share

FYI - This works, but does not work for a game controller...

avatar image Xpoint · Oct 08, 2017 at 08:36 PM 0
Share

@Jonesy19 do you have a way to make it work for game controller ? Or a way to get controller input

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Odd input glitch 2 Answers

Perform event only once on keypress 1 Answer

Control config assignments 2 Answers

Animation Keypress Trigger Problem 0 Answers

Key reading from inputString, backspace only works the first time 4 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