【发布时间】:2016-02-12 19:54:14
【问题描述】:
尽管关于这个主题的同一个问题已经被问了很多次,但我找不到任何可行的解决方案。
我的简单程序有两个循环。在我输入凭据并按 Enter 键的那一刻。程序必须进入第一个循环。这样我就可以打印菜单并等待用户输入菜单选择。根据菜单选择,另一个循环必须激活并等待用户输入。
但这里的问题是,当我输入凭据并按 Enter 键时,它会打印菜单并且无需等待用户输入它会进入 1st menu 并显示项目内的第一个菜单。出现这个问题是因为WinAPI的Key state问题
if (GetAsyncKeyState(VR_RETURN) != 0) 将在这两种情况下激活。如果按键被按下。 or 键之前被按下过。
如何解决这个问题?
以下是我的访客登录功能
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();
}
}
//用户菜单
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);
}
}
(GetAsyncKeyState(VK_MENU)&0x8000) 尝试了许多变体,但没有一个有效。
【问题讨论】:
-
请提供MCVE。包含不相关代码的问题对未来的访问者既无用处,也不太可能得到答案。
标签: c++ winapi console-application