繁体   English   中英

我可以知道在按Enter之前按下了哪个键盘键

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

我可以知道在按Enter之前按下了哪个键盘键。是否有任何方法可以在c ++中捕获此类按下键的事件?请提供一个简短的示例。 我在Windows 32位上使用VC ++。

// 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:
                ;
            }
        }
    }
}

对于GUI程序来说有点不同。

注意:如果需要,您也可以一直使用Windows API,但是,我建议一次采取一个步骤,首先探索conio.h功能。

干杯&hth。,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM