Hi guys, I'm new to this forum and to C programming as well. I need some guidance from you seniors to help me implement my concept in C programming. What I'm trying to do is using C programming to read which key on the keyboard is pressed and then to check whether the key is (lets just say) "r" or not. If it is "r", the program will proceed with stopping my robot's process. So what I have now is listed below. Please give me further guidance. Thanks in advance
Code:
#include <stdio.h>
#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 main(void)
{
while(!kbhit())
printf("You pressed '%c'!\n", getchar());
return 0;
}