首页 > 其他 > 详细

_kbhit() for linux

时间:2014-02-21 08:11:24      阅读:262      评论:0      收藏:0      [点我收藏+]

传送门:http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html

bubuko.com,布布扣
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
 
int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;
 
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
 
  ch = getchar();
 
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);
 
  if(ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }
 
  return 0;
}
 
 
main( int argc, char** argv )
{
    char *input = argv[0];
    int nomor = argc;
    pid_t pid = 0;
    /* set stuff up */
    /* accept command line args */
     
    pid = fork();
    if( pid == 0 ){
        /* this is the "background" process.  Execute process loop here */
        int x=0;    
        while(1)
        {
            if(kbhit())
              printf("you hit keyboard");
        }
    }
    else {
        /* "foreground" process exits */
        exit(0);
    }
}
bubuko.com,布布扣

具体含义,有的解释是等待键盘输入;但此处用意为在 ios下使用while( !kbhit()) 控制openal的音效的循环播放.

_kbhit() for linux

原文:http://www.cnblogs.com/wainiwann/p/3558295.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!