How to get data what I press the key in unity?
like a getch method in cpp
How to get data what I press the key in unity?
like a getch method in cpp
Input.inputString
is about the closest you can get to good old getc(3)
Not sure what the answer would be in the new InputSystem, so in that case check over those docs.
There is also Event.keyCode:
But this is through the legacy OnGUI Event system. This does not really rely on the Update loop (it can happen multiple times per frame). But this is how TextMeshPro does its input stuff.
If you need it for doing UI input, like TextMeshPro’s InputField, that’s how you may want to do it (you can view the source of TextMeshPro to see how it works… it’s not exactly a single function call though).
If you’re attempting to do gameplay through this though, it’s not really how Unity’s input system works.
…
Question, what is it you’re attempting to accomplish?
Yes, but even uGUI still uses this system partially. Since uGUI came out they have added the static method Event.PopEvent which is used internally by components like the UI.Input field. Though as the name of that method suggests, it actually pops the event from the event queue. So only a single consumer can process events through this method. So you have to be careful how to handle it. Using the OnGUI callback is usually the simplest solution.
That’s one of the annoying things with Unity. They added the PopEvent method but didn’t add a PushEvent. Having a PushEvent method would open up so many possibilities, especially when it comes to custom event mapping or testing scenarios.
The fact that OnGUI is called for multiple events shouldn’t really matter. Yes, it’s usually called twice each frame to handle the layout and repaint event, though the layout event can be disabled (with all the consequences). Since you usually filter the events by the event type, it’s quite easy to ignore events you’re not interested in.
Apart from Unity specific solutions, you can always use platform specific solutions. So when you’re building a windows standalone application, you could use a message hook to intercept window messages or use things like GetKeyState or GetKeyboardState. Though it depends on your exact usecase. Of course the OnGUI solution is much better for staying cross-platform.
Note I didn’t call it legacy as a suggestion you shouldn’t use it.
I called it legacy, because it’s a legacy system. And that clarification was to follow on that it does not rely on the Update loop directly. That’s the intent of the “but”.
Here’s how you would access the key code in the new input system:
using UnityEngine;
using UnityEngine.InputSystem;
public class KeyTest : MonoBehaviour
{
private void Update()
{
foreach (var key in Keyboard.current.allKeys)
{
if (key.wasPressedThisFrame)
{
Debug.Log($"Key code: {key.keyCode}");
}
}
}
}
Getch() works by pausing input until a character has been received. We can’t do that in Unity so we instead have to track if one has been received:
using UnityEngine;
using UnityEngine.InputSystem;
public class KeyTest : MonoBehaviour
{
private KeyCode lastKeyPressed;
private bool keyPressed;
public KeyCode LastKeyPressed
{
get
{
KeyCode tmp = lastKeyPressed;
lastKeyPressed = KeyCode.None;
keyPressed = false;
return tmp;
}
}
public bool KeyPressed
{
get
{
return keyPressed;
}
}
private void Update()
{
if (!keyPressed)
{
foreach (var key in Keyboard.current.allKeys)
{
if (key.wasPressedThisFrame)
{
lastKeyPressed = key.keyCode;
keyPressed = true;
break;
}
}
}
}
}
The second script was written with the help of GPT-4. I haven’t tested it but it should work.