C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 11-01-2002, 11:56 AM   #1
Lead Moderator
 
kermi3's Avatar
 
Join Date: Aug 1998
Posts: 2,568
FAQ: Is there a getch() (from conio) equivalent on Linux/UNIX?

Q: Is there a getch() (from conio) equivalent on Linux/UNIX?

A: No. But it's easy to emulate:



code:--------------------------------------------------------------------------------
Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

int mygetch( ) {
  struct termios oldt,
                 newt;
  int            ch;
  tcgetattr( STDIN_FILENO, &oldt );
  newt = oldt;
  newt.c_lflag &= ~( ICANON | ECHO );
  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
  ch = getchar();
  tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
  return ch;
}--------------------------------------------------------------------------------
This code sets the terminal into non-canonical mode, thus disabling line buffering, reads a character from stdin and then restores the old terminal status. For more info on what else you can do with termios, see ``man termios''.
There's also a ``getch()'' function in the curses library, but it is /not/ equivalent to the DOS ``getch()'' and may only be used within real curses applications (ie: it only works in curses ``WINDOW''s)..


Thanks to VvV for writing this.
__________________
Kermi3

If you're new to the boards, welcome and reading this will help you get started.
Information on code tags may be found here

- Sandlot is the highest form of sport.
kermi3 is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
getch() equivalent in C++ hauzer C++ Programming 3 06-02-2009 05:54 PM
Another syntax error caldeira C Programming 31 09-05-2008 01:01 AM
Wiki FAQ dwks General Discussions 192 04-29-2008 01:17 PM
kbhit() and getch() Equivalent.. ThLstN C Programming 1 03-11-2008 02:44 AM
Pls repair my basketball program death_messiah12 C++ Programming 10 12-11-2006 05:15 AM


All times are GMT -6. The time now is 06:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22