C
C#2mo 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
mtreit
mtreit2mo 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._2mo ago
huh let me try that rq you my friend r a genius i have one question tho
Kapa._
Kapa._2mo ago
No description
Kapa._
Kapa._2mo ago
No description
Kapa._
Kapa._2mo 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
mtreit2mo 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._2mo ago
sweet stuff man ty so much
mtreit
mtreit2mo 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._2mo ago
you are brilliant, ty so much man
Denis
Denis2mo ago
$close
MODiX
MODiX2mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server
More Posts
C# with NeovimHey! I'm trying to set up neovim with C# on WSL. Using latest 9.5 nvim. I managed to get OmniSharp (Question to software development interviewers, is it ok to keep portfolio on localhost over prod?Spent a week migrating my websites to the cloud, frontend uploaded to Netlify. Currently delving intRedis or database for cache, distribiuted lock, signalR scale-out?I am evaluating whether to use Redis or alternative methods for caching, distributed locking, and scDiscord.net help - Trying to access a users rolesDoes anybody know a method I can use to access a users discord roles whilst I am logging into my appSeeking community input for project ideas to learn problem-solving and best practicesI recently completed 2 solo projects, a basic notes taking app and a basic e-commerce portal, withouSelecting an overloaded method based on the runtime type of an object by casting it to dynamicIs this something I should do ? Is it a common pattern ? Are there any downsides / unexpected behavi✅ cant assign a value to a variable```cs using System; using System.Collections.Generic; using System.Linq; using System.Reflection.PorEF Core Where on multiple fields## Initial question Hi! I'm currently running into an issue where I try and do something along the✅ Help with LINK Async !Hi ! I am having a trouble with this code: public async Task<IEnumerable<Customer>> GetAllAsync() {Serialization/DeserializationI have a Windows Forms C# application, where I have multiple classes(like Board, Context, Layout, Re