Mouse events couldn't be captured.

Hello all,
I tried to run this code which records Mouse events but it seemed mouse events couldnt be captured on my computer, mouse clicks , scroll wheels, clicks...
Only Keyboard events were captured correctly, sometimes , after I hit Ctrl+F5 to run compiled prog , mouse events shows 1 and that's it.

I use C++ Express, hope you guys could help me.
Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    HANDLE hIn;
    HANDLE hOut;
    COORD KeyWhere;
    COORD MouseWhere;
    COORD EndWhere;
    bool Continue = TRUE;
    int KeyEvents = 0;
    int MouseEvents = 0;
    INPUT_RECORD InRec;
    DWORD NumRead;

    hIn = GetStdHandle(STD_INPUT_HANDLE);
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    cout << "Key Events   : " << endl;
    cout << "Mouse Events : " << flush;

    KeyWhere.X = 15;
    KeyWhere.Y = 0;
    MouseWhere.X = 15;
    MouseWhere.Y = 1;
    EndWhere.X = 0;
    EndWhere.Y = 3;

    while (Continue)
    {
        ReadConsoleInput(hIn,
                         &InRec,
                         1,
                         &NumRead);

        switch (InRec.EventType)
        {
        case KEY_EVENT:
            ++KeyEvents;
            SetConsoleCursorPosition(hOut,
                                     KeyWhere);
            cout << KeyEvents << flush;
            if (InRec.Event.KeyEvent.uChar.AsciiChar == 'x')
            {
                SetConsoleCursorPosition(hOut,
                                         EndWhere);
                cout << "Exiting..." << endl;
                Continue = FALSE;
            }
            break;

        case MOUSE_EVENT:
            ++MouseEvents;
            SetConsoleCursorPosition(hOut,
                                     MouseWhere);
            cout << MouseEvents << flush;
            break;
        }
    }

    return 0;
}
Please ask me for any information I can provide since I am so desperate now knowing why it doesnt work ..
I think you should call this: SetConsoleMode(hIn,ENABLE_MOUSE_INPUT);
somewhere before the loop
Close, but before the loop, and don't lose other modes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    ...
    DWORD consoleMode;

    if (!GetConsoleMode( hIn, &consoleMode ))
    {
        // not connected to a console!
    }
    SetConsoleMode( hIn, consoleMode | ENABLE_MOUSE_INPUT );

    while (Continue)
    {
        ...
    }

    SetConsoleMode( hIn, consoleMode );
    return 0;
    }


There is one other thing that MS neglects to warn you about. The "QuickEdit" mode option on your console must not be enabled. Yes, I know... stupid. I don't know how to disable it programmatically. (I'll figure it out someday though --probably long after it is obsolete. :-P )

To find out, choose "Properties" from the console's system menu (right-click the icon on the console titlebar) and examine the lower-right side of the "Options" tab.

As long as QuickEdit mode is enabled, your console applications will not receive mouse events. Alas.

Hope this helps.
Last edited on
Bravo !! , you helped me out , thanks alot Duoas !
Topic archived. No new replies allowed.