1

I have a procedure that when a user press Ctrl button and right click it will show a message box to screen. But it has a loop, I only press Ctrl button and right click once time but it shows a sequence of message box. How to fix this? https://youtu.be/LzI9M_zEEKQ

This is my MouseProc procedure

    #define EXPORT __declspec(dllexport)
    unsigned char  KeyState[256];

    LRESULT EXPORT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if (nCode < 0)
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        GetKeyboardState(KeyState);

        if (nCode == HC_ACTION)
        {
            if ((wParam == WM_RBUTTONUP) && (KeyState[VK_CONTROL] & 0x80))
            {
                MessageBox(NULL, L"Ctrl + Right Click", L"Mouse hook", MB_OK);
            }
        }
        return CallNextHookEx(hHook, nCode, wParam, lParam);
    }

Thanks for reading.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Hieu Dinh
  • 692
  • 5
  • 18
  • 1
    It appears that your hook procedure receives a new message, when you dismiss the message box with the mouse. Do you see the same effect, when dismissing the message box with the keyboard? It's generally a bad idea to place a blocking UI in a hook procedure. If you want to test your code in a less intrusive way, use [OutputDebugString](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362.aspx) in place of your `MessageBox` call. – IInspectable Oct 27 '15 at 16:22
  • It still appear when I dismiss the message box with the enter button. – Hieu Dinh Oct 27 '15 at 17:10
  • That message box repeats exactly 30 times. – Hieu Dinh Oct 27 '15 at 17:11
  • You are not getting the message. Stop using MessageBox, use OutputDebugString instead. – Hans Passant Oct 27 '15 at 17:18

1 Answers1

0

You shall not use MessageBox() in message hooks as it breaks normal message hook flow - MessageBox() runs its own modal loop.

If you need exactly MessageBox there then you should use PostMessage with custom message and handler. In this case MessageBox will be invoked after CallNextHookEx(hHook, nCode, wParam, lParam);

c-smile
  • 26,734
  • 7
  • 59
  • 86