4

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;

        } 

    }
}
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Duyve
  • 100
  • 8
  • _... from my first real c++ program ..._ [tag:winapi] is inherently **not** [tag:c++]!! What about using c++ standards `std::cin`, `std::cout` to interact with console IO?? – πάντα ῥεῖ Mar 27 '14 at 00:34
  • 5
    There are indeed two notifications for a key press. One when it goes down, another one when it goes up. You are forgetting to check the [KEY_EVENT_RECORD.bKeyDown member](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684166%28v=vs.85%29.aspx). You are also forgetting to check the INPUT_RECORD.EventType, you get more than keyboard input from this winapi function. – Hans Passant Mar 27 '14 at 01:48
  • @πάνταῥεῖ This is but a section of my code, where I am using more winapi to manipulate the console in a way that is just easier to write and understand. As previously stated, I am a new programmer, so when I started this program "using namespace std;" was what I started and I will definitely fix it before this is finished. – Duyve Mar 27 '14 at 03:59
  • @HansPassant Thank you Hans for the actual advice. You saved me hours of searching. Its late now but Ill try implementing this tomorrow, I have no doubt it will work. – Duyve Mar 27 '14 at 04:04
  • 1
    @πάνταῥεῖ, there's nothing about the Windows API that precludes using C++. As for using C++ stream I/O for interacting with the console, I think the code in this question illustrates a very good reason for not doing that: The C++ standard library provides no facility for reading arrow keys or moving the cursor around the screen. – Rob Kennedy Mar 27 '14 at 20:06

1 Answers1

2

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.

Duyve
  • 100
  • 8