The un*x to OS/2-EMX Porting FAQ:Sample Code: Difference between revisions
Appearance
Created page with "Sample code of the "The un*x to OS/2-EMX Porting FAQ" article: →$Id$ Some code to query for a single keypress within an xterm on XFree86OS/2: #include <s..." |
mNo edit summary |
||
Line 1: | Line 1: | ||
<code> | |||
/* | /* | ||
$Id$ | $Id$ | ||
Some code to query for a single keypress within an xterm on | Some code to query for a single keypress within an xterm on | ||
XFree86OS/2 | XFree86OS/2 | ||
*/ | */ | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <termios.h> | #include <termios.h> | ||
int kbhit (void) | int kbhit (void) | ||
Line 96: | Line 93: | ||
else | else | ||
return (char) 0; | return (char) 0; | ||
} | } | ||
</code> | |||
Revision as of 18:45, 23 December 2017
/*
$Id$
Some code to query for a single keypress within an xterm on
XFree86OS/2
*/
#include <stdio.h>
#include <termios.h>
int kbhit (void)
/* check whether a key was pressed */
{
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);
/* if we retrieved a character, put it back on
the input stream */
if (c != -1)
ungetc (c, stdin);
/* return 1 if the keyboard was hit, or 0 if it
was not hit */
return ((c != -1) ? 1 : 0);
}
/********************************************************/
int getch (void)
/* get pressed key */
{
int c, fd = 0;
struct termios term, oterm;
/* 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 will wait until a character has been
pressed. This function will not time out */
term.c_lflag = term.c_lflag & (!ICANON);
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
tcsetattr (fd, TCSANOW, &term);
/* get a character. c is the character */
c = getchar ();
/* reset the terminal to its original state */
tcsetattr (fd, TCSANOW, &oterm);
/* return the character */
return c;
}
/********************************************************/
int getkey (void)
/* merge kbhit() and getch() into a useful one :-) */
{
if (kbhit())
return getch();
else
return (char) 0;
}