?
Solved

programming a menu(termios,getch)

Posted on 2003-03-26
2
Medium Priority
?
1,146 Views
Last Modified: 2008-12-02
I tried to write a simple menu program under linux in cpp. But then
i realized that there is no conio.h under linux. So actuali I am
trying to find a replacement for that simple function (getch) from borlanc Cpp. I can't read the keybord (the directional keys anyway)I heard that using termios.h could solve my problem but the man page is not that helpfull. I would like it to be as simple as possible.
0
Comment
Question by:Xumxum
[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
  • 2
2 Comments
 
LVL 11

Expert Comment

by:dimitry
ID: 8209880
There is no getch() function but it can be implemented for example next way:

#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()
{
  tcgetattr( 0, &initial_settings );
  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 );
  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 ) {
    peek_character = ch;
    return( 1 );
  }
  return( 0 );
}

int readch()
{
  char ch;
 
  if( peek_character != -1 ) {
    ch = peek_character;
    peek_character = -1;
    return( ch );
  }
  read( 0, &ch, 1 );
  return( ch );
}

int main()
{
 init_keyboard();
.....
 readch();         <---- getch()
.....
 close_keyboard();
}
0
 
LVL 11

Accepted Solution

by:
dimitry earned 700 total points
ID: 8209887
Sorry, small mistake...

int getch()
{
  while( !kbhit() ) ;
  return( readch() );
}
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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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