Windows Console Mouse operations

Thread Solved
Reply

Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Windows Console Mouse operations

 
0
  #1
Dec 3rd, 2008
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.

  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. int main(void){
  7.  
  8. HANDLE stin = GetStdHandle(STD_INPUT_HANDLE);
  9. DWORD NumRead;
  10. DWORD numEvents;
  11.  
  12. while(1){
  13. GetNumberOfConsoleInputEvents(stin, &numEvents);
  14. INPUT_RECORD *inputStat = new INPUT_RECORD[numEvents];
  15. ReadConsoleInput(stin, inputStat, 1, &NumRead);
  16. if(NumRead != 0){
  17. for(int i = 0; i < NumRead; i++){
  18. if(inputStat[i].EventType==MOUSE_EVENT){
  19. if (inputStat[i].Event.MouseEvent.dwButtonState & 0x0001){
  20. std::cout << "Clicked\n";
  21. }
  22. }
  23. }
  24. delete inputStat;
  25. }
  26.  
  27. }
  28.  
  29. return 0;
  30. }

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Windows Console Mouse operations

 
1
  #2
Dec 3rd, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Windows Console Mouse operations

 
0
  #3
Dec 3rd, 2008
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC