Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
1
Question by Screenhog · Nov 10, 2012 at 12:24 AM · keycodepressed

Can I detect if a letter key is pressed

I want to detect if a letter key is pressed. Is there a quick way of doing this? There's a long way, that would look something like this:

 if (Input.GetKeyDown(KeyCode.A)) {
         
 } else if (Input.GetKeyDown(KeyCode.B)) {
     
 } else if (Input.GetKeyDown(KeyCode.C)) {
     
 } else if (Input.GetKeyDown(KeyCode.D)) {
 
 etc.

That's pretty inefficient, though. Does anyone have a better way?

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

6 Replies

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

Answer by DaveA · Nov 10, 2012 at 12:46 AM

Probably this: http://docs.unity3d.com/Documentation/ScriptReference/Input-inputString.html in conjunction with Input.anyKeyDown. Unity's Input, esp keyboard, is schizo. You could also use an OnGUI function and look at the Event.current structure to get the key hit and compare it to a range of keys like >= KeyCode.A && <= KeyCode.Z

Also see here: http://answers.unity3d.com/questions/343939/how-to-make-a-key-listener-in-unity.html

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 Screenhog · Nov 10, 2012 at 01:14 AM 0
Share

That's good, but I want to limit it to specifically letters. No other keys (like numbers or punctuation) should make it do anything).

avatar image meat5000 ♦ · Nov 23, 2015 at 03:56 PM 0
Share

OnGUI $$anonymous$$eyCode example is here

Just use a switch to filter the $$anonymous$$eyCodes.

avatar image
4

Answer by featon415 · Aug 16, 2018 at 04:41 AM

I know this is an old post but seeing as this is the first result to appear in Google search for this problem, I wanted to make sure that the solution was accessible for everyone.

The answer to the original problem is as such:

 using UnityEngine;
 using System;
 
       void OnGUI()
         {
     
             Event e = Event.current;
     
     //Check the type of the current event, making sure to take in only the KeyDown of the keystroke.
     //char.IsLetter to filter out all other KeyCodes besides alphabetical.
         if (e.type == EventType.KeyDown &&
 e.keyCode.ToString().Length == 1 &&
 char.IsLetter(e.keyCode.ToString()[0]))
             {
     //This is your desired action
                 Debug.Log("Detected key code: " + e.keyCode);
             }
     
         }

Using "char.IsLetter(char)" will filter out all other Keystrokes, as long as pass the KeyCode in as a char by get the first index of the KeyCode string as a char, hence "keycode.ToString()[0]". Getting the Length makes sure that the Key is actually a letter, like "A", and not "Down", which would still pass through otherwise.

I hope that snippet is useful to whoever needs and is searching for it like I was, and I apologize in advance to whoever gets a notification of this post.

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 Ermiq · Aug 16, 2018 at 05:08 AM 0
Share

They have to put your answer up to the top. Nice and handy solution.

avatar image
1

Answer by Albarnie · Nov 23, 2015 at 10:16 AM

@Screenhog

this might work:

try using "GetButton" instead of "GetKey", Go into: Edit/Project Settings/Input and add the letter you want in and set the positive to the letter:

Edit/Project Settings/Input

Like this:

InputManager

Then, change your script to use:

if (Input.GetButton ("E")) {

}

I hope this solved your problem.

(I'm new to Unity, so if there are any stupid mistakes, don't laugh at me.)


screen-shot-2015-11-23-at-70420-pm.png (127.7 kB)
screen-shot-2015-11-23-at-70457-pm.png (56.7 kB)
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
0

Answer by Screenhog · Nov 23, 2012 at 07:33 PM

So, it appears that the answer is "no". There's ways of detecting if any key is pressed, but nothing by default that limits it to the letters of the alphabet.

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
0

Answer by savlon · Nov 23, 2015 at 12:55 PM

Can you not just create your own method? Disregard the method and variable names, as they are only there for ease of understanding.

Here is a method that loops through all of the desired keys variable and checks whether is has been pressed down. If it has it returns true, otherwise it will return false. It's simple and easy.

 private KeyCode[] desiredKeys = {KeyCode.A, KeyCode.B, KeyCode.C};
 
 public bool HasALetterBeenPressed ()
 {
     foreach (KeyCode keyToCheck in desiredKeys)
     {
         if (Input.GetKeyDown (keyToCheck))
             return true;
     }
     return false;
 }

You could make it more efficient but no point in pre-optimization if its not needed.

You can then call the method like so...

 void Update () 
 {
     if (HasALetterBeenPressed ())
     {
         Debug.Log ("A letter has been pressed.");
     }
 }

Give that a try. Obviously I haven't added all of the keys you wish to check for, but i'm sure you won't have an issue with that.

Goodluck.

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 meat5000 ♦ · Nov 23, 2015 at 03:55 PM 0
Share

Indeed, just jam it through a custom class which contains a switch statement with the alphabet.

  • 1
  • 2
  • ›

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

17 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

Related Questions

Key down, to edit property 1 Answer

Integer value casting to KeyCode enum in JS? 3 Answers

Problems with focus input and detect key. 0 Answers

Problems with KeyCodes 2 Answers

How can i open in game manu (GUI) with button? 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