C++ keypress: getch, cin.get?

前端 未结 6 1105
逝去的感伤
逝去的感伤 2020-12-15 16:48

I have a Win32 program that runs on a loop. I would like to be able to pause that program while awaiting a keypress. It doesn\'t matter whether I use \'any key\' or a specif

6条回答
  •  时光说笑
    2020-12-15 16:48

    HWND hwnd = ::GetConsoleWindow();
    
    while (!((::GetForegroundWindow() == hwnd) &&
            ((::GetKeyState(VK_SPACE) & 0x8000) != 0)))
        ::Sleep(0);
    

    Suppose it is not the best way but it solved my problem. Replace VK_SPACE with any other value you like. And it is not portable.

提交回复
热议问题