C
C#•11mo ago
Kapa._

Press enter only

I want to code a loop which only takes spacebar for the code to loop. E.g. While (true) { Console.ReadKey(" ") loop... } However, this isint working.
12 Replies
maxmahem
maxmahem•11mo ago
mtreit
mtreit•11mo ago
That code won't compile, did you look at the documentation for ReadKey? You want something like:
while (true)
{
var key = Console.ReadKey();
if (key.KeyChar == ' ')
{
Console.WriteLine("😎");
}
}
while (true)
{
var key = Console.ReadKey();
if (key.KeyChar == ' ')
{
Console.WriteLine("😎");
}
}
Kapa._
Kapa._OP•11mo ago
huh let me try that rq you my friend r a genius i have one question tho
Kapa._
Kapa._OP•11mo ago
No description
Kapa._
Kapa._OP•11mo ago
No description
Kapa._
Kapa._OP•11mo ago
the code now inputs a space before every 'dice' and it wasnt doing that before anything i did? @mtreit or what could i change if i wanted it to be enter instead nevermind im being stupid about the spaceing before the dice lol
mtreit
mtreit•11mo ago
If you want to check for the Enter key being pressed you can do:
while (true)
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine("😎");
}
}
while (true)
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine("😎");
}
}
Kapa._
Kapa._OP•11mo ago
sweet stuff man ty so much
mtreit
mtreit•11mo ago
@Kapa._ BTW you might want to pass true to Console.ReadKey. That will prevent the keypresses from being printed to the screen.
Kapa._
Kapa._OP•11mo ago
you are brilliant, ty so much man
Denis
Denis•11mo ago
$close
MODiX
MODiX•11mo ago
Use the /close command to mark a forum thread as answered

Did you find this page helpful?