0

I am editing a very old question of mine when I used to use turbo c++. So now I may not be able to explain exact behaviour of turboc++ compiler and hence will make some assumptions.

I know turbo c/c++ is not standard but am trying to achieve similar behaviour in g++ ubuntu.

Before I ask my question, it is necessary to quote the meaning of KeyDown, KeyPress and KeyUp event taken from one of the answers from Difference between the KeyDown Event, KeyPress Event and KeyUp Event in Visual Studio

The MSDN documentation states the order in which the three events occur fairly clearly:

Key events occur in the following order:

  1. KeyDown
  2. KeyPress
  3. KeyUp

KeyDown is raised as soon as the user presses a key on the keyboard, while they're still holding it down.

KeyPress is raised for character keys (unlike KeyDown and KeyUp, which are also raised for noncharacter keys) while the key is pressed. This is a "higher-level" event than either KeyDown or KeyUp, and as such, different data is available in the EventArgs.

KeyUp is raised after the user releases a key on the keyboard.

Generally, you should handle the KeyUp event in your application. Actions should not be initiated in the UI until after the user releases the key. And since KeyUp is a lower-level event than KeyPress, you'll always have plenty of information at your fingertips about the key that was pressed, and it will even work for handling non-character keys.

NOTE : My question has nothing to do with MSDN. It was quoted only to get familiar with the KeyDown KeyPress and KeyUp events.

Now, getch in turbo c/c++ does not need the key to be released. The execution of getch(); gets completed as soon as one touches/presses any keyboard key(most probably only alphanumeric keys(assumption)).


Now a number of questions like this(Detecting when a key is released) and many other focus on windows plateform and not on g++ linux environment.


This question C++ mutiple key input or key press/release events seems closest to my question but the question seems incomplete with information about neither the methodologies/code used to take input nor any error message(s).


Answer by Stephen Veiss with 29 up votes in the following question says that it is probably the closest equivalent.

https://stackoverflow.com/questions/1377403/alternative-function-in-iostream-h-for-getch-of-conio-h#:~:text=For%20getch()%2C%20int%20ch,getch%20does%20an%20unbuffered%20read.]

But it still needs the enter key to be pressed after typing input at the input terminal.


The first answer of the following question seems to correctly simulate getch() equivalent in g++(gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) ).

What is the equivalent to getch() & getche() in Linux?

#include<iostream>
using namespace std;
#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, &current); /* 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;
}

I have tested the above code. Touching any alphanumeric key completes execution of getch function but keystrokes of keys like CAPS_LOCK, SHIFT, CONTROL, ALT etc doesn't. Moreover, it forces us to use .h versions of header(stdio.h and termios.h), which I don't find commendable.


The following code

#include<iostream>
#include<ncurses.h>
using namespace std;

int main() {  
    cout<<"NIRBHAY KUMAR PANDEY"<<endl;    
    getch();  
    return 0;  
}

compiles(g++ filename.cpp -lncurses) and runs successfully in my (Linux Mint 18.2 Cinnamon 64-bit, g++(gcc-5.4.0)) but gives following compilation errors in geeksforgeeks and hakerrank online compilers.

IN GEEKSFORGEEKS : geeksforgeeks prog.cpp:2:20: fatal error: ncurses.h: No such file or directory.

IN HACKERRANK : hakerrank /cc1ZsEbF.o: In function main': solution.cc:7: undefined reference to stdscr' solution.cc:7: undefined reference to `wgetch' collect2: error: ld returned 1 exit status Exit Status 255


Now my question is "are there any standard functions in standard headers in (Linux, g++) for detecting - KeyDown KeyPress and KeyUp events of all keys(including control keys and any key combination) of the keyboard - at the input terminal while waiting for input from the user ?
If yes, then a complete full running program will be appreciated.
(Note : I have achieved all this almost 12 years ago very easily by reading let us C 6th edition on turbo c/c++ compiler. I can recall that there were concept of ascii code and scan code. The book C++ primer 5th edition by Stanley B. Lippman has nothing to say anything about ascii code and scan code)

  • 1
    Impossible to tell without seeing error messages or identifying the online compiler you're using. Perhaps some online compilers run on systems that don't have ncurses installed? – Keith Thompson Jan 08 '18 at 00:44
  • Your question does not state what kind of error you are receiving making it hard for someone to answer. Please read the So guidelines before posting. – sparkitny Jan 08 '18 at 00:47
  • geeksforgeeks prog.cpp:2:20: fatal error: ncurses.h: No such file or directory. hakerrank /cc1ZsEbF.o: In function `main': solution.cc:7: undefined reference to `stdscr' solution.cc:7: undefined reference to `wgetch' collect2: error: ld returned 1 exit status Exit Status 255 – NIRBHAY KUMAR PANDEY Jan 08 '18 at 00:53
  • Header not there implies library not installed on the compiler host. For `ncurses` to be useful in an online environment, it would have to implement a full terminal protocol in its output window: not a given. The ones you've tried apparently don't. – Gene Jan 08 '18 at 01:06
  • What are you doing? You are completely rewriting a 3 year old question completely. DON'T DO THAT. Just delete this one and make a completely new question. – JHBonarius Feb 27 '21 at 18:21
  • You want a solution for XWindows, native console, any GUI environment like QT, GTK, TK, ??? There is no general solution which fits for all. – Klaus Feb 27 '21 at 19:13
  • @JHBonarius I see this quoted messege "You have reached your question limit, sorry, we are no longer accepting questions from this account, see the Help Center to learn more" when I try to ask new question. That's why I need to improve my previous question(s) with 0 or 1 downvotes. https://stackoverflow.com/help/question-bans says "Deleting your questions will not help". – NIRBHAY KUMAR PANDEY Feb 27 '21 at 20:24
  • Please contact SO support then, because this is not making much sense... – JHBonarius Feb 28 '21 at 08:20

0 Answers0