- Home /
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?
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
That's good, but I want to limit it to specifically letters. No other keys (like numbers or punctuation) should make it do anything).
OnGUI $$anonymous$$eyCode example is here
Just use a switch to filter the $$anonymous$$eyCodes.
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.
They have to put your answer up to the top. Nice and handy solution.
Answer by Albarnie · Nov 23, 2015 at 10:16 AM
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:
Like this:
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.)
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.
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.
Indeed, just jam it through a custom class which contains a switch statement with the alphabet.
Your answer
Follow this Question
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