0

I'm making an console application. It starts with a menu where if I press the key; 1. The menu changes into another menu screen. But note, without me pressing 'Enter'. This means that my 1 still remains, which is obviously bad when stepping down further in the menus.

How do I clear the input command line?

The function im using.

if(GetAsyncKeyState('1'))
        {
            IEventDataPtr gameState(GCC_NEW EvtData_Set_Game_State("PREGAMESTATE"));
            em->VTriggerEvent(gameState);
//Enter line clearing code.
        }
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Alex
  • 365
  • 4
  • 17
  • See http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input – riklund Apr 20 '14 at 12:18
  • Ive added the section of the code it is used in. The cin.clear() as well as cin.ignore(10000,'\n'); did not help. – Alex Apr 20 '14 at 12:28

1 Answers1

0

The function GetAsyncKeyState gives you "the current state of a key". So it will return true between the point when the keyboard driver has received the "key for 1 has been pressed" until the keyboard driver receives "key for 1 has been released".

I would seriously suggest that you use ReadConsoleInput instead if you want to get one keypress at a time.

The alternative is to use something like this:

 while(GetAsyncKeyState('1'))
 {
     // Do nothing. 
 }

to wait for that key to be released.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227