【发布时间】:2017-05-11 20:15:25
【问题描述】:
这段代码没有做它应该做的事情。
当我按一次光标键时,菜单会跳转不止一次。
我该如何解决这个问题?
.
相关源代码
#include <windows.h>
#include <conio.h>
BOOL IsKeyPressed(KEY_CODE key)
{
if (GetAsyncKeyState(key) < 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
int select_menu(char**menus, int x, int y, int items_count)
{
int selection = 0;
show_menu(x, y, menus, items_count);
gotoxy(x, y);
while (1)
{
if (IsKeyPressed(vk_Down))
{
int xx = wherex();
int yy = wherey() + 1;
if ((yy - x + 1) > items_count)
{
gotoxy(x, y);
}
else
{
gotoxy(xx, yy);
}
}
if (IsKeyPressed(vk_Up))
{
int xx = wherex();
int yy = wherey() - 1;
if (yy < y)
{
gotoxy(x, y + items_count);
}
else
{
gotoxy(xx, yy);
}
}
if (IsKeyPressed(vk_Return))
{
int xx = wherex();
int yy = wherey();
selection = yy - y + 1;
}
}
return selection;
}
参考:
【问题讨论】:
-
您应该执行一次操作,然后等待按键被释放。或者添加一些超时。或者有一个检测transitions的功能。
-
GetAsyncKeyState() 返回 2 位信息。按键按下时设置高位,即您现在正在测试的那个。当您再次调用 IsKeyPressed() 时,它仍然处于关闭状态,除非您的手指快速点亮。自从您上次调用它以来,当键更改状态时设置低位,您希望知道该状态以避免键重复。
-
你为什么要开始投票?