[SOLVED] Input.GetKey not working (Unity 5.2)

EDIT:
Actually, I just found out this is a completely different issue. It’s not related to preprocessors. They do work.
What doesn’t work anymore is getting input from the keyboard:
csharp*__ <strong>*if (Input.GetKeyDown("l")) {Debug.Log("Pressed L");}*</strong> __*
None of my Input.GetKey or Input.GetKeyDown are getting called.
Touch input works still fine using UnityRemote 4.

Hello everyone!
Is this a bug or am I missing something?
I tried using #if UNITY_EDITOR #endif for code that is for testing only.
In C#, this works perfectly, but when doing this in Javascript, it won’t be called.

Documentation doesn’t state anything specific concerning Javascript.
( Unity - Manual: Conditional compilation )

Greetings,
Shu

I’ve been using it myself, but as #ifdef UNITY_EDITOR #endif afaik. Maybe it’s that difference? (I’m not too used to C preprocessor things)

EDIT: what was that code tag doing there

1 Like

I just found out this issue is related to Input rather than preprocessors, which I just edited in my original post. Thanks for your reply, though! I didn’t know about #ifdef!

out of curiosity if someone told you “my input doesn’t work” and expected you to know what the problem was based purely on that, what would you tell them?

I don’t know. What additional information should I give you to elaborate the situation?
When I put

csharp*__ <strong>*if (Input.GetKeyDown("l")) {Debug.Log("Pressed L");}*</strong> __*

(from edited original post)
in Update() and press the L button, there won’t be a message in the console.
On the other hand, all the touch events in Update() work when using UnityRemote, though.

Try using the Input.GetKeyDown(KeyCode code) overload. The string overload probably parses it to the KeyCode enum, which defines L but not l (non-capital L) (maybe it’s case sensitive, no idea).

Input.GetKey(KeyCode.L);
Input.GetKeyDown(KeyCode.L);
Input.GetKeyUp(KeyCode.L);

I’m using also KeyCode. It’s provided by unity so why not using it. also you can’t make typo mistakes

@Zuntatos
@martinmr
Thanks for your help! It works now. I already tried using KeyCode yesterday and it failed, too. Today, after changing, it work. This was kind of weird to me (because it wouldn’t work yesterday), so I changed it back to the version without KeyCodes and it works as well.
I don’t really know what this problem was about. Maybe the editor or my machine just needed a restart for whatever reason. You’re right about using KeyCodes, though. The non-KeyCode version sure is more prone to typos.

1 Like

Did you save before restarting? I’m facing a similar issue

It works now. I restarted the application.Thanks

i had the same issue… every thing was fine. infact i didn’t change anything. so i closed unity and re open. then it worked… :smile:

Yes, restarting Unity fixed the issue. Does UE4 have issues like this too?

Also, if you are running the game through the Device Simulator, the keyboard input doesn’t work there. So change to Game view

13 Likes