• entries
122
121
• views
68495

# Keyboard input

286 views

With the help of some of the folks on IRC, I finally got some basic user input working. And it doesn't hog the CPU, either! I think the code speaks better than I can this time.

bool Game::Update(INPUT_RECORD* records, DWORD numEvents){    bool retval = false;    for (DWORD i = 0; i < numEvents; ++i)    {        if (records.EventType == KEY_EVENT)        {            if (records.Event.KeyEvent.bKeyDown)            {                switch (records.Event.KeyEvent.wVirtualKeyCode)                {                    case VK_UP:                        retval = true; y -= 1; break;                    case VK_DOWN:                        retval = true; y += 1; break;                    case VK_LEFT:                        retval = true; x -= 1; break;                    case VK_RIGHT:                        retval = true; x += 1; break;                }            }            keypress[records.Event.KeyEvent.wVirtualKeyCode] = (records.Event.KeyEvent.bKeyDown != 0);        }    }    return retval;}int Game::Run(){    Draw();    INPUT_RECORD* records = new INPUT_RECORD[1];    DWORD numEvents = 1;    bool redraw = false;    while (ReadConsoleInput(hIn, records, numEvents, &numEvents))    {        redraw = Update(records, numEvents);         if (keypress[VK_ESCAPE])            break;         if (redraw)        {            Draw();            redraw = false;        }        GetNumberOfConsoleInputEvents(hIn, &numEvents);        if (numEvents)        {            delete[] records;            records = new INPUT_RECORD[numEvents];        }    }    delete[] records;    return 0;}

It's very basic right now, but most of that is going to go into that Keyboard class I mentioned (currently not being used). While I was working this out, I was using a while (true) loop. That turned out to be a very bad idea, because when I opened up the Task Manager, Cripes was sucking up almost 20% of my CPU time. I figured this version out, luckily, which uses far less CPU time because ReadConsoleInput() blocks when there's no input in the buffer.

This code will definitely be changing, mostly as I move code into the Keyboard class and start utilizing it in Run(). For now I'm just glad I can kinda-sorta move the map around. :P

~Jonathan

I still don't understand how you think "while(true)" was your problem...

Well, just changing it from while (true) to while (GetConsoleInpt(...)) fixed the CPU problem, so I'm just going by logic and evidence.

EDIT: Here's the code I was using before.
while (true)
{
if (GetAsyncKeyState(VK_ESCAPE))
break;

GetNumberOfConsoleInputEvents(hIn, &numEvents);
if (numEvents != 0)
{
records = new INPUT_RECORD[numEvents];
for (DWORD i = 0; i < numEvents; ++i)
{
if (records[i].EventType == KEY_EVENT)
{
redraw = true;
keypress[records[i].Event.KeyEvent.wVirtualKeyCode] = (records[i].Event.KeyEvent.bKeyDown != 0);
}
}
}

if (redraw)
{
Draw();
redraw = false;
}
}



Looking over it again, I see that the problem was that I was continuously looping when there were no events (if (numEvents != 0) { /**/ }). I still think while (GetConsoleInput(...)) is better, and it's a bit harder to make mistakes like this.

## Create an account

Register a new account