From 1da038904d6c11f02c01b86ae0815d7a923222f1 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 (cherry picked from commit aecd99e0ca3275e1a02e782ee2ff815a93248c6c) --- 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 e036bd9ee2c..4c8013365ed 100644 --- a/src/cascadia/TerminalApp/AppLogic.cpp +++ b/src/cascadia/TerminalApp/AppLogic.cpp @@ -911,7 +911,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) { @@ -922,7 +922,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 c879a6f1876..cfc7d55a5b5 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 aaa58656633..e789124e813 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -752,7 +752,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; @@ -760,9 +760,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) @@ -784,7 +782,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 a56fdb3eff6..099ee387499 100644 --- a/src/cascadia/TerminalControl/TermControl.h +++ b/src/cascadia/TerminalControl/TermControl.h @@ -92,7 +92,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 0d99a87b530..fc41fb92a47 100644 --- a/src/cascadia/TerminalControl/TermControl.idl +++ b/src/cascadia/TerminalControl/TermControl.idl @@ -14,8 +14,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 d9e1b4bb70d..6dd0aa5515d 100644 --- a/src/cascadia/WindowsTerminal/AppHost.cpp +++ b/src/cascadia/WindowsTerminal/AppHost.cpp @@ -68,11 +68,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 4e072411de8..b2de5de39cb 100644 --- a/src/cascadia/WindowsTerminal/AppHost.h +++ b/src/cascadia/WindowsTerminal/AppHost.h @@ -17,7 +17,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;