簡體   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