C#在不显示键的情况下读取键 [英] c# reading a key without displaying it

查看:92
本文介绍了C#在不显示键的情况下读取键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取所按下的键,而不在控制台中显示它,然后根据键决定(切换/大小写)要执行的操作.

我已经尝试过console.readkey(true);,但遇到了意外情况.

有什么想法吗?


在我的游戏中,我有一个开关/保护套(我的保护套是1、2和3),我希望用户选择1,2或3,然后根据该游戏做点什么.

我不希望显示所按下的键.
例如.当用户按下1时,应该启动游戏,但是不会!
它会像忽略情况1并返回默认值一样返回.

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.

推荐答案

尝试System.Console.ReadKey(true);


此时,您需要在Console.ReadKey(true)调用正下方的代码行中,在代码中设置一个断点,然后在调试会话中运行代码.在调试器中检查键变量的值,并逐步执行代码以查看发生了什么.密钥的价值也许不是您所期望的.如果要检查key == 1,则可能有问题,因为key可能会保留一个keycode,而不是显示值1.
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.


As Abhinav 已经指出,您应该使用Console.ReadKey(true),请参见文档 [
As Abhinav already pointed out, you should use Console.ReadKey(true), see the documentation[^] for a code sample.


这篇关于C#在不显示键的情况下读取键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆