Question

Okay, so to start off the topic I want to say that I am relatively new to coding. That being said I apologize if the question is too vague to understand.

The following code is from my first real c++ program I am making for Win32. The function playerMove moves the players icon to a new spot on the screen, while replacing the old spot with an X.

I'm not sure why but whenever I press any of the keys to cause a movement it does the action twice. I'm not sure if is the way I'm reading the input buffer or what, but I was wondering if there was a simple explanation I just missed. I included the FlushConsoleInputBuffer at the end of each case in case that was the problem, but that did not seem to fix it.

void playerMove()
{

    HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
    DWORD NumInputs = 0;
    DWORD InputsRead = 0;
    bool running = true;

    INPUT_RECORD irInput;

    GetNumberOfConsoleInputEvents(hInput, &NumInputs);

    while(running)
    {
        ReadConsoleInput(hInput, &irInput, 1, &InputsRead);
        //std::cout << irInput.Event.KeyEvent.wVirtualKeyCode << std::endl;

            HANDLE hStdout; 
            CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
            COORD coordDest; 

        switch(irInput.Event.KeyEvent.wVirtualKeyCode)
        {
            case M_KEY:
                FlushConsoleInputBuffer(hInput);
                displayMenu();                
                //Opens the menu
            break;

            case VK_LEFT:

            hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
            coordDest.X=xcoord;
            coordDest.Y=ycoord;
            SetConsoleCursorPosition(hStdout, coordDest);
            cout<<'X';
            coordDest.X=(xcoord-1);
            SetConsoleCursorPosition(hStdout, coordDest);
            SetColor(11);
            cout<<'@';
            SetColor(7);
            xcoord--;
            FlushConsoleInputBuffer(hInput);
            // move it left
            break;

        case VK_UP:

            hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
            coordDest.X=xcoord;
            coordDest.Y=ycoord;
            SetConsoleCursorPosition(hStdout, coordDest);
            cout<<'X';
            coordDest.Y=(ycoord-1);
            SetConsoleCursorPosition(hStdout, coordDest);
            SetColor(11);
            cout<<'@';
            SetColor(7);
            ycoord--;
            FlushConsoleInputBuffer(hInput);
            // move it up
            break;
        case VK_RIGHT:

            hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
            coordDest.X=xcoord;
            coordDest.Y=ycoord;
            SetConsoleCursorPosition(hStdout, coordDest);
            cout<<'X';
            coordDest.X=(xcoord+1);
            SetConsoleCursorPosition(hStdout, coordDest);
            SetColor(11);
            cout<<'@';
            SetColor(7);
            xcoord++;
            FlushConsoleInputBuffer(hInput);
            // move it right
            break;

        case VK_DOWN:

            hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
            coordDest.X=xcoord;
            coordDest.Y=ycoord;
            SetConsoleCursorPosition(hStdout, coordDest);
            cout<<'X';
            coordDest.Y=(ycoord+1);
            SetConsoleCursorPosition(hStdout, coordDest);
            SetColor(11);
            cout<<'@';
            SetColor(7);
            ycoord++;
            FlushConsoleInputBuffer(hInput);
            // move it down
            break;

        } 

    }
}
Was it helpful?

Solution

By adding a if statement to check if a key is pressed (bKeyDown) before the switch statement it only catches the key as it is depressed by first checking if a key is down. So this question can be considered as answered.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top