GetKey won't work

Hi, I heve problem with GetKey. I create new project, create script put there code:

function Update () { 
  if(Input.GetKey("m") || Input.GetKey(KeyCode.M)) { 
    Debug.Log("Look, this works!"); 
  } 
}

add this code to Main Camera and play and press m, Shift+m nothing happends.
Try GetKeyDown or GetKeyUp - nothing.
The interesting thing that

  if ( Input.inputString == "m" ) 
  	Debug.Log("input = m");

this code print “input = m” each time when m button is pressed.

Try to setup key in Edit->Project Settings->Input menu. GetAxis and GetButton won’t work too. Could you help me.

158195–5716–$new_unity_project_3_121.zip (1.07 MB)

var times = 0;
function Update () 
{ 
	if(Input.GetKeyDown("m"))
	{ 
		times++;
		if(Input.GetKey("left shift") || Input.GetKey("right shift"))
		{
		Debug.Log("M has been clicked "+ times + "time(S)! this time it was shift and m!"); 
		}
		else if ( Input.inputString == "M") 
		{
    		Debug.Log("caps and m were pressed!"); 
		}
		else
		{
			Debug.Log("M has been clicked "+ times + "time(S)! this time it was just m!"); 
		}
	} 
}

This code can distinguish between caps and m, shift and m, or just plain m.

The first script posted by alexbl works just fine here on my machine (Unity 2.5, Mac OS X).

I use Unity 2.5 and Max OS X and it won’t work for me.
Could you help me?

And script by Flynn won’t work as well :cry:
Don’t know what to do. People help, please :!:

The first script will only work if m is the only thing pressed.
You would better use blabla.ToLower().Contains(“m”) there to check if m is one of the currently pressed keys.

Or alternatively something in the way of Input.GetKey(Keycode.Key_M)

There are no Keycode.Key_M there are only KeyCode.M, but it won’t work too.

Then the script likely is never executed.
You can test that with a small Debug.Log(“test test”); at the beginning of the function. if the log shows no test test then the function is never called, be it because the object is inactive, because the component is inactive or because the component isn’t present at all in that scene

I agree with the above, step 1 would be to ensure that your script is getting called at all, perhaps testing with Input.anyKey or Input.anyKeyDown once you’re sure it’s executing. This will help sort out if the script getting triggered at all is the issue or if it’s the key detection in particular.

Ok. I add this debug message to the Update() function. Now “test test” flooding the console, and still work only “Input.inputString == “m””, but “Input.GetKey(“m”) || Input.GetKey(KeyCode.M)” won’t work :cry:
Here my code:

function Update () { 
//	Debug.Log("test test");
  if(Input.GetKey("m") || Input.GetKey(KeyCode.M)) { 
    Debug.Log("Look, this works!"); 
  } 
  if ( Input.inputString == "m" ) 
  	Debug.Log("input = m");
}

I can see “input = m” in console, but never see “Look, this works!”. Have you idea how find this bug. Probably I should setup smth in project settings?

Interesting thing that old project (FPS Tutorial for instance I remember sometime ago I could move with a,s,d,w key, but now only with keyboard arrow) it won’t work too.

ANY IDEA ABOUT IT? :?: :!:

STOP SHOUTING, seriously…

And no, I don’t have any concrete ideas as your code works perfectly for me. Do you have a different language setting/configuration that could have your system “confused” as to what key is actually being pressed? Is it only “m” that fails or do other keys produce similar results?

Just an idea… As there are two debugs in that latest code, could one debug be wiping the other off the screen? Perhaps you should open the full debug window (if you have not all ready) and check for both?

sorry, but this is big problem for me. I can’t work with keyboard press in nomral way. Is there are any debug message or smth like this to understand you and me where is the problem lay?

I use russian and english both, but I see message from Input.inputString that user press ‘m’, and it works ok, problem appear with GetKey or even GetAxis if there are alphabetic key.

Could this problem appear I upgrade Unity 2.4 to 2.5?

Real this is not a problem. Btw does New Unity Project 3.zip work ok for you?
I open console window there only “input = m” message.

To be brutally honest that doesn’t matter one iota, there is no reason to yell. We’re here trying to help you but are not at your personal beck and call, we too have jobs, work and other things to attend to. So please, go easy on us! :slight_smile:

Read Flynn’s reply above, test against other keys, test on other machines, more data is needed. So far we have perfectly fine code failing for one key on one machine. Test a few variations and let’s see what comes of that (different keys, different language settings, different machines, etc.)

Where did you get Unity 2.4??? :wink: :stuck_out_tongue:

It might help but I’m doubtful as this wasn’t a specific issue in 2.1 AFAIK. But apart from this issue I think you ought to upgrade anyway as 2.5 offers plenty o’ benefits.

Sorry, million times. Yes, you are right.

ok. I’ll collect as much information about it as I can. See you late.

may be it’s not 2.4, but it’s version before 2.5 (I don’t remember, but guess that it was 2.4). I download it about a month before 2.5 release.

So what I could find.
There are no problem with unity 2.5 or project which I produce on other computer. Even if I made web player GetKey become work in correct way. So the problem in my Unity Editor or configuration of it.
Do you think it could help if I reinstall unity (do I have problem with activate unity with my key if I reinstall it)?

I’ve seen several instances where people are having the same trouble I seem to be having intercepting a keystroke. Here’s my code that doesn’t work.

function Update()
{
if(Input.GetButton(KeyCode.C))
x = a + b;
}

What I get is this error:

BCE0017:The best overload for the method ‘UnityEngine.Input.GetButton(String)’ is not compatible with the argument list ‘(UnityEngine.KeyCode)’

This seems pretty simple but cannot get past this problem. I also haven’t found any solutions in the threads I’ve been reading on this subject.

I’m a newbie and assuming this is caused by something simple I’m missing.

BTW, I’ve tested to see if function Update() is being called at all using a print() statement and that does work.

Thanks in advance for any assistance.

jkellar, you want to use GetKey or GetKeyDown if you’re passing a keycode.

Ah Ha. I knew it had to be something simple I was doing wrong. That worked. Thanks.