C
C#•3mo 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•3mo ago
mtreit
mtreit•3mo 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._•3mo ago
huh let me try that rq you my friend r a genius i have one question tho
Kapa._
Kapa._•3mo ago
No description
Kapa._
Kapa._•3mo ago
No description
Kapa._
Kapa._•3mo 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•3mo 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._•3mo ago
sweet stuff man ty so much
mtreit
mtreit•3mo 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._•3mo ago
you are brilliant, ty so much man
Denis
Denis•3mo ago
$close
MODiX
MODiX•3mo 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