I would like to write a C++ programme on Ubuntu,
which reacts immediately to the input without pressing enter.
(-> I cannot use the header #include <conio.h>
due to the reason that I am working on an UNIX system)
For instance: I press on my keyboard the key "a", but instead of showing "a" in the terminal, the programme should show "p".
For the last two days, I tried to do this with the header #include <ncurses.h>
.
Unfortunately, it doesn't work.
Therefore, I would like to ask for your request.
With conio.h it would be like this:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main(void)
{
char c;
c = getch();
while(true)
{
if(c=='a')
{
putch('p');
}
else
{
putch(c);
}
c = getch();
}
cin.sync();
cin.get();
}
Can you please simply post the working source code with #include <ncurses.h>
instead of #include <conio.h>
?
Thank you so much in advance!!!
With best regards
quark42
Thank you Paulo1205!!!!
Here is my final code without conio.h:
#include <iostream>
#include <string>
#include <unistd.h>
#include <termios.h>
#include <ncurses.h>
using namespace std;
int my_getch(void){
struct termios oldattr, newattr;
unsigned char ch;
int retcode;
tcgetattr(STDIN_FILENO, &oldattr);
newattr=oldattr;
newattr.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
retcode=read(STDIN_FILENO, &ch, 1);
tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
return retcode<=0? EOF: (int)ch;
}
int main(void)
{
char c;
c = my_getch();
while(true)
{
if(c=='a')
{
putchar('p'); fflush(stdout);
}
else
{
putchar(c); fflush(stdout);
}
c = my_getch();
}
cin.sync();
cin.get();
}