【发布时间】:2014-09-09 15:32:38
【问题描述】:
我目前正在使用 Windows 控制台制作平台游戏。但是,当我发出多个命令时,我遇到了一个问题。
例如,当我让我的角色向一个方向奔跑然后按下跳跃键时,忘记了正在按住的奔跑键这一事实,必须再次按下。
是否有可以检查哪些键被按住或按下的输入法?或者更确切地说,一种检查重复输入的方法?
作为参考,这是我当前的代码: http://pastebin.com/2y6Rf6rb
我目前使用的输入法是这样的:
/*** Keyboard Input ***/
/* Number of Events */
DWORD numEvents = 0;
/* Events from Console Read */
DWORD numEventsRead = 0;
/* Find Number of Console Events */
GetNumberOfConsoleInputEvents(rHnd, &numEvents);
/*If there is an Event */
if(numEvents != 0)
{
/* Create Buffer to Store Events */
INPUT_RECORD *eventBuffer = new INPUT_RECORD[numEvents];
/* Read Console Events to that Buffer and Save that many Events */
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
/* Cycle through Events that have Happened */
for(DWORD i = 0; i < numEventsRead; i++)
{
/* If Event was a Key Press */
if(eventBuffer[i].EventType == KEY_EVENT && eventBuffer[i].Event.KeyEvent.bKeyDown == TRUE)
{
/* Right ('d' Key) 0x44*/
if(eventBuffer[i].Event.KeyEvent.wVirtualKeyCode == 0x44)
{
key = 'd';
for(y = 0; y < 3; y ++)
{
if(gdisplay[PY - y][PX + 4] == square1[0] && gdisplay[PY - y][PX + 4 + 1] == square1[1])
{
movecheck = 0;
}
}
if(momentum < 0)
{
momentum++;
}
else if(movecheck == 1)
{
PX+=2;
momentum++;
if(panime == 1)
{
panime = 2;
}
else if(panime == 2)
{
panime = 1;
}
}
}
/* Left ('a' Key) 0x41*/
else if(eventBuffer[i].Event.KeyEvent.wVirtualKeyCode == 0x41)
{
key = 'a';
if((PX - 2) >= 2)
{
for(y = 0; y < 3; y ++)
{
if(gdisplay[PY - y][PX - 4] == square1[0] && gdisplay[PY - y][PX - 4 + 1] == square1[1])
{
movecheck = 0;
}
}
if(momentum > 0)
{
momentum--;
}
else if(movecheck == 1)
{
PX-=2;
momentum--;
if(panime == 1)
{
panime = 2;
}
else if(panime == 2)
{
panime = 1;
}
}
}
}
/* Jump (Spacebar) VK_SPACE*/
else if(eventBuffer[i].Event.KeyEvent.wVirtualKeyCode == VK_SPACE)
{
if(jump == 0)
{
jump++;
PY-=2;
}
}
}
/* Max Momentum */
if(momentum > 4)
{
momentum = 4;
}
if(momentum < -4)
{
momentum = -4;
}
rewind(stdin);
}
}
【问题讨论】: