用C语言实现键盘中断
节目:用C语言实现键盘中断,c,linux,unix,keyboard,signal-handling,C,Linux,Unix,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(); } } #包括 void main() { 整数时间=1800; 而(1){ 系统(“清除”);
#include<stdio.h>
void main()
{
int time=1800;
while(1){
system("clear");
time-=1;
printf("%d\n",time);
sleep(1);
if(time==0)
pause();
}
}
#包括
void main()
{
整数时间=1800;
而(1){
系统(“清除”);
时间-=1;
printf(“%d\n”,时间);
睡眠(1);
如果(时间==0)
暂停();
}
}
当时间达到0时,上述程序停止。我的要求是在程序运行期间,如果我按下空格键或其他任何键,程序将暂停,再次按下该键,程序将恢复。因此,在执行
当条件满足时,我们提交键盘中断的信号处理程序。在C语言中如何做到这一点
用于获取键盘中断的函数是什么。我不想从用户那里得到输入,我想处理用户通过键盘产生的中断
提前感谢。我的要求是在程序运行期间,如果我按空格键或任何其他键,程序将暂停,再次按该键,程序将恢复
您可以使用这种类型的代码来实现这一点
#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;
}
#包括
int main()
{
字符i;int y=0;
而(1)
{
如果(!(_kbhit()))
{
y=0;
printf(“//键未按下”);
//做你想做的
}
其他的
{
printf(“按下键(执行暂停),再次按下以继续”);
i=_getch();
y=1;
}
如果(y==1)
{
getch();
}
}
返回0;
}
纯标准C99或C11中不存在键盘(stdin不是一个键盘,可以是一个,所以不一定是一个;您可以从/dev/tty
..中读取)
所以你想要它是什么就不那么简单了,它是特定于操作系统的。我专注于Linux
阅读更多关于TTY的信息,尤其是阅读。注意,tty通常处于cooke模式,其中内核缓冲行(除了stdin缓冲行之外)
合理的方法是使用像or这样的终端库。这些库正在raw模式下设置tty(您可以自己进行设置,请参见和;您可能还需要这样做)。确保正确退出,并在退出前将tty重置为熟食模式
但是你的生命可能太短了,所以只需使用ncurses
或readline
<>你也可以考虑一些GUI应用程序(例如上面或)。然后使用工具箱(,…)。您需要conio.h来满足您的需求。它定义了kbhit()和getch()都等待键盘的输入 无论何时调用kbhit(),它都会检查键盘缓冲区,如果缓冲区有任何按键,则返回非零值,否则返回0 h由MSDOS编译器使用,不是标准C库(ISO)的一部分。POSIX中也没有定义它
#include<stdio.h>
#include<conio.h>
int main()
{
while(1)
{
while(!kbhit())
{
//works continuously until interrupted by keyboard input.
printf("M Tired. Break Me\n");
}
getch();
}
return 0;
}
#包括
#包括
int main()
{
而(1)
{
而(!kbhit())
{
//连续工作,直到被键盘输入中断。
printf(“我累了,让我崩溃”\n);
}
getch();
}
返回0;
}
对于linux,您可以使用以下代码段来实现kbhit(),方法是使用fnctl.h中的fnctl()进行信号处理:
#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;
}
#包括
#包括
#包括
int kbhit(无效)
{
结构termios oldt,NETT;
int-ch;
int oldf;
tcgetattr(标准文件号和旧文件号);
newt=oldt;
newt.c|lflag&=~(ICANON | ECHO);
tcsetattr(标准文件号、TCSANOW和NETT);
oldf=fcntl(标准文件号,F\u GETFL,0);
fcntl(标准文件号、固定格式、非块格式);
ch=getchar();
tcsetattr(标准文件号、TCSANOW和oldt);
fcntl(标准文件号、固定格式、旧格式);
如果(ch!=EOF)
{
ungetc(ch、stdin);
返回1;
}
返回0;
}
什么操作系统?使用sighandler\u t信号(int signum,sighandler\u t处理程序)@venki和vish4071请详述如何使用信号来获得键盘按键。我不知道有什么办法。信号可用于捕捉某些组合键,如ctrl-c。但问题似乎在于获得任意按键。在Linux上,您可以通过输入子系统接收按键。读一读。基本上,输入设备是打开的,例如与键盘设备相对应的/dev/input/event0
。然后可以从该设备读取事件。每次按键和释放都将生成一个event.mohan+@alanau:_kbhit(),_getch(),在其中声明,这意味着这些文件在哪些头文件中declared@mohan标题为conio.h
@ameyCU-conio.h仅在windows上可用。Iam使用Ubuntu12.04您可以将其发送到一个函数,该函数再次实现相同的中断处理,而不是在windows 10上运行getch(),谢谢!