How to cancel the "system key down" state in Windows

On Windows, when you press Alt and release it again, the menu bar is activated. However, when you press Alt, then press and then release, the menu bar is not activated because presumably you want to Alt + click and not activate the menu bar.

I want to support Alt + mouse changes (which will show in horizontal scrolling). This works great, but if you're ready and release the Alt key, the menu bar is activated. This is obviously not desirable. There must be some action that Windows does internally when you Alt + click to cancel the internal "ActivateMenuBarWhenAltIsReleased" flag. If I know what it is, I can do it myself when I get a mouse wheel message.

So, does anyone know how the system that activates the menu bar when you press and release Alt works, and how to undo this? Thank!

0


source to share


2 answers


When you release the Alt key, the system generates the message WM_SYSCOMMAND

/ SC_KEYMENU

. Moreover, if you do not press a key to open a specific popup menu, then lparam will be 0. DefWindowProc, upon receiving this message, will enter the menu loop. So, all you have to do is detect this message and prevent it from reaching DefWindowProc:



LRESULT CALLBACK MainWndProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (msg) {
        // ...
        case WM_SYSCOMMAND: {
            DWORD cmd = wparam & 0xfff0;
            // test for Alt release without any letter key pressed
            if (cmd == SC_KEYMENU && lparam == 0) {
                // don't let it reach DefWindowProc
                return 0;
            }
            break;
        }
        // ...
    }

    return DefWindowProc(wnd, msg, wparam, lparam);
}

      

+2


source


I think this (keybd_event function) will help you



0


source







All Articles