Although the same question on this topic has been asked tons of time, I couldn't find any working solution.
My simple program has two loops. The moment I enter credentials and I press enter. the program has to enter into 1st loop. so that I can print menus and wait for user input for menu selection. Based up on menu selection another loop has to activate and waits for user input.
But the problem here though the moment I enter credentials and press enter, It prints menu and without waiting for user input It goes into 1st menu and shows the 1st menu inside items. This problem occurs due to the Key state problem of WinAPI
if (GetAsyncKeyState(VR_RETURN) != 0)
will activate in both scenarios. If the key is pressed. or key has been pressed before.
How to overcome this problem?
Following is my guest login function
void guestLogin()
{
setColor(BASECOLOR);
clear();
string screen_lable = "GUEST LOGIN";
gotoxy(getCenterCoordinatesForText(screen_lable), 2);
cout << screen_lable << endl;
gotoxy(getCenterCoordinatesForText(screen_lable), 3);
for (int i = 0; i < screen_lable.length(); i++)
{
cout << "=";
}
screen_lable = "PLEASE ENTER YOUR NAME: ";
gotoxy(getCenterCoordinatesForText(screen_lable), 5);
gotoxy(getCenterCoordinatesForText(screen_lable), 7);
getline(cin, username);
//When user presses enter this will take username and proceed to show menus for the user and it should not go ahead and and select 1st menu and show the content.
if (username.length() != 0)
{
OptionsList();
}
}
//Menus for the user
void OptionsList()
{
menuIndex = 0;
while (true)
{
setColor(BASECOLOR);
clear();
string appName = "BEST FOOD INFORMATION SYSTEM";
string menuBtn = "MENUS";
string catering = "CATERING";
string services = "SERVICES";
string helpBtn = "HELP";
string logoutBtn = "LOGOUT";
gotoxy(getCenterCoordinatesForText(appName), 2);
cout << appName << endl;
/*if (UserRole::Customer)
{
gotoxy(getCenterCoordinatesForText(menuBtn + " || " + catering + " || " + helpBtn + " || " + logoutBtn), 5);
}*/
//else if (UserRole::Administrator){
gotoxy(getCenterCoordinatesForText(menuBtn + " || " + catering +" || " + services + " || " + helpBtn + " || " + logoutBtn), 5);
//}
//Updating Screen Text State Based On Key Selection
if (menuIndex == 0)
{
setColor(INVERTED_BASECOLOR);
cout << menuBtn;
setColor(BASECOLOR);
cout << " || ";
cout << catering;
cout << " || ";
cout << services;
cout << " || ";
cout << helpBtn;
cout << " || ";
cout<< logoutBtn;
}
else if (menuIndex == 1)
{
setColor(BASECOLOR);
cout << menuBtn;
cout << " || ";
setColor(INVERTED_BASECOLOR);
cout << catering;
setColor(BASECOLOR);
cout << " || ";
cout << services;
cout << " || ";
cout << helpBtn;
cout << " || ";
cout << logoutBtn;
}
else if (menuIndex == 2)
{
setColor(BASECOLOR);
cout << menuBtn;
cout << " || ";
cout << catering;
cout << " || ";
setColor(INVERTED_BASECOLOR);
cout << services;
setColor(BASECOLOR);
cout << " || ";
cout << helpBtn;
cout << " || ";
cout << logoutBtn;
}
else if (menuIndex == 3)
{
setColor(BASECOLOR);
cout << menuBtn;
cout << " || ";
cout << catering;
cout << " || ";
cout << services;
cout << " || ";
setColor(INVERTED_BASECOLOR);
cout << helpBtn;
setColor(BASECOLOR);
cout << " || ";
cout << logoutBtn;
}
else if (menuIndex == 4)
{
setColor(BASECOLOR);
cout << menuBtn;
cout << " || ";
cout << catering;
cout << " || ";
cout << services;
cout << " || ";
cout << helpBtn;
cout << " || ";
setColor(INVERTED_BASECOLOR);
cout << logoutBtn;
}
//Getting Key Events
while (true)
{
if (GetAsyncKeyState(VK_RIGHT) != 0)
{
menuIndex += 1;
if (menuIndex == 5)
{
menuIndex = 4;
}
break;
}
else if (GetAsyncKeyState(VK_LEFT) != 0)
{
menuIndex -= 1;
if (menuIndex == -1)
{
menuIndex = 0;
}
break;
}
// Here is the issue occours
else if (GetAsyncKeyState(VK_RETURN) != 0)
{
switch (menuIndex)
{
case 0:
{
//Previously pressed ENTER Key Stroke evokes this function as the GetAsyncKeyState() returns the pressed state.
FoodsNBeverages();
} break;
case 1:
{
Catering();
} break;
case 2:
{
Services();
} break;
case 3:
{
Help();
} break;
case 4:
{
Logoff();
} break;
}
}
}
Sleep(200);
}
}
Many variations have been tried (GetAsyncKeyState(VK_MENU)&0x8000)
but none of them are working.
答案 0 :(得分:1)
To test if the key is down you need to test that the return values has MSB set. This is explained in the documentation.
If the most significant bit is set, the key is down.
So replace
numpy.vectorize
with
GetAsyncKeyState(VK_RETURN) != 0
答案 1 :(得分:-2)
You have to use the GetAsyncKeyState() function constantly to check if the key is pressed. Then, the function will return 0 when the key is not being pressed although the key have been pressed before.