-2

I want to create my own custom method that works exactly the same as the getch() method. Either in C/C++.

Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52
  • Interesting and a worthwhile learning experience, but currently not a question. – user4581301 Jun 23 '16 at 00:11
  • There is no language C/C++. Only the different languages C and C++. C, for instance does not support _methods_. – too honest for this site Jun 23 '16 at 00:14
  • 1
    I have no idea how to get a character without getch() there is no function that gets a character and terminate. – Syed Qamar Abbas Jun 23 '16 at 00:16
  • 1
    @Olaf - this appears already to be understood. Why do you think the OP wrote _Either in C/C++_ (with the emphasis on the word either) if there existed a belief that this was a single language? – enhzflep Jun 23 '16 at 00:54
  • 1
    @enhzflep: Well, the title is more prominent and indicates it has not been understood. Reading the text, I still think OP does not understand they are different languages. Anyway, the question is too broad, this is not a coding service. Too bad there are users who don't enforce site-rules for a little potential reps. – too honest for this site Jun 23 '16 at 01:05
  • @Olaf - I appreciate your response, thanks. Yes indeed, I'm surprised that this has so few close votes. There's no mention of the platform used nor does the OP appear to understand the stunning difference that this will make to the answer. It's one thing to tickle the keyboard-controller on an x86 system and entirely another to coax key-strokes from (say) an Arduino or a RasPi. – enhzflep Jun 23 '16 at 01:09
  • 2
    Possible duplicate of [How to implement getch() function of C in Linux?](http://stackoverflow.com/questions/3276546/how-to-implement-getch-function-of-c-in-linux) – gsamaras Jun 23 '16 at 01:26

1 Answers1

0

Like this:

#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;
}

in , which I got from here.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Thank you sir.. :) – Syed Qamar Abbas Jun 23 '16 at 00:17
  • This is no coding service. You should have requested OP to show her own research effort. Also that does not run on all OSs. Also this actually copies just the code from another answer, which is at best bad behaviour. YOu should have dup-CVed instead. – too honest for this site Jun 23 '16 at 01:06
  • 1
    Someone deleted my comment @Olaf, I said that despite your general behavior, you are right this time, thank you. – gsamaras Jun 23 '16 at 01:31
  • 1
    @gsamaras: It was deleted for good reason. I flagged it as offending. Your comment would have been sufficient without the offending side-blow. Watch your manners. That comment is also borderline. – too honest for this site Jun 23 '16 at 02:24