question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

lowlevel_keyboard_event_data.h doesn't seem to swallow keyboard events

See original GitHub issue

Environment

Windows build number: 19041.172
PowerToys version: Master/v0.16.0
PowerToy module for which you are reporting the bug: Interface

Steps to reproduce

I’ve been playing around with the source code for FancyZones a lot recently. When adding keybindings the header file interface/lowlevel_keyboard_event_data.h is used to prevent keyboard events being sent to the OS. I don’t believe this functionality actually works. An example given to block Win+L in the file is:

virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override {
    if (wcscmp(name, ll_keyboard) == 0) {
      auto& event = *(reinterpret_cast<LowlevelKeyboardEvent*>(data));
      // The L key has vkCode of 0x4C
      if (event.wParam ==  WM_KEYDOWN && event.lParam->vkCode == 0x4C) {
        return 1;
      } else {
        return 0;
      }
    } else {
      return 0;
    }
  }

Testing this out by altering the code in FancyZones.cpp like thus:

IFACEMETHODIMP_(bool)
FancyZones::OnKeyDown(PKBDLLHOOKSTRUCT info) noexcept
{
    // Return true to swallow the keyboard event
    bool const shift = GetAsyncKeyState(VK_SHIFT) & 0x8000;
    bool const win = GetAsyncKeyState(VK_LWIN) & 0x8000;
    if (win && !shift)
    {
        if ((info->vkCode == VkKeyScan('l')) || (info->vkCode == VkKeyScan('h'))) // <-- h & l hotkeys like vim
        {
            if (m_settings->GetSettings()->overrideSnapHotkeys)
            {
                Trace::FancyZones::OnKeyDown(info->vkCode, win, ctrl, false /*inMoveSize*/);
                return OnSnapHotkey(info->vkCode);
            }
        }
    }
    if (m_dragEnabled && shift)
    {
        return true;
    }
    return false;
}

OnSnapHotkey has been updated accordingly.

Pressing Win+L locks the computer. The window does move accordingly and placing a breakpoint in the if statement shows that it does get hit. The keyboard event just isn’t being swallowed as it should.

Win+L is notoriously difficult to block, though I have found this affects other keys too, like Win+plus which is also not blocked and opens the magnifier.

Expected behavior

The defined key presses should not be passed to the OS.

Actual behavior

Key presses are being passed to the OS preventing them from being effectively rebound.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
arjunbalgovindcommented, Apr 6, 2020

I don’t have a proper resource about which key combinations can’t be blocked, apart from stack overflow answers and just trial and error. While working on the Keyboard Manager(#6) we have found that Win+L and Ctrl+Alt+Del can’t be suppressed. These are two that we are aware of right now. We will be looking into other shortcuts later, but most of the Win shortcuts that we have been testing with seem to get suppressed with low level hooks.

Shall I close the issue?

@crutkas I’ll defer that to you.

0reactions
enricogiorcommented, Apr 6, 2020

That code has been working consistently since version 0.11, for existing module it hasn’t caused any problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to determine which control is eating my keyboard events
The best idea would be to make a trace statement (add a breakpoint and right click, go to "When Hit") in your form's...
Read more >
KeyboardEvent - Web APIs | MDN
Note: KeyboardEvent events just indicate what interaction the user had with a key on the keyboard at a low level, providing no contextual ......
Read more >
Consult: space/enter keyEvent is swallowed by NVDA #7898
We've just found that this doesn't work correctly with NVDA, and it seems related to this issue. Basically NVDA is blocking the keyboard...
Read more >
boppreh/keyboard: Hook and simulate global ...
Hook and simulate global keyboard events on Windows and Linux. - GitHub - boppreh/keyboard: Hook and simulate global keyboard events on Windows and...
Read more >
Keyboard: keydown and keyup
The keydown events happens when a key is pressed down, and then keyup – when it's released.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found