ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a loop which updates display but now I want that when user presses a key 'q' then it goes to some other function etc. I can't use getch() cause it will top the loop and waits for user key, is there any other way in linux C that I can detect a key and then check its value?
I'm going to answer slightly more than you ask, but the following will give you all you need to know in this area, without using curses. You'll be particularly interested in hurltty; its first read blocks, but its second read does not.
anykey.c
Code:
/*
* This program reads a single key, and then exits without echoing it.
*/
#include <stdio.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
int main(void)
{
int count;
int jndex;
int result;
char in_buffer[80];
struct termios tp1;
struct termios tp2;
tcgetattr(0,&tp1);
tp2=tp1;
tp2.c_iflag&=~ICRNL;
tp2.c_lflag&=~ICANON;
tp2.c_lflag&=~ECHO;
tp2.c_cc[VMIN ]=1;
tp2.c_cc[VTIME]=0;
tp2.c_cc[VINTR]=0xFF;
tp2.c_cc[VSUSP]=0xFF;
tp2.c_cc[VQUIT]=0xFF;
tcsetattr(0,TCSANOW,&tp2);
do
{
in_buffer[0]=0;
count=read(0,in_buffer,1);
} while(0);
tcsetattr(0,TCSANOW,&tp1);
result=in_buffer[0]&0xFF;
return result;
} /* main(void) */
/*
* This program waits for a character. It then outputs that character
* repeatedly until another character is input, and then outputs that
* character repeatedly until and so on.
*/
#include <stdio.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
int main(void)
{
int count;
int jndex;
int result;
int virgin;
char echoed_data;
char in_buffer[80];
struct termios tp1;
struct termios tp2;
virgin=1;
echoed_data='X';
printf("Type a space character to exit.\n");
tcgetattr(0,&tp1);
tp2=tp1;
tp2.c_iflag&=~ICRNL;
tp2.c_lflag&=~ICANON;
tp2.c_lflag&=~ECHO;
tp2.c_cc[VMIN ]=1;
tp2.c_cc[VTIME]=0;
tp2.c_cc[VINTR]=0xFF;
tp2.c_cc[VSUSP]=0xFF;
tp2.c_cc[VQUIT]=0xFF;
tcsetattr(0,TCSANOW,&tp2);
do
{
in_buffer[0]=0;
count=read(0,in_buffer,1);
if(virgin)
{
virgin=0;
tp2.c_cc[VMIN]=0;
tcsetattr(0,TCSANOW,&tp2);
}
if(count>0)
echoed_data=in_buffer[0];
printf("%c",echoed_data);
fflush(stdout);
} while(echoed_data!=0x20);
printf("\n");
tcsetattr(0,TCSANOW,&tp1);
} /* main(void) */
nudgetty.c:
Code:
/*
* This program repeatedly reads a single keystroke without echoing it, and
* dumps its value in hex and decimal. But it does not block when reading; if
* it doesn't get a character within a certain period of time, it outputs
* hyphens and then goes back to reading.
*/
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
volatile int ding_flag;
void alarm_dinger(int something)
{
ding_flag=1;
} // alarm_dinger(void)
int main(void)
{
int count;
int done;
int jndex;
int result;
char in_buffer[80];
struct sigaction new_action;
struct sigaction old_action;
struct termios tp1;
struct termios tp2;
done=0;
printf("Type a space character to exit.\n");
tcgetattr(0,&tp1);
tp2=tp1;
tp2.c_iflag&=~ICRNL;
tp2.c_lflag&=~ICANON;
tp2.c_lflag&=~ECHO;
tp2.c_cc[VMIN ]=1;
tp2.c_cc[VTIME]=0;
tp2.c_cc[VINTR]=0xFF;
tp2.c_cc[VSUSP]=0xFF;
tp2.c_cc[VQUIT]=0xFF;
tcsetattr(0,TCSANOW,&tp2);
new_action.sa_handler=alarm_dinger;
new_action.sa_flags=0;
ding_flag=0;
if(sigaction(SIGALRM,&new_action,&old_action))
{
printf("couldn't arm signal\n");
done=1;
}
alarm(5);
while(!done)
{
in_buffer[0]=0;
count=read(0,in_buffer,1);
if(ding_flag)
{
ding_flag=0;
printf("--- -- ");
fflush(stdout);
alarm(5);
}
else
{
printf("%3d %02X ",
(unsigned char)in_buffer[0],
(unsigned char)in_buffer[0]
);
fflush(stdout);
if(in_buffer[0]==0x20)
{
done=1;
}
}
}
if(sigaction(SIGALRM,&old_action,NULL))
{
printf("couldn't disarm signal\n");
done=1;
}
printf("\n");
tcsetattr(0,TCSANOW,&tp1);
} /* main(void) */
playtty.c:
Code:
/*
* This program repeatedly reads a character from the keyboard, blocking but
* not echoing. It displays in decimal and hex the value of the character and
* then goes back for more.
*/
#include <stdio.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
int main(void)
{
int count;
int jndex;
int result;
char in_buffer[80];
struct termios tp1;
struct termios tp2;
printf("Type a space character to exit.\n");
tcgetattr(0,&tp1);
tp2=tp1;
tp2.c_iflag&=~ICRNL;
tp2.c_lflag&=~ICANON;
tp2.c_lflag&=~ECHO;
tp2.c_cc[VMIN ]=1;
tp2.c_cc[VTIME]=0;
tp2.c_cc[VINTR]=0xFF;
tp2.c_cc[VSUSP]=0xFF;
tp2.c_cc[VQUIT]=0xFF;
tcsetattr(0,TCSANOW,&tp2);
do
{
in_buffer[0]=0;
count=read(0,in_buffer,1);
printf("%3d %02X ",
(unsigned char)in_buffer[0],
(unsigned char)in_buffer[0]
);
fflush(stdout);
} while(in_buffer[0]!=0x20);
printf("\n");
tcsetattr(0,TCSANOW,&tp1);
} /* main(void) */
You can use select() to check if input is available on stdin. And you can set the terminal to RAW mode (something that's probably done in the examples above using tcsetattr() )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.