C
C#3w ago
dragon

✅ Is it possible to ask check for a keypress without pausing the whole console?

Kinda new to c# so i dont rlly know alot, but i started a project and i literally have no clue how to ask for a keypress w out pausing and waiting for one, ive seen examples on stack like
Task.Factory.StartNew(() => Console.ReadKey(true)).Wait(TimeSpan.FromSeconds(0.2)); but i cant assign that to var key to use as controls etc, if anyone wants to check through my code more i can send screenshtos or try open a git
50 Replies
Kouhai
Kouhai3w ago
Check Console.KeyAvailable before calling Console.ReadKey
dragon
dragon3w ago
uhh lemme try that kinda worked the code runs smoothly but i dont think it takes input ima go check it out some more
Kouhai
Kouhai3w ago
It gets input Console.KeyAvailable returns true if there's data in stdin, and Console.ReadKey will read a key from stdin
dragon
dragon3w ago
No description
dragon
dragon3w ago
like this?
Kouhai
Kouhai3w ago
Yeah
dragon
dragon3w ago
um
Kouhai
Kouhai3w ago
Also
dragon
dragon3w ago
it doesnt change the colour yeah?
Kouhai
Kouhai3w ago
You don't need if (Console.KeyAvailable == true) you can just do if (Console.KeyAvailable)
dragon
dragon3w ago
fair point hold on im gonna go use breakpoints and see if that is ever true brb its always false im trying to make mspaint in console atm bc im bored at school and had no idea what to do
Kouhai
Kouhai3w ago
Show your full code please 😅
dragon
dragon3w ago
oh sure
Kouhai
Kouhai3w ago
If it's too long $paste
MODiX
MODiX3w ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
dragon
dragon3w ago
BlazeBin - btmppmzbjait
A tool for sharing your source code with the world!
dragon
dragon3w ago
i took some stuff from stack overflow, like to know where cursor is etc but i hard coded parts so you might need to change the numbers for ur screen its prolly insanely inefficient but this is js a first draft for school atm also thank you sm kouhai ALSO I LITERALYL STARTED MAKING IT TODAY SO NOT MUCH HAS BEEN MADE i think my code killed them
Kouhai
Kouhai3w ago
The problem is most likely with ColourList
dragon
dragon3w ago
oh i see can you explain why?
Kouhai
Kouhai3w ago
Well
for (int i = 0; i < Colours.Length; i++) // Iterate from 0 to Colours.Length which is 16, meaning the last value of i will be 15
{
if (i % 2 == 0)
{
Console.SetCursorPosition(2, (Console.WindowHeight / 2 - Colours.Length / 2) + i);
}
else { Console.SetCursorPosition(4, (Console.WindowHeight / 2 - Colours.Length / 2) + i); }
Console.ForegroundColor = (ConsoleColor)(i); // Set the foreground color to i, which means the last color would be white
Console.WriteLine("█");

}
for (int i = 0; i < Colours.Length; i++) // Iterate from 0 to Colours.Length which is 16, meaning the last value of i will be 15
{
if (i % 2 == 0)
{
Console.SetCursorPosition(2, (Console.WindowHeight / 2 - Colours.Length / 2) + i);
}
else { Console.SetCursorPosition(4, (Console.WindowHeight / 2 - Colours.Length / 2) + i); }
Console.ForegroundColor = (ConsoleColor)(i); // Set the foreground color to i, which means the last color would be white
Console.WriteLine("█");

}
dragon
dragon3w ago
yeah um i have it so you can go back and forth tho
Kouhai
Kouhai3w ago
No, what you have is literally what I copy pasted
dragon
dragon3w ago
OH WAIT I SE
Kouhai
Kouhai3w ago
void ColourList()
{

for (int i = 0; i < Colours.Length; i++)
{
if (i % 2 == 0)
{
Console.SetCursorPosition(2, (Console.WindowHeight / 2 - Colours.Length / 2) + i);
}
else { Console.SetCursorPosition(4, (Console.WindowHeight / 2 - Colours.Length / 2) + i); }
Console.ForegroundColor = (ConsoleColor)(i);
Console.WriteLine("█");

}
}
void ColourList()
{

for (int i = 0; i < Colours.Length; i++)
{
if (i % 2 == 0)
{
Console.SetCursorPosition(2, (Console.WindowHeight / 2 - Colours.Length / 2) + i);
}
else { Console.SetCursorPosition(4, (Console.WindowHeight / 2 - Colours.Length / 2) + i); }
Console.ForegroundColor = (ConsoleColor)(i);
Console.WriteLine("█");

}
}
dragon
dragon3w ago
I REPEATED MYSELF IN THE CODE THANK YOU SM WAIT NO im dumb this is for a seperate thing uhh hold on
Kouhai
Kouhai3w ago
Yes, the problem is with it
dragon
dragon3w ago
No description
dragon
dragon3w ago
it prints out this
Kouhai
Kouhai3w ago
You're setting ForegroundColor to i to display the colors, but ForegroundColor is not being reset to the previous color
dragon
dragon3w ago
colourlist does that
Kouhai
Kouhai3w ago
Yes, and that's the problem
dragon
dragon3w ago
oh Oh i se
Kouhai
Kouhai3w ago
Let's say before exeuting the method that ForegroundColor is DarkRed We enter the function
dragon
dragon3w ago
its set to white after running
Kouhai
Kouhai3w ago
ForegroundColor is getting set to i
dragon
dragon3w ago
and its not set back to dark red
Kouhai
Kouhai3w ago
Yup
dragon
dragon3w ago
so when console prints other things hold on ah okay ill go figure how to get it to stay thank you so so much kouhai im a bit dum at times
Kouhai
Kouhai3w ago
It's all good
dragon
dragon3w ago
ill be back after running some tests ig
Kouhai
Kouhai3w ago
Also, your program will crash here Console.SetCursorPosition(defPnt.X * Console.WindowWidth / 1919, defPnt.Y * Console.WindowHeight / 1079); Because sometimes the cursor will be outside the console screen
dragon
dragon3w ago
yeah it only crashes if i hover over x like the red close tab otherwise it runs fine so it doesnt rlly matter for rn
Kouhai
Kouhai3w ago
Because it's outside the console screen 😜
dragon
dragon3w ago
yeah lol but if ur goin on X your shutting the program down anyways
Kouhai
Kouhai3w ago
Well...a program shouldn't crash because the user is hovering over X
dragon
dragon3w ago
ill fix it after i get everything else working thats like my lowest priority problem rn 😭 OKAY IT WORKS
dragon
dragon3w ago
No description
dragon
dragon3w ago
drew a frog w it
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3w ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server