获取用C键盘中断 [英] Get Keyboard Interrupt in C

查看:381
本文介绍了获取用C键盘中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序:

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

当时间达到0.我的要求是该程序的运行过程中,如果我preSS像空格键或任何其他任何键上面的程序停止,该程序被暂停,并再次我preSS该键,程序得到恢复。所以这样做,在执行前
而条件下,我们提交键盘中断信号处理程序。在C语言中如何做到这一点。

The above program stops when the time reaches 0. My requirement is during the runtime of the program, If I press any key like spacebar or any other key, the program gets paused and once again I press the key, the program gets resumed. So for doing this, before execution of while condition, we submit the signal handler for keyboard interrupt. In C how to do this.

什么是用于获取键盘中断功能。我不想从用户获取输入,我想要处理通过键盘用户产生的中断。

What is the function used to get keyboard interrupt. I dont want to get input from the user, I want to handle the interrupt generated by the user through keyboard.

在此先感谢..,

推荐答案

键盘不纯粹标准的C99或C11(标准输入的不是键盘,并可能是一个的管(7)所以并不总是一个的 TTY(4),你可能会从的/ dev / tty的 ...)。

The keyboard does not exist in purely standard C99 or C11 (stdin is not a keyboard, and could be a pipe(7) so is not always a tty(4); you might read from /dev/tty ...).

因此​​,它是简单的要少得多,你想要的它是,它是操作系统特定。我专注于Linux操作系统。

So it is much less simple that what you want it to be, and it is operating system specific. I am focusing on Linux.

了解更多关于ttys中,尤其是阅读 TTY揭秘。请注意,TTY通常是煮熟的模式,其中的内核的是缓冲线(除中的标准输入的正行缓冲)。

Read much more about ttys, notably read the tty demystified. Notice that tty are usually in cooked mode, where the kernel is buffering lines (in addition of stdin being line buffered).

合理的方法是使用像 ncurses的或的 readline的。这些库设置在原始模式的TTY(你可以自己做,请参见并的的termios(3);你可能也需要的民意调查(2))。一定要正确退出,并退出前复位TTY熟模式。

The reasonable way is to use a terminal library like ncurses or readline. These libraries are setting the tty in raw mode (which you might do by yourself, see this and termios(3); you'll probably need also to poll(2)). Be sure to exit properly and reset the tty to cooked mode before exiting.

但你的生活可能是太短潜入的termios(3) tty_ioctl(4)所以只需要使用的ncurses 的readline

But your life is probably too short to dive into termios(3) and tty_ioctl(4) so just use ncurses or readline

您也可以考虑一些GUI应用程序(如上面的 X11 或的韦兰)。然后使用工具包( GTK ,的 Qt的,...)。

You could also consider some GUI application (e.g. above X11 or Wayland). Then use a toolkit (GTK, Qt, ...).

这篇关于获取用C键盘中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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