- Home /
Check if there is a keyboard input (not a specified key input)
Since Input.anyKey indicates mouse input as well, and Input.inputString only contains visiable characters, I can't find out the way to check whether there is a keyboard input correctly except for checking KeyCode one by one.
So what's the correct way to do so?
Answer by LeeroyLin · Jun 22, 2018 at 02:08 PM
// Press A
if(Input.GetKeyDown(KeyCode.A)){
}
// Press A always
if(Input.GetKey(KeyCode.A)){
}
// Don't press A anymore
if(Input.GetKeyUp(KeyCode.A)){
}
This is what "checking $$anonymous$$eyCode one by one"... And I have to test hundreds of keys just like that... So there isn't any better idea?
O$$anonymous$$, I know what you mean. Use this.
public void OnGUI()
{
if (Input.any$$anonymous$$eyDown)
{
Event e = Event.current;
if (e.is$$anonymous$$ey)
{
Debug.Log("Current $$anonymous$$ey is : " + e.keyCode.ToString());
}
}
}
Tested and yes that's what I want! Thanks a lot.
Answer by DevJeeth · Jul 30, 2019 at 06:31 AM
This isn't what was asked for and this is exactly what I am looking out for too. Any key pressed on the keyboard should be detected. Input.anyKey includes mouse click unfortunately.
LeeroyLin just specifies the keycode here
I have worked on a work around, looked into the docs https://docs.unity3d.com/ScriptReference/$$anonymous$$eyCode.html
if(!Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)
&& !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse1)
&& !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse2)
&& !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse3)
&& !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse4)
&& !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse5)
&& !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse6))
{
if (Input.any$$anonymous$$eyDown)
{
Debug.Log("<color=red> $$anonymous$$ey board key pressed</color>");
}
}
Hope it helps somebody, if anyone got a better way to do this please do update this
I tried to use the anonymous thing, but it didn't work, and the link you psoted is broken. I suspect that Unity has since deprecated this functionality.
See my earlier reply to you under the other answer. This page has got corrupted, ignore all the anonymous nonsense - they're all just supposed to be 'K'!
it seems that the comments are lost, which is exactly the answer. look at this: https://docs.unity3d.com/ScriptReference/Event.html And then I shall get Event.current.keyCode in OnGUI, and do whatever I would.
Event has an is$$anonymous$$ey property, not used it myself but it sounds like it might help