cmKeyboard.h/c : Added cmIsKeyWaiting().
This commit is contained in:
parent
f002553852
commit
44cdad61e0
34
cmKeyboard.c
34
cmKeyboard.c
@ -233,3 +233,37 @@ void testKb2()
|
||||
|
||||
reset_keypress();
|
||||
}
|
||||
|
||||
|
||||
// Based on: // From: http://www.flipcode.com/archives/_kbhit_for_Linux.shtml
|
||||
|
||||
int cmIsKeyWaiting()
|
||||
{
|
||||
static const int STDIN = 0;
|
||||
static bool initialized = false;
|
||||
struct timeval timeout;
|
||||
fd_set rdset;
|
||||
|
||||
if (! initialized)
|
||||
{
|
||||
// Use termios to turn off line buffering
|
||||
struct termios term;
|
||||
tcgetattr(STDIN, &term);
|
||||
term.c_lflag &= ~ICANON;
|
||||
tcsetattr(STDIN, TCSANOW, &term);
|
||||
setbuf(stdin, NULL);
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
|
||||
FD_ZERO(&rdset);
|
||||
FD_SET(STDIN, &rdset);
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
// time out immediately if STDIN is not ready.
|
||||
return select(STDIN + 1, &rdset, NULL, NULL, &timeout);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
29
cmKeyboard.h
29
cmKeyboard.h
@ -34,4 +34,33 @@ typedef struct
|
||||
// Set 'p' to NULL if the value of the key is not required.
|
||||
void cmKeyPress( cmKbRecd* p );
|
||||
|
||||
|
||||
// Return non-zero if a key is waiting to be read otherwise return 0.
|
||||
// Use getchar() to pick up the key.
|
||||
//
|
||||
// Example:
|
||||
// while( 1 )
|
||||
// {
|
||||
// if( cmIsKeyWaiting() == 0 )
|
||||
// usleep(20000);
|
||||
// else
|
||||
// {
|
||||
// char c = getchar();
|
||||
// switch(c)
|
||||
// {
|
||||
// ....
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// TODO: Note that this function turns off line-buffering on stdin.
|
||||
// It should be changed to a three function sequence.
|
||||
// bool org_state = cmSetStdinLineBuffering(false);
|
||||
// ....
|
||||
// cmIsKeyWaiting()
|
||||
// ....
|
||||
// cmSetStdinLineBuffering(org_state)
|
||||
int cmIsKeyWaiting();
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user