DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Windows Console Mouse operations (http://www.daniweb.com/forums/thread160608.html)

Freaky_Chris Dec 3rd, 2008 2:57 pm
Windows Console Mouse operations
 
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

Salem Dec 3rd, 2008 3:27 pm
Re: Windows Console Mouse operations
 
> 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.

Freaky_Chris Dec 3rd, 2008 3:46 pm
Re: Windows Console Mouse operations
 
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


All times are GMT -4. The time now is 1:43 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC