Skip to content

Commit b33bde1

Browse files
authored
Fix dispatching of system keys (#18444)
This fixes the bit check for key down and adds a few comments. Closes #18331 ## Validation Steps Performed Printing the resulting INPUT_RECORDs shows both key down and up events when pressing F7. Alt-Space now also works again.
1 parent c56fb1b commit b33bde1

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/cascadia/WindowsTerminal/WindowEmperor.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
387387
MSG msg{};
388388
while (GetMessageW(&msg, nullptr, 0, 0))
389389
{
390+
// This is `if (WM_KEYDOWN || WM_KEYUP || WM_SYSKEYDOWN || WM_SYSKEYUP)`.
391+
// It really doesn't need to be written that obtuse, but it at
392+
// least nicely mirrors our `keyDown = msg.message & 1` logic.
390393
// FYI: For the key-down/up messages the lowest bit indicates if it's up.
391394
if ((msg.message & ~1) == WM_KEYDOWN || (msg.message & ~1) == WM_SYSKEYDOWN)
392395
{
@@ -401,7 +404,7 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
401404
loggedInteraction = true;
402405
}
403406

404-
const bool keyDown = msg.message & 1;
407+
const bool keyDown = (msg.message & 1) == 0;
405408
if (
406409
// GH#638: The Xaml input stack doesn't allow an application to suppress the "caret browsing"
407410
// dialog experience triggered when you press F7. Official recommendation from the Xaml
@@ -448,15 +451,13 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
448451
__assume(false);
449452
}
450453

451-
void WindowEmperor::_dispatchSpecialKey(MSG& msg) const
454+
void WindowEmperor::_dispatchSpecialKey(const MSG& msg) const
452455
{
453-
const auto hwnd = msg.hwnd;
456+
// Each CoreInput window is a child of our IslandWindow.
457+
// We can figure out the IslandWindow HWND via GetAncestor().
458+
const auto hwnd = GetAncestor(msg.hwnd, GA_ROOT);
454459
AppHost* window = nullptr;
455460

456-
// Just in case someone has targed a specific HWND,
457-
// we'll try to dispatch it to the corresponding class.
458-
// Usually this will not find anything because under WinUI the hidden CoreInput
459-
// window is responsible for all input handling (for whatever reason).
460461
for (const auto& h : _windows)
461462
{
462463
const auto w = h->GetWindow();
@@ -467,6 +468,7 @@ void WindowEmperor::_dispatchSpecialKey(MSG& msg) const
467468
}
468469
}
469470

471+
// Fallback.
470472
if (!window)
471473
{
472474
window = _mostRecentWindow();
@@ -478,7 +480,7 @@ void WindowEmperor::_dispatchSpecialKey(MSG& msg) const
478480

479481
const auto vkey = gsl::narrow_cast<uint32_t>(msg.wParam);
480482
const auto scanCode = gsl::narrow_cast<uint8_t>(msg.lParam >> 16);
481-
const bool keyDown = msg.message & 1;
483+
const bool keyDown = (msg.message & 1) == 0;
482484
window->OnDirectKeyEvent(vkey, scanCode, keyDown);
483485
}
484486

src/cascadia/WindowsTerminal/WindowEmperor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class WindowEmperor
5050
AppHost* _mostRecentWindow() const noexcept;
5151
bool _summonWindow(const SummonWindowSelectionArgs& args) const;
5252
void _summonAllWindows() const;
53-
void _dispatchSpecialKey(MSG& msg) const;
53+
void _dispatchSpecialKey(const MSG& msg) const;
5454
void _dispatchCommandline(winrt::TerminalApp::CommandlineArgs args);
5555
safe_void_coroutine _dispatchCommandlineCurrentDesktop(winrt::TerminalApp::CommandlineArgs args);
5656
LRESULT _messageHandler(HWND window, UINT message, WPARAM wParam, LPARAM lParam) noexcept;

0 commit comments

Comments
 (0)