From c38e7106c0d6795571615b21a10614159a513c24 Mon Sep 17 00:00:00 2001 From: Francisco Magdaleno Date: Thu, 9 Apr 2020 20:55:02 -0700 Subject: [PATCH] [windows] Sends complete key data to framework (flutter/engine#17577) --- .../cpp/client_wrapper/standard_codec.cc | 1 - .../platform/windows/key_event_handler.cc | 70 ++++++++++++++++++- .../platform/windows/key_event_handler.h | 2 +- .../platform/windows/keyboard_hook_handler.h | 2 +- .../windows/testing/win32_window_test.cc | 5 +- .../windows/testing/win32_window_test.h | 2 +- .../platform/windows/text_input_plugin.cc | 2 +- .../platform/windows/text_input_plugin.h | 2 +- .../platform/windows/win32_flutter_window.cc | 14 ++-- .../platform/windows/win32_flutter_window.h | 4 +- .../shell/platform/windows/win32_window.cc | 65 +++++++++++++---- .../shell/platform/windows/win32_window.h | 6 +- 12 files changed, 144 insertions(+), 31 deletions(-) diff --git a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc b/engine/src/flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc index 137a3e8490..d16ce3b54f 100644 --- a/engine/src/flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc +++ b/engine/src/flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc @@ -83,7 +83,6 @@ StandardCodecSerializer::~StandardCodecSerializer() = default; EncodableValue StandardCodecSerializer::ReadValue( ByteBufferStreamReader* stream) const { EncodedType type = static_cast(stream->ReadByte()); - ; switch (type) { case EncodedType::kNull: return EncodableValue(); diff --git a/engine/src/flutter/shell/platform/windows/key_event_handler.cc b/engine/src/flutter/shell/platform/windows/key_event_handler.cc index f611210174..8079f197cf 100644 --- a/engine/src/flutter/shell/platform/windows/key_event_handler.cc +++ b/engine/src/flutter/shell/platform/windows/key_event_handler.cc @@ -13,15 +13,76 @@ static constexpr char kChannelName[] = "flutter/keyevent"; static constexpr char kKeyCodeKey[] = "keyCode"; +static constexpr char kScanCodeKey[] = "scanCode"; +static constexpr char kCharacterCodePointKey[] = "characterCodePoint"; +static constexpr char kModifiersKey[] = "modifiers"; static constexpr char kKeyMapKey[] = "keymap"; static constexpr char kTypeKey[] = "type"; -static constexpr char kAndroidKeyMap[] = "android"; +static constexpr char kWindowsKeyMap[] = "windows"; static constexpr char kKeyUp[] = "keyup"; static constexpr char kKeyDown[] = "keydown"; namespace flutter { +// Re-definition of the modifiers for compatibility with the Flutter framework. +// These have to be in sync with the framework's RawKeyEventDataWindows +// modifiers definition. +// https://github.com/flutter/flutter/blob/19ff596979e407c484a32f4071420fca4f4c885f/packages/flutter/lib/src/services/raw_keyboard_windows.dart#L203 +const int kShift = 1 << 0; +const int kShiftLeft = 1 << 1; +const int kShiftRight = 1 << 2; +const int kControl = 1 << 3; +const int kControlLeft = 1 << 4; +const int kControlRight = 1 << 5; +const int kAlt = 1 << 6; +const int kAltLeft = 1 << 7; +const int kAltRight = 1 << 8; +const int kWinLeft = 1 << 9; +const int kWinRight = 1 << 10; +const int kCapsLock = 1 << 11; +const int kNumLock = 1 << 12; +const int kScrollLock = 1 << 13; + +namespace { +/// Calls GetKeyState() an all modifier keys and packs the result in an int, +/// with the re-defined values declared above for compatibility with the Flutter +/// framework. +int GetModsForKeyState() { + int mods = 0; + + if (GetKeyState(VK_SHIFT) < 0) + mods |= kShift; + if (GetKeyState(VK_LSHIFT) < 0) + mods |= kShiftLeft; + if (GetKeyState(VK_RSHIFT) < 0) + mods |= kShiftRight; + if (GetKeyState(VK_CONTROL) < 0) + mods |= kControl; + if (GetKeyState(VK_LCONTROL) < 0) + mods |= kControlLeft; + if (GetKeyState(VK_RCONTROL) < 0) + mods |= kControlRight; + if (GetKeyState(VK_MENU) < 0) + mods |= kAlt; + if (GetKeyState(VK_LMENU) < 0) + mods |= kAltLeft; + if (GetKeyState(VK_RMENU) < 0) + mods |= kAltRight; + if (GetKeyState(VK_LWIN) < 0) + mods |= kWinLeft; + if (GetKeyState(VK_RWIN) < 0) + mods |= kWinRight; + if (GetKeyState(VK_CAPITAL) < 0) + mods |= kCapsLock; + if (GetKeyState(VK_NUMLOCK) < 0) + mods |= kNumLock; + if (GetKeyState(VK_SCROLL) < 0) + mods |= kScrollLock; + return mods; +} +} // namespace + KeyEventHandler::KeyEventHandler(flutter::BinaryMessenger* messenger) : channel_( std::make_unique>( @@ -38,13 +99,16 @@ void KeyEventHandler::KeyboardHook(Win32FlutterWindow* window, int key, int scancode, int action, - int mods) { + char32_t character) { // TODO: Translate to a cross-platform key code system rather than passing // the native key code. rapidjson::Document event(rapidjson::kObjectType); auto& allocator = event.GetAllocator(); event.AddMember(kKeyCodeKey, key, allocator); - event.AddMember(kKeyMapKey, kAndroidKeyMap, allocator); + event.AddMember(kScanCodeKey, scancode, allocator); + event.AddMember(kCharacterCodePointKey, character, allocator); + event.AddMember(kKeyMapKey, kWindowsKeyMap, allocator); + event.AddMember(kModifiersKey, GetModsForKeyState(), allocator); switch (action) { case WM_KEYDOWN: diff --git a/engine/src/flutter/shell/platform/windows/key_event_handler.h b/engine/src/flutter/shell/platform/windows/key_event_handler.h index 47ec63f6d1..1b28e90b71 100644 --- a/engine/src/flutter/shell/platform/windows/key_event_handler.h +++ b/engine/src/flutter/shell/platform/windows/key_event_handler.h @@ -31,7 +31,7 @@ class KeyEventHandler : public KeyboardHookHandler { int key, int scancode, int action, - int mods) override; + char32_t character) override; // |KeyboardHookHandler| void CharHook(Win32FlutterWindow* window, char32_t code_point) override; diff --git a/engine/src/flutter/shell/platform/windows/keyboard_hook_handler.h b/engine/src/flutter/shell/platform/windows/keyboard_hook_handler.h index 1b3304ddd5..4583b2bb76 100644 --- a/engine/src/flutter/shell/platform/windows/keyboard_hook_handler.h +++ b/engine/src/flutter/shell/platform/windows/keyboard_hook_handler.h @@ -21,7 +21,7 @@ class KeyboardHookHandler { int key, int scancode, int action, - int mods) = 0; + char32_t character) = 0; // A function for hooking into unicode code point input. virtual void CharHook(Win32FlutterWindow* window, char32_t code_point) = 0; diff --git a/engine/src/flutter/shell/platform/windows/testing/win32_window_test.cc b/engine/src/flutter/shell/platform/windows/testing/win32_window_test.cc index 13a70e0396..53ad12b5b7 100644 --- a/engine/src/flutter/shell/platform/windows/testing/win32_window_test.cc +++ b/engine/src/flutter/shell/platform/windows/testing/win32_window_test.cc @@ -21,7 +21,10 @@ void Win32WindowTest::OnPointerLeave() {} void Win32WindowTest::OnChar(char32_t code_point) {} -void Win32WindowTest::OnKey(int key, int scancode, int action, int mods) {} +void Win32WindowTest::OnKey(int key, + int scancode, + int action, + char32_t character) {} void Win32WindowTest::OnScroll(double delta_x, double delta_y) {} diff --git a/engine/src/flutter/shell/platform/windows/testing/win32_window_test.h b/engine/src/flutter/shell/platform/windows/testing/win32_window_test.h index ccefdfe717..77d25464b9 100644 --- a/engine/src/flutter/shell/platform/windows/testing/win32_window_test.h +++ b/engine/src/flutter/shell/platform/windows/testing/win32_window_test.h @@ -45,7 +45,7 @@ class Win32WindowTest : public Win32Window { void OnChar(char32_t code_point) override; // |Win32Window| - void OnKey(int key, int scancode, int action, int mods) override; + void OnKey(int key, int scancode, int action, char32_t character) override; // |Win32Window| void OnScroll(double delta_x, double delta_y) override; diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc index 0fd4dee9d2..10b3864d4f 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc @@ -49,7 +49,7 @@ void TextInputPlugin::KeyboardHook(Win32FlutterWindow* window, int key, int scancode, int action, - int mods) { + char32_t character) { if (active_model_ == nullptr) { return; } diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin.h b/engine/src/flutter/shell/platform/windows/text_input_plugin.h index 03cf9b7c55..dced9a6954 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.h +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.h @@ -32,7 +32,7 @@ class TextInputPlugin : public KeyboardHookHandler { int key, int scancode, int action, - int mods) override; + char32_t character) override; // |KeyboardHookHandler| void CharHook(Win32FlutterWindow* window, char32_t code_point) override; diff --git a/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc b/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc index cf69618835..73c46febf3 100644 --- a/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc +++ b/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc @@ -167,9 +167,12 @@ void Win32FlutterWindow::OnChar(char32_t code_point) { } } -void Win32FlutterWindow::OnKey(int key, int scancode, int action, int mods) { +void Win32FlutterWindow::OnKey(int key, + int scancode, + int action, + char32_t character) { if (process_events_) { - SendKey(key, scancode, action, 0); + SendKey(key, scancode, action, character); } } @@ -267,9 +270,12 @@ void Win32FlutterWindow::SendChar(char32_t code_point) { } } -void Win32FlutterWindow::SendKey(int key, int scancode, int action, int mods) { +void Win32FlutterWindow::SendKey(int key, + int scancode, + int action, + char32_t character) { for (const auto& handler : keyboard_hook_handlers_) { - handler->KeyboardHook(this, key, scancode, action, mods); + handler->KeyboardHook(this, key, scancode, action, character); } } diff --git a/engine/src/flutter/shell/platform/windows/win32_flutter_window.h b/engine/src/flutter/shell/platform/windows/win32_flutter_window.h index 5fa58aa295..bce1f2e6fd 100644 --- a/engine/src/flutter/shell/platform/windows/win32_flutter_window.h +++ b/engine/src/flutter/shell/platform/windows/win32_flutter_window.h @@ -60,7 +60,7 @@ class Win32FlutterWindow : public Win32Window { void OnChar(char32_t code_point) override; // |Win32Window| - void OnKey(int key, int scancode, int action, int mods) override; + void OnKey(int key, int scancode, int action, char32_t character) override; // |Win32Window| void OnScroll(double delta_x, double delta_y) override; @@ -116,7 +116,7 @@ class Win32FlutterWindow : public Win32Window { void SendChar(char32_t code_point); // Reports a raw keyboard message to Flutter engine. - void SendKey(int key, int scancode, int action, int mods); + void SendKey(int key, int scancode, int action, char32_t character); // Reports scroll wheel events to Flutter engine. void SendScroll(double delta_x, double delta_y); diff --git a/engine/src/flutter/shell/platform/windows/win32_window.cc b/engine/src/flutter/shell/platform/windows/win32_window.cc index 0abafb562f..6f8c48e9c0 100644 --- a/engine/src/flutter/shell/platform/windows/win32_window.cc +++ b/engine/src/flutter/shell/platform/windows/win32_window.cc @@ -110,6 +110,7 @@ Win32Window::MessageHandler(HWND hwnd, auto window = reinterpret_cast(GetWindowLongPtr(hwnd, GWLP_USERDATA)); UINT button_pressed = 0; + if (window != nullptr) { switch (message) { case kWmDpiChangedBeforeParent: @@ -190,35 +191,71 @@ Win32Window::MessageHandler(HWND hwnd, // DefWindowProc will send WM_CHAR for this WM_UNICHAR. break; } + case WM_DEADCHAR: + case WM_SYSDEADCHAR: case WM_CHAR: case WM_SYSCHAR: { - if (wparam == VK_BACK) - break; char32_t code_point = static_cast(wparam); static char32_t lead_surrogate = 0; - // If code_point is LeadSurrogate, save and return. + // If code_point is LeadSurrogate, save to combine to potentially form + // a complex Unicode character. if ((code_point & 0xFFFFFC00) == 0xD800) { lead_surrogate = code_point; - return TRUE; - } - // Merge TrailSurrogate and LeadSurrogate. - if (lead_surrogate != 0 && (code_point & 0xFFFFFC00) == 0xDC00) { + } else if (lead_surrogate != 0 && (code_point & 0xFFFFFC00) == 0xDC00) { + // Merge TrailSurrogate and LeadSurrogate. code_point = 0x10000 + ((lead_surrogate & 0x000003FF) << 10) + (code_point & 0x3FF); + lead_surrogate = 0; + } + + // In an ENG-INTL keyboard, pressing "'" + "e" produces é. In this case, + // the "'" key is a dead char, and shouldn't be sent to window->OnChar + // for text input. However, the key event should still be sent to + // Flutter. The result would be: + // * Key event - key code: 222 (quote) - key label: ' + // * Key event - key code: 69 (e) - key label: é + // + // As for text input, only the second key press will display a + // character. + if (wparam != VK_BACK && message != WM_DEADCHAR && + message != WM_SYSDEADCHAR) { + window->OnChar(code_point); + } + + // All key presses that generate a character should be sent from + // WM_CHAR. In order to send the full key press information, the keycode + // is persisted in keycode_for_char_message_ obtained from WM_KEYDOWN. + if (keycode_for_char_message_ != 0) { + const unsigned int scancode = (lparam >> 16) & 0xff; + window->OnKey(keycode_for_char_message_, scancode, WM_KEYDOWN, + code_point); + keycode_for_char_message_ = 0; } - lead_surrogate = 0; - window->OnChar(code_point); break; } case WM_KEYDOWN: case WM_SYSKEYDOWN: case WM_KEYUP: case WM_SYSKEYUP: - unsigned char scancode = ((unsigned char*)&lparam)[2]; - unsigned int virtualKey = MapVirtualKey(scancode, MAPVK_VSC_TO_VK); - const int key = virtualKey; - const int action = message == WM_KEYDOWN ? WM_KEYDOWN : WM_KEYUP; - window->OnKey(key, scancode, action, 0); + const bool is_keydown_message = + (message == WM_KEYDOWN || message == WM_SYSKEYDOWN); + // Check if this key produces a character. If so, the key press should + // be sent with the character produced at WM_CHAR. Store the produced + // keycode (it's not accessible from WM_CHAR) to be used in WM_CHAR. + const unsigned int character = MapVirtualKey(wparam, MAPVK_VK_TO_CHAR); + if (character > 0 && is_keydown_message) { + keycode_for_char_message_ = wparam; + break; + } + unsigned int keyCode(wparam); + const unsigned int scancode = (lparam >> 16) & 0xff; + // If the key is a modifier, get its side. + if (keyCode == VK_SHIFT || keyCode == VK_MENU || + keyCode == VK_CONTROL) { + keyCode = MapVirtualKey(scancode, MAPVK_VSC_TO_VK_EX); + } + const int action = is_keydown_message ? WM_KEYDOWN : WM_KEYUP; + window->OnKey(keyCode, scancode, action, 0); break; } return DefWindowProc(hwnd, message, wparam, lparam); diff --git a/engine/src/flutter/shell/platform/windows/win32_window.h b/engine/src/flutter/shell/platform/windows/win32_window.h index dfb5928048..ba959ffcaa 100644 --- a/engine/src/flutter/shell/platform/windows/win32_window.h +++ b/engine/src/flutter/shell/platform/windows/win32_window.h @@ -106,7 +106,7 @@ class Win32Window { virtual void OnChar(char32_t code_point) = 0; // Called when raw keyboard input occurs. - virtual void OnKey(int key, int scancode, int action, int mods) = 0; + virtual void OnKey(int key, int scancode, int action, char32_t character) = 0; // Called when mouse scrollwheel input occurs. virtual void OnScroll(double delta_x, double delta_y) = 0; @@ -172,6 +172,10 @@ class Win32Window { // Keeps track of mouse state in relation to the window. MouseState mouse_state_; + + // Keeps track of the last key code produced by a WM_KEYDOWN or WM_SYSKEYDOWN + // message. + int keycode_for_char_message_ = 0; }; } // namespace flutter