CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2015
    Posts
    1

    Exclamation Mouse Input Coordinates Not Being Read in C++

    Please take a look the code below, its not the complete thing obviously since that'd be too long.

    Essentially the problem is that when I click the mouse, the program doesn't seem to record the coordinates. So the if statements are never executed.

    One thing I noticed was that when the do while loop is running and the left mouse button is not pressed then X and Y of dwMousePosition are both 0. But then if I press the left mouse button then the coordinates become x = 1 and y = 0.

    Any help is appreciated

    NOTE Im on Windows 7, 64 bit, using Visual Studio Express 2013 for Windows Desktop

    Code:
    #include <iostream>
    #include <windows.h>
    #include <string>
    #include <new>
    #include <stdio.h>
    
    using namespace std;    
    
    INPUT_RECORD ir[128];                                            
    DWORD nRead;                                                      
    COORD pos;
    UINT z;
    POINTER_INFO pointerInfo = {};
    POINT p;
    
    HANDLE hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);                  
    HANDLE handles[2] = { hEvent, hStdInput };
    
    FlushConsoleInputBuffer(hStdInput);
    
    do
    {
        Sleep(1);
        if (!wait_for_up)
        {
            if ((GetKeyState(VK_LBUTTON) & 0x100) != 0)
            {
                ReadConsoleInput(hStdInput, ir, 128, &nRead);
                pos.X = 0, pos.Y = 0;
                SetConsoleCursorPosition(hStdOutput, pos);
                if (*login == false)
                {
                    if ((ir[z].Event.MouseEvent.dwMousePosition.Y) == 0)
                    {
                        *choice = 1;
                        finished = true;
                    }
                    if ((ir[z].Event.MouseEvent.dwMousePosition.Y) == 4)
                    {
                        *choice = 2;
                        finished = true;
                    }
                }
            }
        }
    } while (!finished);
    
    cout << *choice;

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Mouse Input Coordinates Not Being Read in C++

    1) you need to enable mouse events.
    See SetConsoleMode()

    2) z, the index into the inputrecords buffer, isn't being initialized or set to anything.

    3) you are not checking if the event is in fact a mouse event.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured