[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

kbhit() and getch() in FreeBSD

Posted on 1997-06-04
1
Medium Priority
?
1,246 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 220 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 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