Code:
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.cc
*
* chkkeystroke is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* chkkeystroke is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>
#include <fcntl.h>
struct termios orig_termios;
void reset_terminal_mode()
{
tcsetattr(0, TCSANOW, &orig_termios);
}
void set_conio_terminal_mode()
{
struct termios new_termios;
/* take two copies - one for now, one for later */
tcgetattr(0, &orig_termios);
memcpy(&new_termios, &orig_termios, sizeof(new_termios));
/* register cleanup handler, and set the new terminal mode */
atexit(reset_terminal_mode);
cfmakeraw(&new_termios);
tcsetattr(0, TCSANOW, &new_termios);
}
int kbhit()
{
struct timeval tv = { 0L, 0L };
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
// normaly 1, but better ch, so we have the ASCII-value;
int xkbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return ch;// Normaly return value 1, here the ASCII-value
}
return 0;
}
/// reads from keypress, doesn't echo
int getch(){
struct termios oldt, newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
int own_getstring(char wort[])
{
int rewer = 0, taste = 0, i = 0;
while ((taste == 10) || (taste == 13))
{
taste = xkbhit();
if ((taste == 10) || (taste == 13))
{
rewer = 1;
}
else
{
wort[i++] = (char)taste;
wort[i] = '\0';
}
}
return rewer;
}
int main(int argc, char *argv[])
{
int taste = 0, i = 0;
char wort[200] = {0};
//set_conio_terminal_mode(); // Wenn nicht gesetzt dann bei gedrueckter Enter-Taste
/* // with xkbhit returnvalue 10, if set then 13
while (!xkbhit())
{}
printf("%c", taste);
*/
for (i = 0; i < 20; i++)
{
while (taste == 0)
taste = xkbhit();
usleep(100000); //<--------------------you can modify these number, try and see
printf("%c", taste);
taste = 0;
}
printf("***%c = %d i=%d\n", taste, taste, i);
//taste = own_getstring(wort);
//printf("Enter was: %s\n", wort);
getch();
printf("Program end\n");
return EXIT_SUCCESS;
}
It was written for SUSE Linux tumbleweed, but i hope, it can help you.