kbhit.cc

00001 // KBHIT.CPP
00002 
00003 #include "kbhit.h"
00004 #include <unistd.h> // read()
00005 
00006 keyboard::keyboard()
00007 {
00008     tcgetattr(0,&initial_settings);
00009     new_settings = initial_settings;
00010     new_settings.c_lflag &= ~ICANON;
00011     new_settings.c_lflag &= ~ECHO;
00012     new_settings.c_lflag &= ~ISIG;
00013     new_settings.c_cc[VMIN] = 1;
00014     new_settings.c_cc[VTIME] = 0;
00015     tcsetattr(0, TCSANOW, &new_settings);
00016     peek_character=-1;
00017 }
00018 
00019 keyboard::~keyboard()
00020 {
00021     tcsetattr(0, TCSANOW, &initial_settings);
00022 }
00023 
00024 int keyboard::kbhit()
00025 {
00026 unsigned char ch;
00027 int nread;
00028 
00029     if (peek_character != -1) return 1;
00030     new_settings.c_cc[VMIN]=0;
00031     tcsetattr(0, TCSANOW, &new_settings);
00032     nread = read(0,&ch,1);
00033     new_settings.c_cc[VMIN]=1;
00034     tcsetattr(0, TCSANOW, &new_settings);
00035 
00036     if (nread == 1)
00037     {
00038         peek_character = ch;
00039         return 1;
00040     }
00041     return 0;
00042 }
00043 
00044 int keyboard::getch()
00045 {
00046   char ch;
00047   int nread;
00048   if (peek_character != -1)
00049     {
00050       ch = peek_character;
00051       peek_character = -1;
00052     } 
00053   else 
00054     nread=read(0,&ch,1);
00055 
00056     return ch;
00057 }
00058 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1