Linux equivalent for conio.h getch()
There are a number of different ways of doing this more portably. The simplest is to use curses:
#include "curses.h"
int main() {
initscr();
addstr("hit a key:");
getch();
return endwin();
}
What is the equivalent to getch() & getche() in Linux?
#include <termios.h>
#include <stdio.h>
static struct termios old, current;
/* Initialize new terminal i/o settings */
void initTermios(int echo)
{
tcgetattr(0, &old); /* grab old terminal i/o settings */
current = old; /* make new settings same as old settings */
current.c_lflag &= ~ICANON; /* disable buffered i/o */
if (echo) {
current.c_lflag |= ECHO; /* set echo mode */
} else {
current.c_lflag &= ~ECHO; /* set no echo mode */
}
tcsetattr(0, TCSANOW, ¤t); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
/* Read 1 character - echo defines echo mode */
char getch_(int echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
/* Read 1 character without echo */
char getch(void)
{
return getch_(0);
}
/* Read 1 character with echo */
char getche(void)
{
return getch_(1);
}
/* Let's test it out */
int main(void) {
char c;
printf("(getche example) please type a letter: ");
c = getche();
printf("\nYou typed: %c\n", c);
printf("(getch example) please type a letter...");
c = getch();
printf("\nYou typed: %c\n", c);
return 0;
}
Output:
(getche example) please type a letter: g
You typed: g
(getch example) please type a letter...
You typed: g
Where is the conio.h header file on Linux? Why can't I find conio.h ?
conio.h
is a C header file used with old MS-DOS compilers to create text user interfaces. Compilers that target other operating systems, such as Linux-based, 32-bit Windows and OS/2, provide equivalent functionality through other header files and libraries.
The #include <curses.h>
will give you almost all of the functionality provided by conio.h
.
"ncurses" needs to be installed in the first place.
If you use the Apt package manager:
sudo apt-get install libncurses5-dev libncursesw5-dev
If you use rpm
:
sudo yum install ncurses-devel ncurses
For getch
, take a look at the "NCURSES Programming HOWTO" article.
Replacement for conio.h in Linux
There is an replacement version of Conio.h for linux based on NCurses.
http://sourceforge.net/projects/linux-conioh/
Using kbhit() and getch() on Linux
The ncurses howto cited above can be helpful. Here is an example illustrating how ncurses could be used like the conio example:
#include <ncurses.h>
int
main()
{
initscr();
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
while (true) {
if (getch() == 'g') {
printw("You pressed G\n");
}
napms(500);
printw("Running\n");
}
}
Note that with ncurses, the iostream
header is not used. That is because mixing stdio with ncurses can have unexpected results.
ncurses, by the way, defines TRUE
and FALSE
. A correctly configured ncurses will use the same data-type for ncurses' bool
as the C++ compiler used for configuring ncurses.
Is there an equivalent of getch() function in Mac?
You probably want to look at ncurses. It has a lot in common with conio. You can get its documentation with "man ncurses".
How can I use kbhit() and getch() on Linux? C++
As zoelabbb said in this link,
Step 1 :
Open terminal sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git
git clone https://github.com/zoelabbb/conio.h.git
cd conio.h
Step 2 :
sudo cp conio.h /usr/include/
or (step 2)
In GUI, using open-as-administrator
pakage:
Copy file conio.h --> !! copy file not folder !!
Go to /usr/include/
Right click on folder /usr/include/
Choose Open as Administrator
Paste file conio.h
Finaly after step 1 and step 2 you can use #include <conio.h>
in your code.
Related Topics
Conversion from String Literal to Char* Is Deprecated
Changing the Value of Const Variable in C++
Why Floating Point Value Such as 3.14 Are Considered as Double by Default in Msvc
What Do Each Memory_Order Mean
How to Get the Number of Characters in a Std::String
How to Create a Pause/Wait Function Using Qt
C++ Std::Vector VS Array in the Real World
How Would I Load a Png Image Using Win32/Gdi (No Gdi+ If Possible)
C++: Is It Safe to Cast Pointer to Int and Later Back to Pointer Again
How to Modify Values in a Map Using Range Based for Loop
Why Does 'Std::Vector<Int> B{2};' Create a 1-Element Vector, and Not a 2-Element One
How to Use Openssl's Sha256 Functions
Getdibits and Loop Through Pixels Using X, Y
Understanding Recursion to Generate Permutations
How to Get the Class Name from a C++ Object
Are There Practical Uses for Dynamic-Casting to Void Pointer