Home > Enterprise >  How to pause my console on the desired key
How to pause my console on the desired key

Time:01-27

I want to pause my program on the 0x32, 0x33 and 0x34 key and make it work again on the 0x31 key, how can I? I used this code to pause on the 0x32 key, it's working, but I can't get it back to work on the desired key

if (GetAsyncKeyState(0x32))
        {
            getchar();
        }

CodePudding user response:

To summarize what @user4581301 suggested:

#include <conio.h>
...

if (GetAsyncKeyState(0x32) || GetAsyncKeyState(0x33) || GetAsyncKeyState(0x34)) {
    while (_getch() != 0x31)
        ;
}

CodePudding user response:

If your application is a console application, I implemented the function you want through loop. If it's a desktop application, you can refer to my code logic.

#include <iostream>
#include<cstdlib>
#include<time.h>
#include<Windows.h>
#include<conio.h>

using namespace std;
int main()
{
int i = 0 , c;
bool stateFlag = true;
while(true)
{
    cout << i << endl;
    Sleep(500);
    if (_kbhit())//Checks if there is currently keyboard input and returns a non-zero value if there is, or 0 otherwise
    {
        int ch = _getch(); 
        if (ch == 0x32|| ch == 0x33|| ch == 0x34)
        {
            c=_getch();//It waits for input and pauses the program
            stateFlag = false;
        }
        while (true)
        {
            if (_kbhit())
            {
                int ch = _getch();
                if (ch == 0x31) break;
            }

        }
        fflush(stdin);//Clear the input buffer
    }
    i  ;
}
return 0;
}
  •  Tags:  
  • Related