Solved

kbhit() and getch() in FreeBSD

Posted on 1997-06-04
1
1,234 Views
Last Modified: 2013-12-26
CAn you give me an example how to simulate kbhit() and
getch() in unix (FreeBSD). I've tried library called CURSED,
but I still couldn't find how to do kbhit().
0
Comment
Question by:lenin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
jos010697 earned 110 total points
ID: 1293221
Things are a bit more complicated in Unix: you have to
set your terminal to 'raw' mode and reset the 'echo'
flag, i.e. no line buffered input, no character translation
(backspace etc) and no echo of the keytpress. Here's
an example:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>

int raw_key()
{
struct sgttyb tty, tty_org;
char           key;
int            retval;

/* get current terminal setting and save it */

ioctl(fileno(stdin), TIOCGETP, &tty_org);
tty = tty_org;

/* set terminal to raw mode ... */
tty.sg_flags |= CRMOD;
tty.sg_flags &= ~ECHO;
tty.sg_flags &= ~XTABS;

/* tty.sg_flags |= CBREAK; */
tty.sg_flags |= RAW;
ioctl(0, TIOCSETP, &tty);
fcntl(0, F_SETFL, O_NDELAY);

/* read a keystroke if one's available */
if ((retval= read(0, &key, 1)) == 1)
        retval= key;

/* reset terminal characteristics */
ioctl(fileno(stdin), TIOCSETP, &tty_org);
fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0)&~O_NDELAY);

return retval;
}

kind regards,

Jos aka jos@and.nl
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question