Get keyboard interrupt in C

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 requirements are during the execution of the program. If I press any key, like space or any other key, the program pauses, and once again I press the key, the program gets resumed. Therefore, for this, before the condition is satisfied, we send a signal handler to interrupt the keyboard. In C, how to do it.

What is the function used to get keyboard interrupt. I do not want to receive data from the user, I want to handle the interrupt generated by the user from the keyboard.

Thank you Advance ..,

+1
source share
3 answers

The keyboard does not exist in the standard C99 or C11 (stdin is not a keyboard and may be pipe (7) , therefore it is not always tty (4) , you can read with /dev/tty ...).

So it’s much less simple that you want it to be, and it depends on the operating system. I am focused on Linux.

Read more about ttys, especially read tty demystified . Please note: tty is usually in cookie mode, where the kernel is buffering strings (in addition, stdin is a string buffer).

A smart way is to use a terminal library like ncurses or readline . These libraries install tty in raw mode (which you can do yourself, see this and termios (3) , you will also need poll (2) ). Be sure to exit correctly and reset tty to ready 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 may also consider some GUI application (e.g. above X11 or Wayland ). Then use the toolkit ( GTK , Qt , ...).

+2
source

My requirement is during the execution of the program. If I press any key, such as a space or any other key, the program pauses and once again I press the key, the program resumes.

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; } 
0
source

You need conio.h for your requirement. It determines that kbhit () and getch () are waiting for keyboard input.

When kbhit () is called, it checks the keyboard buffer and returns a nonzero value if the buffer has any keystroke, otherwise 0 is returned.

Conio.h is used by MSDOS compilers and is not part of the standard C (ISO) libraries. It is also not defined in 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; } 

For linux, you can use the following snippet to implement kbhit () using fnctl () from fnctl.h to process signals:

  #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; } 
0
source

Source: https://habr.com/ru/post/1239272/


All Articles