> Hi all,
> I want to get the character while I strike any key,I try to use
getchar(),but it need to strike ENTER to end of string,can I get the value
of key immediately before I strike ENTER.In DOS,I can use kbhit() to check
if there is any key to be striked,but how can I do in linux?
> I have no idea how to get the key. Do you have any idea?
>
> Thanks!
>
> Michael
>
Hi,
I've seen you have had many replies, heres mine which I'm using in a program
which is cross platform. Just call enable and it'll work. Make sure you
call disable when you program exists (even for things like ^C)!
// Simulate Standard Microsoft Extensions under Unix
int _kbhit (void)
{ // Set no delay
static struct timeval tv = {0, 0};
fd_set rdfs;
// See if key has been pressed
FD_ZERO (&rdfs);
FD_SET (STDERR_FILENO, &rdfs);
if (select (STDERR_FILENO + 1, &rdfs, NULL, NULL, &tv) <= 0)
return 0;
if (FD_ISSET (STDERR_FILENO, &rdfs))
return 1;
return 0;
}
int _getch (void)
{
char ch = 0;
if (read (STDERR_FILENO, &ch, 1) < 0);
return -1;
return ch;
}
// Set keyboard to raw mode so getch will work
static termios term;
void keyboard_enable_raw ()
{
// set to non canonical mode, echo off, ignore signals
struct termios current;
// save current terminal settings
tcgetattr (STDERR_FILENO, ¤t);
// set to non canonical mode, echo off, ignore signals
term = current;
current.c_lflag &= ~(ECHO | ICANON | IEXTEN);
current.c_cc[VMIN] = 1;
current.c_cc[VTIME] = 0;
tcsetattr (STDERR_FILENO, TCSAFLUSH, ¤t);
}
void keyboard_disable_raw ()
{
// Restore old terminal settings
tcsetattr (STDERR_FILENO, TCSAFLUSH, &term);
}
Simon
-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
--
For more information on Real-Time Linux see:
http://www.rtlinux.org/rtlinux/