1

can I know which Keyboard Key has been pressed before hitting Enter.is there any way to capture such key pressed event in c++ ??Please provide a short example of it. i'm using VC++ on Windows 32bit.

aj8080
  • 95
  • 1
  • 8
  • 1
    on what platform? Using which APIs? Otherwise you're going to get 2 dozen examples. – Doug T. Oct 03 '11 at 18:13
  • Sure. Record ALL keystrokes into a buffer, then look back into the buffer whenever you encounter an 'enter' event. – Marc B Oct 03 '11 at 18:15
  • C++ does not have keystrokes. – Lightness Races in Orbit Oct 03 '11 at 18:16
  • 1
    Look into **terminal handling**. On Linux, check out the `ncurses` library. – Kerrek SB Oct 03 '11 at 18:17
  • 7
    To the closers: even though YOU can't think of any answer, or what the question is really about, others probably can. For example, I would have had no problem answering this question. Instead of closing questions that you don't understand, close only those that you thoroughly understand, where you are sure that the question admits no useful answer. Or, simply don't touch that close button at all. Because you're not qualified, and it's not like you can train yourself to become better at it by closing questions willy-nilly. – Cheers and hth. - Alf Oct 03 '11 at 18:33
  • @AlfP.Steinbach sir will you please answer my question?? – aj8080 Oct 03 '11 at 18:38
  • 2
    @AlfP.Steinbach: Actually, What is interesting & at the same time concerning as well as disappointing is that none of the users who closed this as `Not a Real Q` bothered to add a reason as to why they feel this is not a real Q. – Alok Save Oct 03 '11 at 18:41

1 Answers1

3
// See <url: http://en.wikipedia.org/wiki/Conio.h>.
#include <iostream>
#include <conio.h>      // ! Non-standard, but de facto std. on Windows.

int main()
{
    using namespace std;

    for( ;; )
    {
        cout << "OK, should this program stop now..." << endl;
        cout << "Press Y for Yes or N for No: " << flush;

        for( bool answered = false; !answered; )
        {
            char const ch = getch();        // From [conio.h].
            switch( ch )
            {
            case 'y':
            case 'Y':
                cout << "<- Yes" << endl;      // Input echo.
                cout << "Bye!" << endl;
                return 0;

            case 'n':
            case 'N':
                cout << "<- No" << endl;      // Input echo.
                cout << endl;
                answered = true;

            default:
                ;
            }
        }
    }
}

For GUI programs is a bit different.

Note: you can also go all the way down to the Windows API if you want, but, I recommend taking one step at a time, exploring the conio.h functionality first.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • P Steinbach Thanks sir i am trying on it. actually I am bit new to c++ and trying to learn it.. thanks once again. – aj8080 Oct 03 '11 at 18:58
  • P Steinbach Sir i have not understood working of "for( bool answered = false; !answered; ) " loop it seems equivalent to infinite loop i.e while(1) [but i know its not ]..will you Explain this loop ?? – aj8080 Oct 03 '11 at 19:09
  • @aj8080: You need **[a C++ textbook](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)**. But in short, when the loop is entered the variable `answered` is set to `false`, and each time around, including the first, execution only proceeds with the loop body if `!answered`, i.e. if the user has *not* yet answered. When this *continuation condition* is false, execution jumps straight to an imaginary place right after the loop body; the loop is finished. – Cheers and hth. - Alf Oct 03 '11 at 19:14
  • P Steninbach : ohh.. now got it!sorry for such question ... one more thing i wanted to ask you is that will you recommend me best reference book for C++ ,currently i am using "c++ The complete reference "by Herbert Schield. ?? – aj8080 Oct 03 '11 at 19:20
  • Note that [getch is deprecated](http://msdn.microsoft.com/en-us/library/ms235446%28v=VS.100%29.aspx). – Benjamin Lindley Oct 03 '11 at 19:38
  • @Benjamin: the `getch` from `conio.h` has never been officially standard, and it is not Posix function. It's not easy to intuit what the Microsoft tech writer might have thought he or she was communicating. But we can safely conclude the he or she was mightily confused. – Cheers and hth. - Alf Oct 03 '11 at 22:43
  • @ah8080: the only completely general recommendation is to stay safely away from Herbert Schildt books. if you see one near you, set fire to it. try to click on the link above, the bold "a C++ textbook". – Cheers and hth. - Alf Oct 03 '11 at 22:44
  • @AlfP.Steinbach yea better I will. – aj8080 Oct 04 '11 at 05:24