加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何读取单个键盘字符(如getch)?

发布时间:2020-12-15 08:15:18 所属栏目:百科 来源:网络整理
导读:在C中,我可以使用getch()获取输入,而无需用户按Enter键. 例如 #include conio.hint main(){ char c; c = getch(); return 0;} 什么功能可以在C#中做同样的事情? (没有按下输入). 解决方法 你可以使用 Console.ReadKey() : Obtains the next character or f
在C中,我可以使用getch()获取输入,而无需用户按Enter键.
例如
#include <conio.h>

int main()
{
    char c;
    c = getch();
    return 0;
}

什么功能可以在C#中做同样的事情? (没有按下输入).

解决方法

你可以使用 Console.ReadKey()

Obtains the next character or function key pressed by the user.

它返回有关按下的键的信息.然后你可以使用KeyChar属性来获得按下键的Unicode字符:

int Main()
{
    char c = Console.ReadKey().KeyChar;
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读