Project Homepage | Sourceforge Page | CVS Repository | Freshmeat.net Page | Download project | Author's Homepage |
00001 /*!\file kbhit.c 00002 \brief Handle console keyboard functionality 00003 */ 00004 /* NOTICE: 00005 Copyright (C) 2004 Karl N. Redman (SleepingStill.com) 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 For further information contact: parasyte@sleepingstill.com 00022 */ 00023 00024 #include <stdio.h> 00025 #include <termios.h> 00026 #include <term.h> 00027 #include <curses.h> 00028 #include <unistd.h> 00029 00030 #include "kbhit.h" 00031 00032 ///console termio settings (pre and post operation) 00033 static struct termios initial_settings, new_settings; 00034 00035 /** 00036 \brief Handle a keybaord keypress 00037 00038 \author Karl N. Redman, various. 00039 */ 00040 int do_kbhit() 00041 { 00042 int ch = 0; 00043 00044 printf("PRESS ANY KEY TO CONTINUE"); 00045 fflush((FILE *)0); 00046 00047 //set keyboard state - no echo, single char input 00048 init_keyboard(); 00049 00050 //get charecter 00051 read(STDIN_FILENO, &ch, 1); /* getchar() works here too */ 00052 printf("%c",ch); 00053 00054 //set keyboard back to initial state 00055 close_keyboard(); 00056 00057 return(0); 00058 } 00059 00060 /** 00061 \brief Initialize the keyboard for console keyboard handling 00062 00063 \author Karl N. Redman, various 00064 */ 00065 void init_keyboard() 00066 { 00067 // struct termios new_settings; 00068 00069 //save attribures for close 00070 tcgetattr(0, &initial_settings); 00071 00072 //set no echo, one char input, 00073 new_settings = initial_settings; 00074 new_settings.c_lflag &= ~ICANON; 00075 new_settings.c_lflag &= ~ECHO; 00076 new_settings.c_lflag &= ~ISIG; 00077 new_settings.c_cc[VMIN] = 1; 00078 new_settings.c_cc[VTIME] = 0; 00079 00080 tcsetattr(0, TCSANOW, &new_settings); 00081 } 00082 00083 /** 00084 \brief Reset keyboard to initial settins 00085 00086 \author Karl N. Redman, various 00087 */ 00088 void close_keyboard() 00089 { 00090 //reset terminal to initial settings 00091 tcsetattr(0, TCSANOW, &initial_settings); 00092 }