C库函数从键盘检查按键preSS(在Linux) [英] C library function to check the keypress from keyboard( in linux )

查看:178
本文介绍了C库函数从键盘检查按键preSS(在Linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有C库函数来检查从键盘键preSS(我的工作Linux机器上)。

Is there any C library function to check the keypress from keyboard( I am working on linux machine ).

推荐答案

的getchar() 在头文件 stdio.h中返回从标准输入下一个字符。这可能是你要搜索的内容。

getchar() from the Header file stdio.h returns the next character from stdin. That's probably what you're searching for.

以下code将输出从标准输入流中的第一个字符:

The following code will output the first char from the stdin stream:

#include <stdio.h>
int main (int argc, char **argv){
  char c = getchar();
  printf("Char: %c", c);
  return 0;
}

有也可以做到不堵即 的kbhit(其他功能) 和残培()在 CONIO.H 。但是,头文件 CONIO.H 是非标准的,可能不是您的平台上,如果你使用的是Linux。

There are also other functions available to do this without blocking i.e. kbhit() and getch() in conio.h. But the header file conio.h is non-standard and probably not available on your platform if you are using linux.

所以,你有2个选择:

1)。使用库的ncurses 可以使用即功能的超时()定义的残培的超时()这样的功能:

1.) Using the library ncurses you can use i.e. the function timeout() to define an timeout for the getch() function like this:

initscr();
timeout(1000);
char c = getch();
endwin();
printf("Char: %c\n", c);

2)实施的kbhit()自己不使用ncurses的。有一个伟大的expanation 来这样做。你必须自己和循环到达您的超时,直到测量时间。为了测量C时间,这里有大量的线程上的计算器 - 你只需要寻找它。那么你的code是这样的:

2.) Implement kbhit() by yourself without using ncurses. There is a great expanation here to do so. You would have to measure time by yourself and looping until your timeout is reached. To measure time in C, there are plenty threads here on stackoverflow - you just have to search for it. Then your code would look like this:

while(pastTime() < YOUR_TIMING_CONSTRAINT){
  if (kbhit()){
    char c = fgetc(stdin);
    printf("Char: %c\n", c);
  }
}

这篇关于C库函数从键盘检查按键preSS(在Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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