[Windows] Hide Win32 API of the keyboard system behind ifdef (flutter/engine#24677)

This commit is contained in:
Tong Mu
2021-02-27 14:05:32 -08:00
committed by GitHub
parent 9b9f2c2d15
commit 5d8d91980b
7 changed files with 55 additions and 16 deletions

View File

@@ -268,10 +268,10 @@ void FlutterWindowWinUWP::OnKeyUp(
auto status = args.KeyStatus();
unsigned int scancode = status.ScanCode;
int key = static_cast<int>(args.VirtualKey());
char32_t chararacter = static_cast<char32_t>(key | 32);
int action = 0x0101;
binding_handler_delegate_->OnKey(key, scancode, action, chararacter,
false /* extended */, true /* was_down */);
binding_handler_delegate_->OnKey(key, scancode, action, 0,
status.IsExtendedKey /* extended */,
status.WasKeyDown /* was_down */);
}
void FlutterWindowWinUWP::OnKeyDown(
@@ -284,10 +284,10 @@ void FlutterWindowWinUWP::OnKeyDown(
auto status = args.KeyStatus();
unsigned int scancode = status.ScanCode;
int key = static_cast<int>(args.VirtualKey());
char32_t chararacter = static_cast<char32_t>(key | 32);
int action = 0x0100;
binding_handler_delegate_->OnKey(key, scancode, action, chararacter,
false /* extended */, false /* was_down */);
binding_handler_delegate_->OnKey(key, scancode, action, 0,
status.IsExtendedKey /* extended */,
status.WasKeyDown /* was_down */);
}
void FlutterWindowWinUWP::OnCharacterReceived(

View File

@@ -78,7 +78,17 @@ void FlutterWindowsView::RegisterKeyboardHandlers(
// of the event. In order to allow the same real event in the future, the
// handler is "toggled" when events pass through, therefore the redispatching
// algorithm does not allow more than 1 handler that takes |SendInput|.
auto key_handler = std::make_unique<flutter::KeyboardKeyHandler>(SendInput);
#ifdef WINUWP
flutter::KeyboardKeyHandler::EventRedispatcher redispatch_event = nullptr;
flutter::KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state =
nullptr;
#else
flutter::KeyboardKeyHandler::EventRedispatcher redispatch_event = SendInput;
flutter::KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state =
GetKeyState;
#endif
auto key_handler =
std::make_unique<flutter::KeyboardKeyHandler>(redispatch_event);
key_handler->AddDelegate(
std::make_unique<KeyboardKeyChannelHandler>(messenger));
key_handler->AddDelegate(std::make_unique<KeyboardKeyEmbedderHandler>(
@@ -86,7 +96,7 @@ void FlutterWindowsView::RegisterKeyboardHandlers(
void* user_data) {
return engine_->SendKeyEvent(event, callback, user_data);
},
GetKeyState));
get_key_state));
AddKeyboardHandler(std::move(key_handler));
AddKeyboardHandler(
std::make_unique<flutter::TextInputPlugin>(messenger, this));

View File

@@ -55,6 +55,11 @@ static constexpr int kScrollLock = 1 << 13;
/// with the re-defined values declared above for compatibility with the Flutter
/// framework.
int GetModsForKeyState() {
// TODO(clarkezone) need to add support for get modifier state for UWP
// https://github.com/flutter/flutter/issues/70202
#ifdef WINUWP
return 0;
#else
int mods = 0;
if (GetKeyState(VK_SHIFT) < 0)
@@ -86,6 +91,7 @@ int GetModsForKeyState() {
if (GetKeyState(VK_SCROLL) < 0)
mods |= kScrollLock;
return mods;
#endif
}
} // namespace

View File

@@ -56,9 +56,7 @@ constexpr SHORT kStateMaskPressed = 0x80;
} // namespace
KeyboardKeyEmbedderHandler::KeyboardKeyEmbedderHandler(
std::function<void(const FlutterKeyEvent&,
FlutterKeyEventCallback,
void* user_data)> send_event,
SendEvent send_event,
GetKeyStateHandler get_key_state)
: sendEvent_(send_event), get_key_state_(get_key_state), response_id_(1) {
InitCriticalKeys();
@@ -264,6 +262,11 @@ void KeyboardKeyEmbedderHandler::UpdateLastSeenCritialKey(
void KeyboardKeyEmbedderHandler::SynchronizeCritialToggledStates(
int toggle_virtual_key) {
// TODO(dkwingsmt) consider adding support for synchronizing key state for UWP
// https://github.com/flutter/flutter/issues/70202
#ifdef WINUWP
return;
#else
for (auto& kv : critical_keys_) {
UINT virtual_key = kv.first;
CriticalKey& key_info = kv.second;
@@ -301,9 +304,15 @@ void KeyboardKeyEmbedderHandler::SynchronizeCritialToggledStates(
key_info.toggled_on = should_toggled;
}
}
#endif
}
void KeyboardKeyEmbedderHandler::SynchronizeCritialPressedStates() {
// TODO(dkwingsmt) consider adding support for synchronizing key state for UWP
// https://github.com/flutter/flutter/issues/70202
#ifdef WINUWP
return;
#else
for (auto& kv : critical_keys_) {
UINT virtual_key = kv.first;
CriticalKey& key_info = kv.second;
@@ -333,6 +342,7 @@ void KeyboardKeyEmbedderHandler::SynchronizeCritialPressedStates() {
}
}
}
#endif
}
void KeyboardKeyEmbedderHandler::HandleResponse(bool handled, void* user_data) {
@@ -342,6 +352,11 @@ void KeyboardKeyEmbedderHandler::HandleResponse(bool handled, void* user_data) {
}
void KeyboardKeyEmbedderHandler::InitCriticalKeys() {
// TODO(dkwingsmt) consider adding support for synchronizing key state for UWP
// https://github.com/flutter/flutter/issues/70202
#ifdef WINUWP
return;
#else
auto createCheckedKey = [this](UINT virtual_key, bool extended,
bool check_pressed,
bool check_toggled) -> CriticalKey {
@@ -374,6 +389,7 @@ void KeyboardKeyEmbedderHandler::InitCriticalKeys() {
createCheckedKey(VK_SCROLL, false, true, true));
critical_keys_.emplace(VK_NUMLOCK,
createCheckedKey(VK_NUMLOCK, true, true, true));
#endif
}
void KeyboardKeyEmbedderHandler::ConvertUtf32ToUtf8_(char* out, char32_t ch) {

View File

@@ -37,7 +37,8 @@ class KeyboardKeyEmbedderHandler
// of the SendEvent call might be nullptr.
//
// Use `get_key_state` to define how the class should get a reliable result of
// the state for a virtual key. It's typically Win32's GetKeyState.
// the state for a virtual key. It's typically Win32's GetKeyState, but can
// also be nullptr (for UWP).
explicit KeyboardKeyEmbedderHandler(SendEvent send_event,
GetKeyStateHandler get_key_state);

View File

@@ -38,9 +38,7 @@ KeyboardKeyHandler::KeyboardKeyHandlerDelegate::~KeyboardKeyHandlerDelegate() =
default;
KeyboardKeyHandler::KeyboardKeyHandler(EventRedispatcher redispatch_event)
: redispatch_event_(redispatch_event), last_sequence_id_(1) {
assert(redispatch_event_ != nullptr);
}
: redispatch_event_(redispatch_event), last_sequence_id_(1) {}
KeyboardKeyHandler::~KeyboardKeyHandler() = default;
@@ -57,6 +55,12 @@ size_t KeyboardKeyHandler::RedispatchedCount() {
}
void KeyboardKeyHandler::RedispatchEvent(std::unique_ptr<PendingEvent> event) {
// TODO(dkwingsmt) consider adding support for redispatching events for UWP
// in order to support add-to-app.
// https://github.com/flutter/flutter/issues/70202
#ifdef WINUWP
return;
#else
uint8_t scancode = event->scancode;
char32_t character = event->character;
@@ -81,6 +85,7 @@ void KeyboardKeyHandler::RedispatchEvent(std::unique_ptr<PendingEvent> event) {
"with scancode "
<< scancode << " (character " << character << ")" << std::endl;
}
#endif
}
bool KeyboardKeyHandler::KeyboardHook(FlutterWindowsView* view,

View File

@@ -58,7 +58,8 @@ class KeyboardKeyHandler : public KeyboardHandlerBase {
// Create a KeyboardKeyHandler and specify where to redispatch events.
//
// The |redispatch_event| is typically |SendInput|.
// The |redispatch_event| is typically |SendInput|, but can also be nullptr
// (for UWP).
explicit KeyboardKeyHandler(EventRedispatcher redispatch_event);
~KeyboardKeyHandler();