Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to read the pressed key, without displaying it in console, and then decide (switch/case) what to do depending on the Key.

I have tried console.readkey(true); but it goes to the unexpected case.

Any ideas?


In my game I have a switch/case, (my cases are 1, 2 and 3), and I want the user to choose either 1,2 or 3 and then, depending on that, the game should do something.

I don't want the pressed key to be displayed.
Eg. When the user presses 1, the game should be started but it doesn't!
It returns like it ignores case 1 and jumps to default.
Posted
Updated 19-Aug-11 2:52am
v3
Comments
Manfred Rudolf Bihy 19-Aug-11 8:38am    
What "unexpected case" are you talking about?
firstfox 19-Aug-11 8:45am    
in my game i have a switch/case...my cases are 1, 2 and 3...
i want the user to choose either 1,2 or 3 and then,depending on that the game should do sth....
but i dont want the pressed key to be displayed...
for example the user presses 1,the game should be started but it doesnt! it returns...like it ignores case 1.. and jumps to default
Dalek Dave 19-Aug-11 8:52am    
Edited for Grammar, Syntax and Readability.

Try System.Console.ReadKey(true);
 
Share this answer
 
v2
Comments
firstfox 19-Aug-11 8:42am    
sorry..i made a mistake in typing..i have tried console.readkey(true);
BoxyBrown 19-Aug-11 8:45am    
and what you mean by "but it goes to the unexpected way"?
firstfox 19-Aug-11 8:48am    
in my game i have a switch/case...my cases are 1, 2 and 3... i want the user to choose either 1,2 or 3 and then,depending on that the game should do sth.... but i dont want the pressed key to be displayed... for example the user presses 1,the game should be started but it doesnt! it returns...like it ignores case 1.. and jumps to default
Sergey Alexandrovich Kryukov 20-Aug-11 0:38am    
This is correct, only I fixed case and added namespace. My 5.
--SA
Abhinav S 21-Aug-11 12:04pm    
Thank you.
At this point you need to set a breakpoint in your code on the line directly below your Console.ReadKey(true) call and then run your code in a debug session. Check the value of your key variable in the debugger and step through the code to see what is happening. Perhaps the value of key is not what you expect. If you are checking for key == 1 then you may have an issue as key will likely hold a keycode and not the display value of 1.
 
Share this answer
 
As Abhinav already pointed out, you should use Console.ReadKey(true), see the documentation[^] for a code sample.
 
Share this answer
 
I thought about invisible typing password and create this example. It works well. So 1 and 2 solutions are good answers for your question.

C#
Console.WriteLine("Enter your password");
StringBuilder password = new StringBuilder();
ConsoleKeyInfo keyInfo =  Console.ReadKey(true);
while(keyInfo.Key != ConsoleKey.Enter)
{
    password.Append(keyInfo.KeyChar);

    keyInfo = Console.ReadKey(true);
}
Console.WriteLine("Your password:'{0}'", password);
 
Share this answer
 
Comments
BoxyBrown 19-Aug-11 8:54am    
I think error is in other logic.
firstfox 19-Aug-11 9:02am    
if i declare ConsoleKeyInfo cki = Console.ReadKey(true);
i cannot have the cki in my switch expression becuz its a consolekeyinfo
BoxyBrown 19-Aug-11 9:03am    
int input = Convert.ToInt32(key.KeyChar.ToString());

switch (input)
{
case 1:
Console.WriteLine("1");
break;
case 2:
Console.WriteLine("2");
break;
case 3:
Console.WriteLine("3");
break;
default:
Console.WriteLine("default");
break;
}

try this one. But I better work around Convert.ToInt32(key.KeyChar.ToString());
BoxyBrown 19-Aug-11 9:04am    
if i remove ToString(), i've got your situation.
firstfox 19-Aug-11 9:12am    
what is "Convert.ToInt32(key.KeyChar) " ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900