❔ Console.ReadKey() blocking
Hey there. I have this function that is called every frame:
It blocks until a key is pressed instead of setting KeyAvailable back to false...
I know this is being called every frame because I can insert a
Debug.Log(); return;
at the top and I will see it log every frame.
However if I put the Debug.Log()
after the ReadKey()
, it only gets called when you press keys.10 Replies
ReadKey is blocking
Also, is this Unity??
There is no console associated with Unity.
Do not use the Console class (for Unity)
It is a dedicated server console window
It is spun up with
This is Unity
Pastebin
ConsoleWindow.cs - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
ConsoleInput.cs - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
ConsoleInput.Update() is the function causing the problem
Well, tough sh*t.
ReadKey is blocking.
You can maybe use GetAsyncKeyState?
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate
But if you want an in-game console window, you can just set up Dear Imgui, or make your own.
And I don't recommend using P/Invoke to Windows API in Unity since that will cause issues with cross-platform.
Maybe you want to publish for Mac, and whoops. It does not have Windows API. There goes your console Window, and possibly crashes.
Unity also has an input system you can check out, no windows apis needed
Input System doesn't work because Unity is running in batchmode
I'll try GetAsyncKeyState
try raw input maybe
shouldnt unity have that?
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.