在 C 中获取键盘中断

mrg*_*mrg 5 c unix linux keyboard signal-handling

程序:

#include<stdio.h>
void main()
{
    int time=1800;
    while(1){
        system("clear");
        time-=1;
        printf("%d\n",time);
        sleep(1);
    if(time==0)
        pause();
    }
}
Run Code Online (Sandbox Code Playgroud)

上述程序在时间达到 0 时停止。我的要求是在程序运行期间,如果我按下空格键或任何其他键等任何键,程序将暂停,再次按下该键,程序将恢复。因此,为此,在执行 while 条件之前,我们提交键盘中断的信号处理程序。在C中如何做到这一点。

获取键盘中断的函数是什么。我不想从用户那里得到输入,我想通过键盘处理用户产生的中断。

提前致谢..,

udi*_*043 1

我的要求是在程序运行时,如果我按任何键(例如空格键或任何其他键),程序就会暂停,再次按下该键,程序就会恢复。

您可以使用此类代码来实现此目的

#include <stdio.h>
int main()
{
  char i;int y=0;
  while(1)
  {
     if(!(_kbhit()))
     {
       y=0;
       printf("//key is not pressed");
       //do what you want
     }
     else
     {
       printf("key pressed (execution pause) , press again to resume");
       i=_getch();
       y=1;
     }
     if(y==1)
     {
       getch();
     }
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)