[windows] Sends complete key data to framework (flutter/engine#17577)

This commit is contained in:
Francisco Magdaleno
2020-04-09 20:55:02 -07:00
committed by GitHub
parent efff943c47
commit c38e7106c0
12 changed files with 144 additions and 31 deletions

View File

@@ -83,7 +83,6 @@ StandardCodecSerializer::~StandardCodecSerializer() = default;
EncodableValue StandardCodecSerializer::ReadValue(
ByteBufferStreamReader* stream) const {
EncodedType type = static_cast<EncodedType>(stream->ReadByte());
;
switch (type) {
case EncodedType::kNull:
return EncodableValue();

View File

@@ -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<flutter::BasicMessageChannel<rapidjson::Document>>(
@@ -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:

View File

@@ -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;

View File

@@ -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;

View File

@@ -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) {}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -110,6 +110,7 @@ Win32Window::MessageHandler(HWND hwnd,
auto window =
reinterpret_cast<Win32Window*>(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<char32_t>(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);

View File

@@ -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