C
C#3mo 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
Kouhai3mo ago
Check Console.KeyAvailable before calling Console.ReadKey
dragon
dragonOP3mo 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
Kouhai3mo ago
It gets input Console.KeyAvailable returns true if there's data in stdin, and Console.ReadKey will read a key from stdin
dragon
dragonOP3mo ago
No description
dragon
dragonOP3mo ago
like this?
Kouhai
Kouhai3mo ago
Yeah
dragon
dragonOP3mo ago
um
Kouhai
Kouhai3mo ago
Also
dragon
dragonOP3mo ago
it doesnt change the colour yeah?
Kouhai
Kouhai3mo ago
You don't need if (Console.KeyAvailable == true) you can just do if (Console.KeyAvailable)
dragon
dragonOP3mo 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
Kouhai3mo ago
Show your full code please 😅
dragon
dragonOP3mo ago
oh sure
Kouhai
Kouhai3mo ago
If it's too long $paste
MODiX
MODiX3mo 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
dragonOP3mo ago
BlazeBin - btmppmzbjait
A tool for sharing your source code with the world!
dragon
dragonOP3mo 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
Kouhai3mo ago
The problem is most likely with ColourList
dragon
dragonOP3mo ago
oh i see can you explain why?
Kouhai
Kouhai3mo 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
dragonOP3mo ago
yeah um i have it so you can go back and forth tho
Kouhai
Kouhai3mo ago
No, what you have is literally what I copy pasted
dragon
dragonOP3mo ago
OH WAIT I SE
Kouhai
Kouhai3mo 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
dragonOP3mo ago
I REPEATED MYSELF IN THE CODE THANK YOU SM WAIT NO im dumb this is for a seperate thing uhh hold on
Kouhai
Kouhai3mo ago
Yes, the problem is with it
dragon
dragonOP3mo ago
No description
dragon
dragonOP3mo ago
it prints out this
Kouhai
Kouhai3mo ago
You're setting ForegroundColor to i to display the colors, but ForegroundColor is not being reset to the previous color
dragon
dragonOP3mo ago
colourlist does that
Kouhai
Kouhai3mo ago
Yes, and that's the problem
dragon
dragonOP3mo ago
oh Oh i se
Kouhai
Kouhai3mo ago
Let's say before exeuting the method that ForegroundColor is DarkRed We enter the function
dragon
dragonOP3mo ago
its set to white after running
Kouhai
Kouhai3mo ago
ForegroundColor is getting set to i
dragon
dragonOP3mo ago
and its not set back to dark red
Kouhai
Kouhai3mo ago
Yup
dragon
dragonOP3mo 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
Kouhai3mo ago
It's all good
dragon
dragonOP3mo ago
ill be back after running some tests ig
Kouhai
Kouhai3mo 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
dragonOP3mo 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
Kouhai3mo ago
Because it's outside the console screen 😜
dragon
dragonOP3mo ago
yeah lol but if ur goin on X your shutting the program down anyways
Kouhai
Kouhai3mo ago
Well...a program shouldn't crash because the user is hovering over X
dragon
dragonOP3mo ago
ill fix it after i get everything else working thats like my lowest priority problem rn 😭 OKAY IT WORKS
dragon
dragonOP3mo ago
No description
dragon
dragonOP3mo ago
drew a frog w it
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3mo 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