Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-28-2002, 02:51 PM   #1
Senior Member
Registered: Oct 2002
Location: Belgium
Distribution: Debian, Free/OpenBSD
Posts: 1,123

Rep: Reputation: 47

I'll rewrite my question here after posting it in the wrong place .
Is there an alternative for the function kbhit() as included in Borland's conio.h?
Thanks again.
Old 10-28-2002, 04:40 PM   #2
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Here's a complete example program that should get you there. Link it with the ncurses libs.

#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_character = -1;

void init_keyboard();
void close_keyboard();
int kbhit();
int readch();

int main()
    int ch = 0;

    while(ch != 'q') {
        if(kbhit()) {
            ch = readch();
            printf("you hit %c\n",ch);

void init_keyboard()
    new_settings = initial_settings;
    new_settings.c_lflag &= ~ICANON;
    new_settings.c_lflag &= ~ECHO;
    new_settings.c_lflag &= ~ISIG;
    new_settings.c_cc[VMIN] = 1;
    new_settings.c_cc[VTIME] = 0;
    tcsetattr(0, TCSANOW, &new_settings);

void close_keyboard()
    tcsetattr(0, TCSANOW, &initial_settings);

int kbhit()
    char ch;
    int nread;

    if(peek_character != -1)
        return 1;
    tcsetattr(0, TCSANOW, &new_settings);
    nread = read(0,&ch,1);
    tcsetattr(0, TCSANOW, &new_settings);

    if(nread == 1) {
        peek_character = ch;
        return 1;
    return 0;

int readch()
    char ch;

    if(peek_character != -1) {
        ch = peek_character;
        peek_character = -1;
        return ch;
    return ch;
From the downloaded sources (GPL'd) of the book "Beginning Linux Programming" - Wrox.
Old 10-28-2002, 05:09 PM   #3
Senior Member
Registered: Oct 2002
Location: Belgium
Distribution: Debian, Free/OpenBSD
Posts: 1,123

Original Poster
Rep: Reputation: 47
That helped me alot. Thanks once more for your time
Old 05-22-2008, 10:55 AM   #4
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16

This is a great post and has helped me loads.

How can I modify this to detect the arrow keys?


Old 05-22-2008, 11:21 PM   #5
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Use printf("you hit %0x02x\n",ch); instead of the original printf.
It will tell you (in hex) which data is received. Next you can analyse and modify the code to do what it needs to do.

I seem to remember that (at least in DOS) you will get two bytes back when you hit the arrow keys, home etc.
Old 09-20-2009, 12:34 PM   #6
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
why not just generate interrupt 16h


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
kbhit () Mohsen Programming 2 03-29-2003 08:36 PM
Borland's kbhit() iceman47 Linux - General 1 10-28-2002 02:46 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:32 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration