Tuesday, April 4, 2023

getch and getche in Linux in both C++ and C

In C++ language :-

-----------------------------

 file : ClassGetch.h
-------------------------------

#ifndef ClassGetch_H
#define ClassGetch_H

#include <termio.h>
#include <cstdio>

#include <iostream>
#include <iomanip>
#include <unistd.h>

static struct termios oldterm, newterm;

class ClassGetch
{
public:
    ClassGetch();
    virtual ~ClassGetch();

    char getch();
    char getche();
    void initTermios(bool echo);
    void resetTermios(void);



protected:

private:

    termios   oldterm;
    termios   newterm;
    char getch_(bool echo);

};

#endif // ClassGetch_H

---------------------------------------------------------------
// file :- ClassGetch.cpp

-------------------------------------------------
#include "ClassGetch.h"

ClassGetch::ClassGetch()
{
    //ctor
}

ClassGetch::~ClassGetch()
{
    //dtor
}

void ClassGetch::initTermios(bool echo)
{
    tcgetattr(0, &oldterm);
    newterm = oldterm;
    newterm.c_lflag &= ~ICANON;
    newterm.c_lflag &= echo ? ECHO : ~ECHO;
    tcsetattr(0, TCSANOW, &newterm);
}

void ClassGetch::resetTermios()
{
    tcsetattr(0, TCSANOW, &oldterm);
}

char ClassGetch::getch_(bool echo)
{
     char ch;
     initTermios(echo);
     ch = getchar();
     resetTermios();
     return ch;
}


char ClassGetch::getch()
{
    return getch_(false);
}

char ClassGetch::getche()
{
    return getch_(true);
}

-----------------------------------------------------------------------------------

Include class and write above code then use getche() and getch() as where you want in your program.

getche() wait for echo of char i.e. prints a character and then returns, while getch() doesn't wait for echo of char and returns without print of char.

 -----------------------------------------------------------------------------------
file : main.cpp
-----------------------
#include "ClassGetChoice.h"
#include <iostream>


using std::cout;
using std::endl;

int main()
{
    char c;

    ClassGetch * cg = new ClassGetch;

    cout << "getche() example : " << endl;
    cout << "Enter any character : ";
 

    c = cg->getche();

    cout << "\nyou entered : " << c <<endl;

    cout << "getch() Example : "<< endl;
    cout << "Enter any character : ";
 

    c = cg->getch();


    cout << "\nYou entered : " << c << endl;

    return 0;
}

/* 

output :-

-------------

[rahul@C-Client Debug]$ ./getche-getch
getche() example :
Enter any character : w
you entered : w
getch() Example :
Enter any character :
You entered : w
[rahul@C-Client Debug]$
*/
----------------------------------------------------------------------------

In C language : -

-------------------------

file : getche-getc.h :-
-----------------------

#ifndef GETCHE-GETCH_H_INCLUDED
#define GETCHE-GETCH_H_INCLUDED

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

struct termios   oldterm;
struct termios   newterm;

void initTermios(int echo);

void resetTermios();

char getch_(int echo);

char getch();

char getche();

#endif // GETCHE-GETCH_H_INCLUDED
------------------------------------------

file : getche-get.c :-
----------------------
#include "getche-getch.h"

void initTermios(int echo)
{
    tcgetattr(0, &oldterm);
    newterm = oldterm;
    newterm.c_lflag &= ~ICANON;
    newterm.c_lflag &= echo ? ECHO : ~ECHO;
    tcsetattr(0, TCSANOW, &newterm);
}

void resetTermios()
{
    tcsetattr(0, TCSANOW, &oldterm);
}

char getch_(int echo)
{
     char ch;
     initTermios(echo);
     ch = getchar();
     resetTermios();
     return ch;
}


char getch()
{
    return getch_(0);
}

char getche()
{
    getch_(1);
}
-----------------------------------------------
file : main.c :-
----------------
#include <stdio.h>
#include <stdlib.h>
#include "getche-getch.h"


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 :-
---------
[rahul@C-Client Debug]$ ./getch-getche-c
(getche example) please type a letter: w
You typed: w
(getch example) please type a letter...
You typed: w
[rahul@C-Client Debug]$
*/

----------------------------------------------------


No comments:

Post a Comment