I
I
Ivan Pavlenko2019-03-29 19:23:02
C++ / C#
Ivan Pavlenko, 2019-03-29 19:23:02

How to make a window ignore mouse clicks if the shift key is not pressed?

There is a window, something like a widget, hanging on top of the screen and showing a picture with transparency. It is necessary to somehow make it so that the clicks pass only with the shift pressed, and simple clicks are transmitted to the windows from below.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SerJook, 2019-03-29
@MrGobus

If we talk about Windows, then:
Create a window with extended styles WS_EX_LAYERED | WS_EX_TRANSPARENT|WS_EX_TOPMOST
Set the window's transparency:
Install the hook on the keyboard:
Keyboard event handling function (sample view):

the code
LRESULT CALLBACK KeyboardEvent(int nCode, WPARAM wParam, LPARAM lParam)
{
    if ((nCode == HC_ACTION))
    {
        KBDLLHOOKSTRUCT* hooked_key = (KBDLLHOOKSTRUCT*)lParam; 
        if (hooked_key->vkCode == VK_LSHIFT || hooked_key->vkCode == VK_RSHIFT) {
            bool shiftPressed = (wParam == WM_KEYDOWN);
            LONG oldStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
            if (shiftPressed) {
                SetWindowLong(hWnd, GWL_EXSTYLE, oldStyle & (~WS_EX_TRANSPARENT));
            }
            else {
                SetWindowLong(hWnd, GWL_EXSTYLE, oldStyle | WS_EX_TRANSPARENT);
            }
        }
    }
    return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}

It dynamically sets and clears the WS_EX_TRANSPARENT extended style.
On exit, remove the hook:
I don't know how to do without the hook, because the inactive window does not receive keyboard events.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question