[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

kbhit() and getch() in FreeBSD

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 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