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?
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.
Probably this: Unity - Scripting API: Input.inputString
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
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.
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.
If you are running your code inside the update function or inside an IEnumerator, in which case you expect the code to be run once per frame and be essentially ignored of no keys were pressed that frame, you could use the following:
for (int i = (int)KeyCode.A; i < (int)KeyCode.Z; i++) {
if (Input.GetKeyDown ((KeyCode)i)) {
Debug.Log ((char)i); // prints the key as a character to the console
Debug.Log (i - (int)KeyCode.A) // prints a number from 0-25, depending on how far into the alphabet i is
}
}