❔ Console.ReadKey() blocking

Hey there. I have this function that is called every frame:
public void Update()
{
if ( !Console.KeyAvailable ) return;

var key = Console.ReadKey(intercept: true);
}
public void Update()
{
if ( !Console.KeyAvailable ) return;

var key = Console.ReadKey(intercept: true);
}
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
Buddy
Buddy2y ago
ReadKey is blocking Also, is this Unity?? There is no console associated with Unity. Do not use the Console class (for Unity)
aaron.frost
aaron.frost2y ago
It is a dedicated server console window It is spun up with
[DllImport( "kernel32.dll", SetLastError = true )]
static extern bool AllocConsole();
[DllImport( "kernel32.dll", SetLastError = true )]
static extern bool AllocConsole();
This is Unity
aaron.frost
aaron.frost2y ago
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.
aaron.frost
aaron.frost2y ago
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.
aaron.frost
aaron.frost2y ago
ConsoleInput.Update() is the function causing the problem
Buddy
Buddy2y ago
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.
arion
arion2y ago
Unity also has an input system you can check out, no windows apis needed
aaron.frost
aaron.frost2y ago
Input System doesn't work because Unity is running in batchmode I'll try GetAsyncKeyState
Jester
Jester2y ago
try raw input maybe shouldnt unity have that?
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server
More Posts
❔ Trouble Creating PowerShell CommandletI'm using visual studio 2022, dotnet 6.0. I installed the automation package using > NuGet\Install-P✅ Passing data from controller to viewHi guys hoping for some help here. I have an eCommerce wep app using ASP.NET MVC (7.0) and I want tHow to implement that when you click on the button the data that was sent with it will be displayedHow to implement that when you click on the button the data that was sent with it will be displayed❔ Visual Studio: How to hide tptrace output from Immediate Window during Test Explorer executions?Title says it all. Is there a way to hide the output messages from tptrace that get written to the I❔ Name "PostsService" does not exist in the current context```cs @using BlazorBookGroup.Data . . . private PostsService postsService = new(); protecte❔ System.AccessViolationException: 'Attempted to read or write protected memory.Even with <AllowUnsafeCodeBlocks> enabled and using unsafe { }, this error does not go away. And bef❔ namespace 'Models' does not exist in the namespace 'BlazorBookGroup'It is a subfolder though so what's the issue?❔ .Net Core API User.FindFirstValue(ClaimTypes.NameIdentifier) in every action?!Hi everyone I have the end point below ``` [HttpPost, Route("stores/new"), Authorize(policy: "client❔ Is it possible to change a project sdk from Microsoft.NET.Sdk to Microft.net.sdk.net?I'm completely new to c# and .NET so apologies if this doesn't make sense. If a project csproj file❔ Razor pages redirectHow do you make a form redirect to a page, and pass a value to it? My entire application is routed