954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

gcc equivalent for getch()

Is there any standard function in gcc that is equivalent to getch() so that I can read the input without hitting enter key?

niyasc
Light Poster
29 posts since Sep 2009
Reputation Points: 7
Solved Threads: 1
 

I assume you're using a Linux system and not MinGW because MinGW's gcc supports getch(). You can reproduce the effect of getch() on Linux like this .

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 

Since getch() is not and never has been part of the language definition, you should consider not using it at all.

The link claims theconio.h is deprecated. It is not deprecated. It has never been defined by C nor C++. It's simply an add-on header extremely few compilers defined.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
Since getch() is not and never has been part of the language definition, you should consider not using it at all.


The language definition doesn't have an equivalent, so unless you're sure the behavior isn't needed, this is a bad argument for such advice. Depending on how getch() is used, it may or may not be superfluous though. If it's superfluous then there are grounds for removing the feature entirely in favor of portability. But that's not really your call, is it? ;)

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 
The language definition doesn't have an equivalent, so unless you're sure the behavior isn't needed, this is a bad argument for such advice. Depending on how getch() is used, it may or may not be superfluous though. If it's superfluous then there are grounds for removing the feature entirely in favor of portability. But that's not really your call, is it? ;)


Hence my phrasing: "you should consider not using it at all" :icon_wink:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
I assume you're using a Linux system and not MinGW because MinGW's gcc supports getch(). You can reproduce the effect of getch() on Linux like this .

This is great! I thought I could only do it using the ncurses library. Thank you, deceptikon.

Built using:andy@debian:/usr/include$ gcc --version
gcc (Debian 4.4.5-8) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

andy1973
Newbie Poster
6 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
 

just save this as conio.h to use getch() and getche() functions :

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

/* reads from keypress, doesn't echo */
int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

/* reads from keypress, echoes */
int getche(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}
jaybhanderi
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: