开发者

can I know which Keyboard Key has been pressed before hitting Enter

can 开发者_如何学PythonI 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.


// 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.,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜