keyboard.cpp
Go to the documentation of this file.
00001 #include "keyboard.h"
00002 #include <termios.h>
00003 #include <unistd.h>   // for read()
00004 
00005 static struct termios initial_settings, new_settings;
00006 static int peek_character = -1;
00007 
00008 void init_keyboard()
00009 {
00010     tcgetattr(0,&initial_settings);
00011     new_settings = initial_settings;
00012     new_settings.c_lflag &= ~ICANON;
00013     new_settings.c_lflag &= ~ECHO;
00014     new_settings.c_lflag &= ~ISIG;
00015     new_settings.c_cc[VMIN] = 1;
00016     new_settings.c_cc[VTIME] = 0;
00017     tcsetattr(0, TCSANOW, &new_settings);
00018 }
00019 
00020 void close_keyboard()
00021 {
00022     tcsetattr(0, TCSANOW, &initial_settings);
00023 }
00024 
00025 int kbhit()
00026 {
00027 unsigned char ch;
00028 int nread;
00029 
00030     if (peek_character != -1) return 1;
00031     new_settings.c_cc[VMIN]=0;
00032     tcsetattr(0, TCSANOW, &new_settings);
00033     nread = read(0,&ch,1);
00034     new_settings.c_cc[VMIN]=1;
00035     tcsetattr(0, TCSANOW, &new_settings);
00036     if(nread == 1)
00037     {
00038         peek_character = ch;
00039         return 1;
00040     }
00041     return 0;
00042 }
00043 
00044 int readch()
00045 {
00046 char ch;
00047 
00048     if(peek_character != -1)
00049     {
00050         ch = peek_character;
00051         peek_character = -1;
00052         return ch;
00053     }
00054     read(0,&ch,1);
00055     return ch;
00056 }
00057 
00058 int _getch() {
00059         init_keyboard();
00060         kbhit();
00061         char c = readch();
00062         close_keyboard();
00063         return c;
00064 }


kni
Author(s): Martin Günther
autogenerated on Mon Aug 14 2017 02:44:12