免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 8594 | 回复: 11
打印 上一主题 下一主题

获取按键的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-06 17:02 |只看该作者 |倒序浏览
想获取键盘上的任何输入,但是发现当按功能键(F1-F12,方向键等),获取的代码都不是单个asiic码,比如说方向键向上,[27][91][65],有什么比较好的办法判断是按了什么键呢?
要求不用curse,纯C
先谢了!

论坛徽章:
0
2 [报告]
发表于 2007-02-06 17:13 |只看该作者
发现这是什么值了,原来就是控制码,[27][91][65] = \27[A

但是怎么才能知道终端接受数据结束呢?
发现while((buf = getchar()) != EOF) 条件一直为真

PS
我用cygwin的gcc编译

论坛徽章:
0
3 [报告]
发表于 2007-02-06 17:23 |只看该作者
用gets()应该就可以了..
不过这个是阻塞方式的..

  1. #include <sys/ioctl.h>
  2. #include <stdio.h>
  3. int main() {
  4.         int kbhit() {
  5.                 int n;
  6.                 ioctl(0,FIONREAD,&n);
  7.                 return n;
  8.         }
  9.         char keys[10];
  10.         while (1) {
  11.                 sleep(1);
  12.                 if(kbhit()){
  13.                         printf("key is %d\n",kbhit());
  14.                         gets(keys);
  15.                         printf("key value is %s\n",key);
  16.                 }
  17.         }

  18. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2007-02-07 12:36 |只看该作者
这个好像。。。。

那有没有办法判断终端接受到几个字符呢?

另外, system ("stty -F /dev/tty cbreak"); 在我的电脑上起作用,但是另外一台上却没有作用,在那台电脑上直接stty -F /dev/tty cbreak是可以的,但是在程序里面却实现不了,这是为什么?

论坛徽章:
0
5 [报告]
发表于 2007-02-15 09:19 |只看该作者
搜寻了几天,终于搜到了直接终端获取按键的方法,根据自己的需要稍微修改了一下
现在贴上,算是个好的结束


  1. /*
  2. * getch() -- a blocking single character input from stdin
  3. *
  4. * Returns a character, or -1 if an input error occurs
  5. *
  6. * Conditionals allow compiling with or without echoing of the input
  7. * characters, and with or without flushing pre-existing buffered input
  8. * before blocking.
  9. */
  10. char _get_key( char echo, char flush )
  11. {
  12.     struct termios old_termios, new_termios;
  13.     int OPTIONAL_ACTIONS;
  14.     int            error, keylen;
  15.     char           cbuf[10];
  16.    
  17.     fflush( stdout );
  18.     tcgetattr( 0, &old_termios );
  19.     new_termios              = old_termios;
  20.     /*
  21.      * raw mode, line settings
  22.      */
  23.     new_termios.c_lflag     &= ~ICANON;
  24.     if(echo)
  25.     /*
  26.      * enable echoing the char as it is typed
  27.      */
  28.     new_termios.c_lflag     |=  ECHO;
  29.     else
  30.     /*
  31.      * disable echoing the char as it is typed
  32.      */
  33.     new_termios.c_lflag     &= ~ECHO;
  34.     if(flush)
  35.     /*
  36.      * use this to flush the input buffer before blocking for new input
  37.      */
  38.     OPTIONAL_ACTIONS = TCSAFLUSH;
  39.     else
  40.     /*
  41.      * use this to return a char from the current input buffer, or block
  42.      * if no input is waiting
  43.      */
  44.     OPTIONAL_ACTIONS = TCSANOW;
  45.     /*
  46.      * minimum chars to wait for
  47.      */
  48.     new_termios.c_cc[VMIN]   = 1;
  49.     /*
  50.      * minimum wait time, 1 * 0.10s
  51.      */
  52.     new_termios.c_cc[VTIME]  = 1;
  53.     error                    = tcsetattr( 0, OPTIONAL_ACTIONS, &new_termios );
  54.      _init_array(cbuf,sizeof(cbuf));
  55.     if ( 0 == error )
  56.     {
  57.         /*
  58.          * get char from stdin
  59.          */
  60.         keylen  = read( 0, &cbuf, sizeof(cbuf) );
  61.     }
  62.     /*
  63.      * restore old settings
  64.      */
  65.     error                   += tcsetattr( 0, OPTIONAL_ACTIONS, &old_termios );
  66.     if(error == 0)
  67.     {
  68.         if(keylen == 1) return cbuf[0];
  69.         return(_analysis_kcode(cbuf+1)); // analysis function key
  70.     }
  71.     else
  72.     {
  73.         return( -1);
  74.     }
  75. }  /* end of getch */
复制代码

论坛徽章:
0
6 [报告]
发表于 2007-02-15 09:41 |只看该作者
看起来不错的样子
但是有好多不认识的函数啊

论坛徽章:
0
7 [报告]
发表于 2008-08-14 15:52 |只看该作者
谢谢楼主,我也正为这问题烦恼呢,谢谢您的答案,我测试一下看是否可行。

论坛徽章:
0
8 [报告]
发表于 2008-08-14 15:56 |只看该作者
每个按键都有自己的代码,[27][91][65] = \27[A]
去试出每个按键终端接受数据.

论坛徽章:
0
9 [报告]
发表于 2008-08-14 16:53 |只看该作者
jemy.zhang 兄弟,能否解说一下,假如是功能键或者方向键是怎么去 分析 所得到的 一系列的 值,例如 向右方向键的 数值 是: 27 91 68 ,你那个 _analysis_kcode函数大致的算法是怎样的?谢谢!!

论坛徽章:
0
10 [报告]
发表于 2008-08-14 16:57 |只看该作者
To qliu00 :
"每个按键都有自己的代码,[27][91][65] = \27[A]"这句话不是很理解,例如 向上的方向键是 [27][91][65] ,是否则取得第三个值:65就可以了?谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP