Is there a less brutal way to simulate BlockInput()?

Hi forum!

I'm starting out in c++ after three courses in school, using Code::Blocks and working on a small project, a textbased game in cmd.

So I'm using code randomly found on different forums (this being a most helpful resource!) and I've found a function Sleep() in <windows.h> and combining it with
BlockInput() from <winable.h> to display a short intro of text.

To my question: Is there a less brutal way to simulate BlockInput() while Sleep() is called? My antivirus-program (Avira) is telling me my .exe is a malware of type TR/ATRAPS.Gen2 and similar problems appear on other computers.
Also it could be nice running the program without the mouse blocked if scrolling through some forum while running the game (since it's only occupying half the screen)

My idea was to reset or clear the cin streambuffer after Sleep(), but I haven't figured out how input really works, and how to implement that..
I also tried cin.sync() and cin.ignore(numeric_limits<streamsize>::max(), '\n')
but without the results I hoped for..

For summary I have a short code that I want to implement without using BlockInput() as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <windows.h>
#include <winable.h>
#include <conio.h>

using namespace std;

int get_key ()
{
    int ch = getch();

    if ( ch == 0 || ch == 224 )
        ch = 256 + getch();

    return ch;
}

int main()
{
     BlockInput(true);
     
     cout << "Showing a line for approx. 3 seconds" << endl;
     Sleep(3000);
     cout << "Showing another line for approx. 3 seconds" << endl;
     Sleep(3000);

     cout << "Press any key to continue!" << endl;
     BlockInput(false);
     get_key();
     
     cout << "Game start!" << endl;
     //some more code that asks for a name and some other variables

     return 0;
}


If the code above is executed and a key is pressed while displaying the two lines, nothing happens, but the mouse cursor and other input outside the cmd is frozen. If BlockInput() is commented then if a key is pressed during the "intro" the program will exit before you can se the "Press any key to continue!" line...

See my problem?
Last edited on
Dunno if this is along the lines of what you want, but:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <iostream>
#include <windows.h>

using namespace std;

void resume()
{
  HANDLE  stdIn = GetStdHandle(STD_INPUT_HANDLE);
  DWORD   saveMode;
  GetConsoleMode(stdIn, &saveMode);
  SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
  FlushConsoleInputBuffer(stdIn); // flush any chars in the buffer before waiting for keypress
  WaitForSingleObject(stdIn, INFINITE); // wait for keypress
  FlushConsoleInputBuffer(stdIn); // flush keypress
  SetConsoleMode(stdIn, saveMode);
}

int main()
{
     cout << "Showing a line for approx. 3 seconds" << endl;
     Sleep(3000);
     cout << "Showing another line for approx. 3 seconds" << endl;
     Sleep(3000);

     cout << "Press any key to continue!" << endl;
     resume();

     cout << "Game start!" << endl;
     //some more code that asks for a name and some other variables

     return 0;
}
Exactly! This does make things easier.

While I am on the subject, my best guess towards a soloution is in the following code.

I don't understand why this code isn't executed like the resume() code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

int get_key ()
{
    int ch = getch();

    if ( ch == 0 || ch == 224 )
        ch = 256 + getch();

    return ch;
}

int main()
{
     // save the cin streambuffer and assign it... Elsewhere?
     streambuf *psbuf, *backup;
     backup = cin.rdbuf();
     cin.rdbuf(psbuf);



     cout << "Showing a line for approx. 3 seconds" << endl;
     Sleep(3000);
     cout << "Showing another line for approx. 3 seconds" << endl;
     Sleep(3000);

     cout << "Press any key to continue!" << endl;

     //re-assign cin streambuffer
     cin.rdbuf(backup);

     while (cin.get()) cout << "cin.get()" << endl;  //*
     while (get_key()) cout << "getch()" << endl; //*

     cout << "Game start!" << endl;
     //some more code that asks for a name and some other variables

     return 0;
}


* Either of the while loops were used to try input, but it didn't work as I imagined.

cin.get() is just shut down, writes keys to the screen but doesn't return anything.
get_key() seems to not even be effected.

I guess that the resume() function works explicitly on windows, is there a similar way to implement this for mac?
I was messing around with many attempts to clear the input buffer before calling get_key() and was unsuccessful. cin.sync() and cin.ignore() didn't work whatsoever and if I hit keys while the Sleep() functions were executing I had the same behavior you did... it would just blaze through the "pause" altogether. I'm guessing there is a disconnect between getch() and the standard C++ input streams. Sorry also I know absolutely nothing about Mac programming =/

The Carbon API seems even more daunting than visiting MSDN with no idea where to start looking!
Last edited on
Topic archived. No new replies allowed.