Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dispatching of system keys #18444

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/cascadia/WindowsTerminal/WindowEmperor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
MSG msg{};
while (GetMessageW(&msg, nullptr, 0, 0))
{
// This is `if (WM_KEYDOWN || WM_KEYUP || WM_SYSKEYDOWN || WM_SYSKEYUP)`.
// It really doesn't need to be written that obtuse, but it at
// least nicely mirrors our `keyDown = msg.message & 1` logic.
// FYI: For the key-down/up messages the lowest bit indicates if it's up.
if ((msg.message & ~1) == WM_KEYDOWN || (msg.message & ~1) == WM_SYSKEYDOWN)
{
Expand All @@ -401,7 +404,7 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
loggedInteraction = true;
}

const bool keyDown = msg.message & 1;
const bool keyDown = (msg.message & 1) == 0;
if (
// GH#638: The Xaml input stack doesn't allow an application to suppress the "caret browsing"
// dialog experience triggered when you press F7. Official recommendation from the Xaml
Expand Down Expand Up @@ -438,15 +441,13 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
__assume(false);
}

void WindowEmperor::_dispatchSpecialKey(MSG& msg) const
void WindowEmperor::_dispatchSpecialKey(const MSG& msg) const
{
const auto hwnd = msg.hwnd;
// Each CoreInput window is a child of our IslandWindow.
// We can figure out the IslandWindow HWND via GetAncestor().
const auto hwnd = GetAncestor(msg.hwnd, GA_ROOT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, which one did this fix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can consider this an extra. 😄 When I added this code you asked if we can't figure out the IslandWindow from a CoreInput target and I said no. Turns out we can.

AppHost* window = nullptr;

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

// Fallback.
if (!window)
{
window = _mostRecentWindow();
Expand All @@ -468,7 +470,7 @@ void WindowEmperor::_dispatchSpecialKey(MSG& msg) const

const auto vkey = gsl::narrow_cast<uint32_t>(msg.wParam);
const auto scanCode = gsl::narrow_cast<uint8_t>(msg.lParam >> 16);
const bool keyDown = msg.message & 1;
const bool keyDown = (msg.message & 1) == 0;
window->OnDirectKeyEvent(vkey, scanCode, keyDown);
}

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/WindowsTerminal/WindowEmperor.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class WindowEmperor
AppHost* _mostRecentWindow() const noexcept;
bool _summonWindow(const SummonWindowSelectionArgs& args) const;
void _summonAllWindows() const;
void _dispatchSpecialKey(MSG& msg) const;
void _dispatchSpecialKey(const MSG& msg) const;
void _dispatchCommandline(winrt::TerminalApp::CommandlineArgs args);
safe_void_coroutine _dispatchCommandlineCurrentDesktop(winrt::TerminalApp::CommandlineArgs args);
LRESULT _messageHandler(HWND window, UINT message, WPARAM wParam, LPARAM lParam) noexcept;
Expand Down
Loading