采用getchar获取终端输入,发现特殊键会有多个字符,以ESC开头,但字符数不固定。如:向上键是27,91,65三个字符,F5:27,91,49,53,126是5个字符。本来打算用取系统时间的方式来判断这些字符是不是一起的,但不同CPU、调度情况下,可能很不稳定,所以还是得用别的办法。用普通的read试试看,先把终端设置成不等待读入时间,如下:
- term.c_lflag &= ~ICANON;
-
- term.c_lflag &= (~ECHO);
-
- term.c_cc[VTIME] = 0;
-
- tcsetattr(STDIN_FILENO, TCSANOW, &term);
-
- for(;;)
-
- {
-
- nRead=read(STDIN_FILENO, szCmdStr, 16);
-
- if (nRead != 0)
-
- {
-
- for (tmp = 0; tmp < nRead; tmp ++)
-
- {
-
- printf("Key %d index%d
", szCmdStr[tmp], tmp);
-
- if (szCmdStr[tmp] == "q")
-
- break;
-
- }
-
- if (szCmdStr[tmp] == "q")
-
- break;
-
- }
-
- }
输出结果如下:Key 10 index 0 《回车Key 27 index 0 《向上键,3个字符Key 91 index 1Key 65 index 2Key 27 index 0 《F12,5个字符Key 91 index 1Key 50 index 2Key 52 index 3Key 126 index 4Key 27 index 0 《F5,5个字符Key 91 index 1Key 49 index 2Key 53 index 3Key 126 index 4Key 113 index 0 《qYeah,搞定了!Fedora 15 安装 VMware 7.1.4 提示 需要Kernel Headers 简单解决方法Mac OS X Lion下安装Titanium开发Mobile应用相关资讯 Linux教程
- Linux教程:如何在命令行中查看目 (07/28/2014 12:22:23)
- Linux 修改root密码 (11/03/2012 07:53:38)
- su - root 与su root的区别 (06/06/2012 00:39:40)
| - Linux进程间通信:消息队列 (01/28/2013 09:43:00)
- U盘安装Linux开机无法启动解决方法 (10/07/2012 08:55:52)
- Windows 7/Linux 同步时间 (05/15/2012 06:17:55)
|
本文评论 查看全部评论 (0)