Hi Tuukka Toivonen,
Here is a short program that emulates the DOS kbhit(). This was circulated
in another mail group about 2 months ago, maybe this will help!
Good luck
John
------- Forwarded Message Follows -------
Date sent: Wed, 22 Apr 1998 03:53:41 -0400
From: holotko <[EMAIL PROTECTED]>
Organization: The Ruffe' School
To: [EMAIL PROTECTED]
Copies to: Jyh-shing <[EMAIL PROTECTED]>
Subject: Re: [linuxprog] kbhit() ?
Send reply to: [EMAIL PROTECTED]
Jyh-shing wrote:
>
> Hi,
>
> How can I make an equivalent of a DOS kbhit() funcction in Linux to
> check for keyboard input ?
>
> Thanks,
>
> Jyh-shing Chen
Here's a textbook example that I just happened to have lying around
here. It appears to use termios structuires to set terminal attributes,
such as non-canonical mode, and turning off echoing , etc. Although I
haven't had a chance to examine this program carefully, nor have I
attempted to code a "better" version myself, I nonetheless, present it
here in hope that it may set you in the right direction:
---------------------------------------------------------------------
/* kbhit.c */
/* Works the same as the "kbhit()" function of msdos */
/* ------------------------------------------------- */
/* Set up standard headers, declares needed structures, variables, and *
* set up needed function prototypes */
#include <stdio.h>
#include <termios.h>
#include <term.h>
#include <curses.h>
#include <unistd.h>
static struct termios initial_settings, new_settings;
static int peek_charachter = -1;
void init_keyboard();
void close_keyboard();
int kbhit();
int readch();
/* main function, calls init_keyboard() to configure the terminal, then
loops once per second calling kbhit() each time checking for a key hit. If
the key hit is 'q' then close_keyboard() returns the initial terminal
settings and, the program exits. */
int main()
{
int ch = 0;
init_keyboard();
while(ch != 'q') {
printf("looping\n");
sleep(1);
if (kbhit()) {
ch = readch();
printf("you hit %c\n", ch);
}
}
close_keyboard();
exit(0);
}
/* init_keyboard() and close_keyboard() configure the terminal at the
start and end of the program */
void init_keyboard()
{
tcgetattr(0, &initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON; /* non cannoncal mode */
new_settings.c_lflag &= ~ECHO; /* turn off echo */
new_settings.c_lflag &= ~ISIG; /* turn off signals */
new_settings.c_cc[VMIN] = 1; /* # of charachters */
new_settings.c_cc[VTIME] = 0; /* delay time = 0 sec */
tcsetattr(0, TCSANOW, &new_settings);
}
void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}
/* The "kbhit()" function, actually checks for the keyboard hit */
int kbhit()
{
char ch;
int nread;
if (peek_charachter != -1)
return 1;
new_settings.c_cc[VMIN] = 0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0, &ch, 1);
new_settings.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_settings);
if (nread == 1) { /* If 1 byte was read */
peek_charachter = ch;
return 1;
}
return 0;
}
/* readch(), reads the charachter that was pressed and resets *
* peek_charachter to -1 for the next loop */
int readch()
{
char ch;
if (peek_charachter != -1) {
ch = peek_charachter;
peek_charachter = -1;
return ch;
}
read(0, &ch, 1);
return ch;
}
/* (c) "Beginning Linux Programming" by Mathew & Stones, WROX Press LTD */
------------------------------------------------------------
--
email: [EMAIL PROTECTED]
Local mailserver <landreau.ruffe.edu> , remote <ns.computer.net>
There is a great alternative to war, it's called Peace.
///
(. .)
/=====oOO-(_)-OOo===========================\
/ \
/ John Gorman \
| E-mail: [EMAIL PROTECTED] |
| |
\ "We are what we repeatedly do." /
\ Aristotle /
\===========================================/