This works, however, I need to press the <enter>-key in order to continue the program. So, if I send a message to the console like "Do you want to continue (Y/N)", and the user presses "Y", he also have to press <enter> after that to continue.
Actually I want *just* to press "Y" after which program continues. How can I do that in newLisp?
I have not found a way to do this on Linux/UNIX using standard 'libc' calls (not curses !). If someone can show me how, I will put a keyboard function in the next release.
int kb_hit(void)
{
#ifdef WIN32
char c;
while(!kbhit()){}; //wait for user to press a key
return(int)getch();
#else // Should work for most Unices
struct termios term, oterm;
int fd = 0;
int c = 0;
/* get the terminal settings */
tcgetattr(fd, &oterm);
/* get a copy of the settings, which we modify */
memcpy(&term, &oterm, sizeof(term));
/* put the terminal in non-canonical mode, any
reads timeout after 0.1 seconds or when a
single character is read */
term.c_lflag = term.c_lflag & (!ICANON);
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 1;
tcsetattr(fd, TCSANOW, &term);
/* get input - timeout after 0.1 seconds or
when one character is read. If timed out
getchar() returns -1, otherwise it returns
the character */
c=getchar();
/* reset the terminal to original state */
tcsetattr(fd, TCSANOW, &oterm);
/* return character */
return c;
#endif
}
This code I ripped from a webpage which does not exist anymore... I have adjusted it a little bit to fit my needs at the time, but I think it is suitable for a generic GETCHAR, also on most unices. I have tested it only with Linux.
>>> Isn't "int getchar(void)" part of the STDIO library?
Yes it is, but it sits in the functions until you hit <enter>, just like (read-char 0) does. 'C' getchar() is really a macro around the 'C' read(int handle ...) function newLISP is using.
Have it working on Linux/Solaris/MinGW/CYGWIN/Win32-Borland, but cannot get it to work on BSD. Anybody out there who can help with BSD (I tried on freeBSD 4.7) ???
struct termios term, oterm;
int c = 0;
tcgetattr(0, &oterm);
memcpy(&term, &oterm, sizeof(term));
/* put the terminal in non-canonical mode, any
reads timeout after 0.1 seconds or when a
single character is read */
term.c_lflag &= ~(ICANON | ECHO);
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 1;
tcsetattr(0, TCSANOW, &term);
while((c=getchar()) == -1);
/* reset the terminal to original state */
tcsetattr(0, TCSANOW, &oterm);