How to cancel RETURN keystroke with windows.h? Or set it to “not pressed”?
Nov 21, 2014 at 11:05pm UTC
I'm a beginner in C++, and I'm making a scrolling menu console application. Everything works as it's supposed to work, but the problem is that when I press the RETURN key, it registers it very quickly and it ends up registering 2 or 3 keystrokes, causing in to select the first option in the next menu.
I want to find a way to cancel a keystroke at the start of a function so those extra keystrokes are nulled out or canceled at the start of every function.
I have been searching in
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646314%28v=vs.85%29.aspx but i can't seem to find the proper function. I found that the function ReadConsoleInput might be helpful but I'm not sure how to use it properly in this situation.
here's a part of my code. Any help would be very helpful, thanks!
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
string Menu[3] = {"Start Game" , "Options" , "Exit" };
int pointer = 0;
while (true )
{
system("cls" );
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << "Main Menu\n\n" ;
for (int i = 0; i < 3; ++i)
{
if (i == pointer)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout << Menu[i] << endl;
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << Menu[i] << endl;
}
}
while (true )
{
if (GetAsyncKeyState(VK_UP) != 0)
{
pointer -= 1;
if (pointer == -1)
{
pointer = 2;
}
break ;
}
else if (GetAsyncKeyState(VK_DOWN) != 0)
{
pointer += 1;
if (pointer == 3)
{
pointer = 0;
}
break ;
}
else if (GetAsyncKeyState(VK_RETURN) != 0)
{
switch (pointer)
{
case 0:
{
startNewGame();
} break ;
case 1:
{
options();
} break ;
case 2:
{
return 0;
} break ;
}
break ;
}
}
Sleep(150);
}
return 0;
Topic archived. No new replies allowed.