It does not work LowLevelMouseProc.

After pressing any key on the keyboard in any active window, but the window ceases to be tracked mouse. What could be the problem?

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#pragma comment(lib, "user32.lib")
 
#include <windows.h>
#include <stdio.h>
 
HHOOK hLowlevelProc;
bool breaked = false;
unsigned char keys[2] = { VK_LWIN, VK_TAB };
 
void SendKey()
{
    INPUT input[4];
 
    for (int i = 0; i <= 1; i++)
    {
        input[i].type = INPUT_KEYBOARD;
        input[i].ki.wVk = keys[i];
        input[i].ki.dwFlags = 0;
    }
    SendInput(2, input, sizeof(INPUT));
 
    for (int i = 0; i <= 1; i++)
    {
        input[i].ki.dwFlags = KEYEVENTF_KEYUP;
    }
    SendInput(2, input, sizeof(INPUT));
}
 
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *) lParam;
 
    if (pMouseStruct != NULL)
    {
        if (pMouseStruct->pt.x <= 10 && pMouseStruct->pt.y <= 10 && breaked == false) {
            SetForegroundWindow(GetConsoleWindow());
            SendKey();
            breaked = true;
        }
        else if (pMouseStruct->pt.x > 10 && pMouseStruct->pt.y > 10 && breaked)
        {
            breaked = false;
        }
 
        printf("Mouse position X = %d  Mouse Position Y = %d\n", pMouseStruct->pt.x, pMouseStruct->pt.y);
    }
 
    return CallNextHookEx(hLowlevelProc, nCode, wParam, lParam);
}
 
void MessageLoop()
{
    MSG message;
    while (GetMessage(&message, NULL, 0, 0) > 0)
    {
        TranslateMessage(&message);
        DispatchMessage(&message);
    }
}
 
DWORD WINAPI MouseDetect(LPVOID lpParm)
{
    HINSTANCE hInstance = GetModuleHandle(NULL);
 
    if (!hInstance)
    {
        return 1;
    }
 
    hLowlevelProc = SetWindowsHookEx(
        WH_MOUSE_LL,
        (HOOKPROC)LowLevelMouseProc,
        hInstance,
        NULL
        );
 
    MessageLoop();
    UnhookWindowsHookEx(hLowlevelProc);
 
    return 0;
}
 
int main(int argc, char** argv)
{
    //ShowWindow(GetConsoleWindow(), SW_HIDE);
 
    HANDLE hThread;
    DWORD dwThread;
 
    hThread = CreateThread(
        NULL,
        NULL,
        (LPTHREAD_START_ROUTINE)MouseDetect,
        (LPVOID)argv[0],
        NULL,
        &dwThread
        );
 
    if (hThread)
    {
        return WaitForSingleObject(hThread, INFINITE);
    }
    else
    {
        return 1;
    }
 
}
Last edited on
I don't think I really understand what the question is though. But by trying your code out I think it's supposed to send the WINDOWS+TAB key once your cursor is at the top left of the screen? In that case it seems to work for me (Windows 10, 64-bit).

One thing I notices was this:

The value of lParam isn't a pointer to a MOUSEHOOKSTRUCT but a pointer to a MSLLHOOKSTRUCT. This should not really make a difference for your purpose though, since the layout of the first member (the x-y coordinates) is the same, but the other members are different. This could give unpredictable results when you try to use them.

But I think your actual problem is something also described on the MSDN page of SendInput. It contains this:

This function does not reset the keyboard's current state. Any keys that are already pressed when the function is called might interfere with the events that this function generates. To avoid this problem, check the keyboard's state with the GetAsyncKeyState function and correct as necessary.


I believe the "any keys that are pressed when the function is called might interfere" part is what's causing your problem. The MSDN article can be found here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx.
Unfortunately it's not SendInput. Even if his comment out all the same when changing the focus of the work program is interrupted and Renewable when focusing on it. The problem has not been solved.I have spent the past few days.
Can you describe exactly what effect you are trying to achieve dmamontov?
Topic archived. No new replies allowed.