Hi all, i'm looking into doing a little bit of work with the mouse in console applications on windows. I'm using windows.h to get access to functions such as ReadConsoleInput. I've created a simple program that just prints a string whenever the left most mouse button is clicked. However sometimes it works fine sometimes it doesn't. By doesn't work it seems to be that when i run it if my curser is out side the console window then when i move my mouse into the console window it crashes. If it is inside to begin with then i seems to be ok....quite odd.

I was wondering if you could shed some light on where i have gone wrong thanks.

#include <iostream>
#include <windows.h>

using namespace std;

int main(void){
    
    HANDLE stin = GetStdHandle(STD_INPUT_HANDLE);
    DWORD NumRead;
    DWORD numEvents;
    
    while(1){
              GetNumberOfConsoleInputEvents(stin, &numEvents);
              INPUT_RECORD *inputStat = new INPUT_RECORD[numEvents];
              ReadConsoleInput(stin, inputStat, 1, &NumRead);
              if(NumRead != 0){
                 for(int i = 0; i < NumRead; i++){
                    if(inputStat[i].EventType==MOUSE_EVENT){
                       if (inputStat[i].Event.MouseEvent.dwButtonState & 0x0001){
                             std::cout << "Clicked\n";
                             }
                    }
                 }
                 delete inputStat;
              }
              
         }
    
    return 0;
}

Chris

> ReadConsoleInput(stin, inputStat, 1, &NumRead);
How about
ReadConsoleInput(stin, inputStat, numEvents, &NumRead);

> delete inputStat;
How about
delete [ ] inputStat;

You should also check the console API calls for success/failure as well.

commented: Thanks +1

How silly of me, Ye i was just tyrying to get it working before i set up failure checks, thanks for that. It was 2 silly mistakes. It's because i originally had it set up not using an array just to process one event and din't think to update those bits lol

Thanks,
Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.