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.
is there any ansi-c-function, that just read one char from keyboard and returns this value? Functions I know are getc(), fgetc() and getchar(); but all this functions read from stdin until carry return and sends every char to stdout like scanf(). I need function, that do following:
- waits until one key is pressed,
- reads value of this key from stdin,
- does not output this char to stdout,
- returns ascii value,
You have to change the terminal to non-buffered I/O. You can do this with the termios() function. The man page might not give you enough details but there's a ton of sample code doing it if you do a google search search on "non-buffered termios".
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
struct termio original_termio;
void enterInputMode()
{
//Settings for stdin (source: svgalib):
int fd = fileno(stdin);
struct termio zap;
ioctl(fd, TCGETA, &original_termio);
zap = original_termio;
zap.c_cc[VMIN] = 0;
zap.c_cc[VTIME] = 0;
zap.c_lflag = 0;
ioctl(fd, TCSETA, &zap);
}
void leaveInputMode()
{
//Restore original stdin
int fd = fileno(stdin);
ioctl(fd, TCSETA, &original_termio);
}
char readKey()
{
int fd = fileno(stdin);
char c = '\0';
int e = read(fd, &c, 1);
if (e == 0) c = '\0';
return c;
}
Call enterInputMode() before doing any readKey() calls, and leaveInputMode() after you're finished, to leave the terminal as bash etc. expect it to be. readKey returns '\0' if there's no key pressed. To wait for a key, do something like this:
Code:
char c = '\n'; //Just anything not equal to '\0'
while(c != '\0') c = readKey();
Special keys, like the arrow keys or the function keys, return "escape sequences". You can use the cat program without arguments to discover these. To scan them, you should do several readKey calls to check for escape sequences whenever the escape character is detected. Most terminal programs (xterm, konsole, PuTTY etc.) generate the same sequences, but to do things correctly you should read the termcap database. I never did this, but if you really want to do it, then it might be easier to use the ncurses library, which does everything for you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.