- Home /
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
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'.
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
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);
}
}
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;
$$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.
Answer by miro-bartos · May 20, 2014 at 12:33 PM
Debug.Log((string)Input.inputString);
FYI - This works, but does not work for a game controller...
@Jonesy19 do you have a way to make it work for game controller ? Or a way to get controller input
Your answer
Follow this Question
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