fdhwlib  2.0.25
keyboard Class Reference

#include <keyboard.h>

List of all members.

Public Member Functions

Public Attributes

Private Attributes


Detailed Description

Emulates the kbhit() function known from.

the windows world

Definition at line 49 of file keyboard.h.


Constructor & Destructor Documentation

keyboard::keyboard ( ) [inline]

Definition at line 115 of file keyboard.h.

References newt, orig, and peek.

{



#ifdef __GNUC__

  peek = -1;



  tcgetattr(0, &orig);

  newt = orig;

  newt.c_lflag &= ~ICANON;

  newt.c_lflag &= ~ECHO;

  newt.c_lflag &= ~ISIG;

  newt.c_cc[VMIN] = 1;

  newt.c_cc[VTIME] = 0;

  tcsetattr(0, TCSANOW, &newt);

#endif



}
keyboard::~keyboard ( ) [inline]

Definition at line 151 of file keyboard.h.

References orig.

{

#ifdef __GNUC__

  tcsetattr(0,TCSANOW, &orig);

#endif

}

Member Function Documentation

char keyboard::getchar ( ) [inline]

Definition at line 232 of file keyboard.h.

References key, and peek.

Referenced by getpasswd(), and hit().

{

#ifdef __GNUC__

  char ch;



  if(peek != -1) {

    ch = peek;

    peek = -1;

    return ch;

  }



  read(0,&ch,1);

  return ch;

#else
  return(key);

#endif



}
char * keyboard::getpasswd ( ) [inline]

Read a password from the terminal.

Definition at line 268 of file keyboard.h.

References getchar(), hit(), and passwd.

                                 {
  char ch;
  int i;
  
  // Clear password
  fflush(stdout);  
  ch = 0;
  i = 0;
  while ((ch != '\n') && (ch != '\r') && (i<20)){
    if (hit()){
      ch = getchar();
      switch (ch){
        case '\n':
        case '\r':
          break;
        default:
          passwd[i] = ch;
          printf("*"); fflush(stdout);
          i++;
          break;
      }    
    }    
  }
    
  passwd[i] = 0;
  printf("\n");

  return(passwd);
  
}  
bool keyboard::hit ( ) [inline]

Definition at line 165 of file keyboard.h.

References key, newt, and peek.

Referenced by getpasswd(), and hit().

{



#ifdef __GNUC__


  char ch;
  int nread;



  if(peek != -1) return 1;

  newt.c_cc[VMIN]=0;

  tcsetattr(0, TCSANOW, &newt);

  nread = read(0,&ch,1);

  newt.c_cc[VMIN]=1;

  tcsetattr(0, TCSANOW, &newt);



  if(nread == 1) {

   peek = ch;

   return 1;

  }



#else
  
  //_kbhit(); // ??? Where is the difference
  if ( _kbhit() ) {
          key = _getch(); 
          return 1;
  }


#endif



  return 0;


}
bool keyboard::hit ( char  key) [inline]

Returns true if the right key.

Definition at line 223 of file keyboard.h.

References getchar(), and hit().

                                 {
  if (hit())
    return( getchar() == key);
  else
    return(false);
}

Member Data Documentation

char keyboard::key [private]

Definition at line 103 of file keyboard.h.

Referenced by getchar(), and hit().

struct termios keyboard::newt [private]

Definition at line 95 of file keyboard.h.

Referenced by hit(), and keyboard().

struct termios keyboard::orig [private]

Definition at line 89 of file keyboard.h.

Referenced by keyboard(), and ~keyboard().

char keyboard::passwd[20]

Character string that contains the last password entered.

Definition at line 79 of file keyboard.h.

Referenced by getpasswd().

int keyboard::peek [private]

Definition at line 99 of file keyboard.h.

Referenced by getchar(), hit(), and keyboard().


The documentation for this class was generated from the following file: