我目前正在windows中用C++编写一个游戏。到目前为止,一切都很好,但我的菜单看起来像这样:
1.向北走
2.往南走
3.向东走
4.向北走
5.Inventory
6.Exit
插入选项-
它工作得很好,但我已经用了一段时间了,我更希望你能用上下箭头导航。我该怎么做呢?
提前致以问候
发布于 2012-02-17 18:48:09
您是否考虑过使用控制台UI库,如ncurses
发布于 2012-02-17 18:26:14
在Windows中,您可以使用通用的kbhit()函数。此函数根据是否按下键盘返回true/false。然后,您可以使用getch()函数读取缓冲区中存在的内容。
while(!kbhit()); // wait for input
c=getch(); // read input你也可以看看扫描码。conio.h包含所需的签名。
发布于 2017-07-17 00:28:39
您可以使用GetAsyncKeyState。它允许您从箭头、功能按钮(F0、F1等)和其他按钮获得直接键盘输入。
下面是一个示例实现:
// Needed for these functions
#define _WIN32_WINNT 0x0500
#include "windows.h"
#include "winuser.h"
#include "wincon.h"
int getkey() {
while(true) {
// This checks if the window is focused
if(GetForegroundWindow() != GetConsoleWindow())
continue;
for (int i = 1; i < 255; ++i) {
// The bitwise and selects the function behavior (look at doc)
if(GetAsyncKeyState(i) & 0x07)
return i;
}
Sleep(250);
}
}https://stackoverflow.com/questions/9326364
复制相似问题