Get Keyboard Interrupt in C

15,735

Solution 1

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 ...).

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

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).

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.

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

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

Solution 2

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.

You can achieve this with this type of code

#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;
}
Share:
15,735
mohangraj
Author by

mohangraj

Updated on June 15, 2022

Comments

  • mohangraj
    mohangraj almost 2 years

    Program:

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

    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.

    Thanks in Advance..,

  • mohangraj
    mohangraj over 8 years
    _kbhit(), _getch() where it is declared which means In which header files these are declared
  • ameyCU
    ameyCU over 8 years
    @mohan header is conio.h
  • mohangraj
    mohangraj over 8 years
    @ameyCU conio.h is available only on windows. Iam using Ubuntu 12.04
  • vish4071
    vish4071 over 8 years
    You can send it to a function that again implements the same interrupt handling, instead of getch()
  • ameyCU
    ameyCU over 8 years
    @mohan Use curses then . See this question .Will be useful in this case -stackoverflow.com/questions/3276546/…
  • udit043
    udit043 almost 3 years
    @ChristianidisVasileios nice :)