From aecd99e0ca3275e1a02e782ee2ff815a93248c6c Mon Sep 17 00:00:00 2001 From: "Dustin L. Howett" Date: Fri, 14 Aug 2020 16:44:39 -0700 Subject: [PATCH] Pass the scancode in our tunneled DirectKey event (#7298) #7145 introduced a check so that we wouldn't dispatch keys unless they actually had a scancode. Our synthetic events actually _didn't_ have scancodes. Not because they couldn't--just because they didn't. Fixes #7297 --- src/cascadia/TerminalApp/AppLogic.cpp | 4 ++-- src/cascadia/TerminalApp/AppLogic.h | 2 +- src/cascadia/TerminalApp/IDirectKeyListener.idl | 4 ++-- src/cascadia/TerminalControl/TermControl.cpp | 8 +++----- src/cascadia/TerminalControl/TermControl.h | 2 +- src/cascadia/TerminalControl/TermControl.idl | 4 ++-- src/cascadia/WindowsTerminal/AppHost.cpp | 4 ++-- src/cascadia/WindowsTerminal/AppHost.h | 2 +- src/cascadia/WindowsTerminal/main.cpp | 4 ++-- 9 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/cascadia/TerminalApp/AppLogic.cpp b/src/cascadia/TerminalApp/AppLogic.cpp index b11c1e2a1fd..148898cd94f 100644 --- a/src/cascadia/TerminalApp/AppLogic.cpp +++ b/src/cascadia/TerminalApp/AppLogic.cpp @@ -917,7 +917,7 @@ namespace winrt::TerminalApp::implementation // - Implements the Alt handler (per GH#6421) // Return value: // - whether the key was handled - bool AppLogic::OnDirectKeyEvent(const uint32_t vkey, const bool down) + bool AppLogic::OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down) { if (_root) { @@ -928,7 +928,7 @@ namespace winrt::TerminalApp::implementation { if (auto keyListener{ focusedObject.try_as() }) { - if (keyListener.OnDirectKeyEvent(vkey, down)) + if (keyListener.OnDirectKeyEvent(vkey, scanCode, down)) { return true; } diff --git a/src/cascadia/TerminalApp/AppLogic.h b/src/cascadia/TerminalApp/AppLogic.h index de2b225f81f..c0c75952b1a 100644 --- a/src/cascadia/TerminalApp/AppLogic.h +++ b/src/cascadia/TerminalApp/AppLogic.h @@ -48,7 +48,7 @@ namespace winrt::TerminalApp::implementation hstring Title(); void TitlebarClicked(); - bool OnDirectKeyEvent(const uint32_t vkey, const bool down); + bool OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down); void WindowCloseButtonClicked(); diff --git a/src/cascadia/TerminalApp/IDirectKeyListener.idl b/src/cascadia/TerminalApp/IDirectKeyListener.idl index 4fb5dea1bc0..06df1a5d3bd 100644 --- a/src/cascadia/TerminalApp/IDirectKeyListener.idl +++ b/src/cascadia/TerminalApp/IDirectKeyListener.idl @@ -8,7 +8,7 @@ namespace TerminalApp // If you update this one, please update the one in TerminalControl\TermControl.idl // If you change this interface, please update the guid. // If you press F7 or Alt and get a runtime error, go make sure both copies are the same. - [uuid("339e1a87-5315-4da6-96f0-565549b6472b")] interface IDirectKeyListener { - Boolean OnDirectKeyEvent(UInt32 vkey, Boolean down); + [uuid("0ddf4edc-3fda-4dee-97ca-a417ee3dd510")] interface IDirectKeyListener { + Boolean OnDirectKeyEvent(UInt32 vkey, UInt8 scanCode, Boolean down); } } diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 058e478bc72..b493054c6a1 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -763,7 +763,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation // normally. Namely, the keys we're concerned with are F7 down and Alt up. // Return value: // - Whether the key was handled. - bool TermControl::OnDirectKeyEvent(const uint32_t vkey, const bool down) + bool TermControl::OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down) { const auto modifiers{ _GetPressedModifierKeys() }; auto handled = false; @@ -771,9 +771,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation { // Manually generate an Alt KeyUp event into the key bindings or terminal. // This is required as part of GH#6421. - // GH#6513 - make sure to set the scancode too, otherwise conpty - // will think this is a NUL - (void)_TrySendKeyEvent(VK_MENU, LOWORD(MapVirtualKeyW(VK_MENU, MAPVK_VK_TO_VSC)), modifiers, false); + (void)_TrySendKeyEvent(VK_MENU, scanCode, modifiers, false); handled = true; } else if (vkey == VK_F7 && down) @@ -795,7 +793,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation if (!handled) { // _TrySendKeyEvent pretends it didn't handle F7 for some unknown reason. - (void)_TrySendKeyEvent(VK_F7, 0, modifiers, true); + (void)_TrySendKeyEvent(VK_F7, scanCode, modifiers, true); // GH#6438: Note that we're _not_ sending the key up here - that'll // get passed through XAML to our KeyUp handler normally. handled = true; diff --git a/src/cascadia/TerminalControl/TermControl.h b/src/cascadia/TerminalControl/TermControl.h index b34ea7b813c..e62f7fe25e5 100644 --- a/src/cascadia/TerminalControl/TermControl.h +++ b/src/cascadia/TerminalControl/TermControl.h @@ -91,7 +91,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation void CreateSearchBoxControl(); - bool OnDirectKeyEvent(const uint32_t vkey, const bool down); + bool OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down); bool OnMouseWheel(const Windows::Foundation::Point location, const int32_t delta, const bool leftButtonDown, const bool midButtonDown, const bool rightButtonDown); diff --git a/src/cascadia/TerminalControl/TermControl.idl b/src/cascadia/TerminalControl/TermControl.idl index 4e3eefa0c44..66d64d74265 100644 --- a/src/cascadia/TerminalControl/TermControl.idl +++ b/src/cascadia/TerminalControl/TermControl.idl @@ -15,8 +15,8 @@ namespace Microsoft.Terminal.TerminalControl // If you update this one, please update TerminalApp\IDirectKeyListener.idl. // If you change this interface, please update the guid. // If you press F7 or Alt and get a runtime error, go make sure both copies are the same. - [uuid("339e1a87-5315-4da6-96f0-565549b6472b")] interface IDirectKeyListener { - Boolean OnDirectKeyEvent(UInt32 vkey, Boolean down); + [uuid("0ddf4edc-3fda-4dee-97ca-a417ee3dd510")] interface IDirectKeyListener { + Boolean OnDirectKeyEvent(UInt32 vkey, UInt8 scanCode, Boolean down); } runtimeclass CopyToClipboardEventArgs diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp index 0590243e6eb..0ae788956a5 100644 --- a/src/cascadia/WindowsTerminal/AppHost.cpp +++ b/src/cascadia/WindowsTerminal/AppHost.cpp @@ -69,11 +69,11 @@ AppHost::~AppHost() _app = nullptr; } -bool AppHost::OnDirectKeyEvent(const uint32_t vkey, const bool down) +bool AppHost::OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down) { if (_logic) { - return _logic.OnDirectKeyEvent(vkey, down); + return _logic.OnDirectKeyEvent(vkey, scanCode, down); } return false; } diff --git a/src/cascadia/WindowsTerminal/AppHost.h b/src/cascadia/WindowsTerminal/AppHost.h index d2b6dd77c69..d99fddadb92 100644 --- a/src/cascadia/WindowsTerminal/AppHost.h +++ b/src/cascadia/WindowsTerminal/AppHost.h @@ -16,7 +16,7 @@ class AppHost void AppTitleChanged(const winrt::Windows::Foundation::IInspectable& sender, winrt::hstring newTitle); void LastTabClosed(const winrt::Windows::Foundation::IInspectable& sender, const winrt::TerminalApp::LastTabClosedEventArgs& args); void Initialize(); - bool OnDirectKeyEvent(const uint32_t vkey, const bool down); + bool OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down); private: bool _useNonClientArea; diff --git a/src/cascadia/WindowsTerminal/main.cpp b/src/cascadia/WindowsTerminal/main.cpp index 69663349b8c..63d007a9800 100644 --- a/src/cascadia/WindowsTerminal/main.cpp +++ b/src/cascadia/WindowsTerminal/main.cpp @@ -141,7 +141,7 @@ int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) // been handled we can discard the message before we even translate it. if (_messageIsF7Keypress(message)) { - if (host.OnDirectKeyEvent(VK_F7, true)) + if (host.OnDirectKeyEvent(VK_F7, LOBYTE(HIWORD(message.lParam)), true)) { // The application consumed the F7. Don't let Xaml get it. continue; @@ -154,7 +154,7 @@ int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) if (_messageIsAltKeyup(message)) { // Let's pass to the application - if (host.OnDirectKeyEvent(VK_MENU, false)) + if (host.OnDirectKeyEvent(VK_MENU, LOBYTE(HIWORD(message.lParam)), false)) { // The application consumed the Alt. Don't let Xaml get it. continue;