mouse_event is used to emulate mouse events, not to detect them. If you want to be able to detect mouse clicks anywhere (even if your window doesn't have focus), you will have to use something like windows hooks. If your using the Windows API and just want to be able to detect a mouse click within your window, you can start with a Win32 Tutorial - then try and understand Windows Procedures. However, to do this in a console window, it's a little different, here's an example:
#include <windows.h> #include <iostream> int main() { HANDLE hStdin; DWORD cNumRead; INPUT_RECORD irInBuf[1]; if ( HANDLE(hStdin = GetStdHandle( STD_INPUT_HANDLE )) == INVALID_HANDLE_VALUE ) return 1; while ( true ) { if (! ReadConsoleInput( hStdin, irInBuf, 1, &cNumRead) ) return 1; for (DWORD i = 0; i < cNumRead; ++i) if ( irInBuf[i].EventType == MOUSE_EVENT && irInBuf[i].Event.MouseEvent.dwEventFlags != MOUSE_MOVED ) { if ( irInBuf[i].Event.MouseEvent.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED ) { std::cout << "Left Click.\n"; } } } return 0; }
Sorry for the uncommented code, but either way, I think that is a little advanced for your level of programming.
Hope this helps.
hi..
how can you "read" the mouse clicks if you are not focused in your console application window? I can read the position, but not the clicks.
Thank you