getch() UNIX equivalent?

Status
Not open for further replies.
I'm trying to detect when a user presses a specific key for a console program in Solaris (without first pressing enter). My searches online have shown that this is really simple in DOS/Windows, which has access to conio.h and the getch() function, which does exactly what I need.

My search has been fruitless, for the most part, on the UNIX side of things. Keyboard handling seems to consist of ugly hacks. Is there a fairly simple way of detecting keypresses that someone could clue me in on? All I want to do with this is detect when a special character is hit, terminate all threads, and exit.

Thanks in advance :)
 

Drutro

Ars Praetorian
461
Subscriptor++
Well, this is in Linux, but the man page for 'gets' (section 3) reveals a few functions you may find useful. fgetc(), getc(), and getchar() look relevant.

#include <stdio.h>

int fgetc(FILE *stream);
char *fgets(char *s, int size, FILE *stream);
int getc(FILE *stream);
int getchar(void);
char *gets(char *s);
int ungetc(int c, FILE *stream);

fgetc() reads the next character from stream and returns
it as an unsigned char cast to an int, or EOF on end of
file or error.

getc() is equivalent to fgetc() except that it may be
implemented as a macro which evaluates stream more than
once.

getchar() is equivalent to getc(stdin).

I suppose you could snag a character and then do a test if it is the one you are looking for, then act appropriately.
 
Status
Not open for further replies.