77#include < windows.h>
88
99#include " flutter/fml/logging.h"
10+ #include " flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h"
1011#include " flutter/shell/platform/windows/keyboard_utils.h"
1112
1213namespace flutter {
@@ -17,15 +18,51 @@ namespace {
1718// emitting a warning on the console about unhandled events.
1819static constexpr int kMaxPendingEvents = 1000 ;
1920
21+ // The name of the channel for keyboard state queries.
22+ static constexpr char kChannelName [] = " flutter/keyboard" ;
23+
24+ static constexpr char kGetKeyboardStateMethod [] = " getKeyboardState" ;
25+
2026} // namespace
2127
2228KeyboardKeyHandler::KeyboardKeyHandlerDelegate::~KeyboardKeyHandlerDelegate () =
2329 default ;
2430
25- KeyboardKeyHandler::KeyboardKeyHandler () : last_sequence_id_(1 ) {}
31+ KeyboardKeyHandler::KeyboardKeyHandler (flutter::BinaryMessenger* messenger)
32+ : last_sequence_id_(1 ),
33+ channel_ (std::make_unique<MethodChannel<EncodableValue>>(
34+ messenger,
35+ kChannelName ,
36+ &StandardMethodCodec::GetInstance ())) {}
2637
2738KeyboardKeyHandler::~KeyboardKeyHandler () = default ;
2839
40+ void KeyboardKeyHandler::InitKeyboardChannel () {
41+ channel_->SetMethodCallHandler (
42+ [this ](const MethodCall<EncodableValue>& call,
43+ std::unique_ptr<MethodResult<EncodableValue>> result) {
44+ HandleMethodCall (call, std::move (result));
45+ });
46+ }
47+
48+ void KeyboardKeyHandler::HandleMethodCall (
49+ const MethodCall<EncodableValue>& method_call,
50+ std::unique_ptr<MethodResult<EncodableValue>> result) {
51+ const std::string& method = method_call.method_name ();
52+ if (method.compare (kGetKeyboardStateMethod ) == 0 ) {
53+ EncodableMap value;
54+ const auto & pressed_state = GetPressedState ();
55+ for (const auto & pressed_key : pressed_state) {
56+ EncodableValue physical_value (static_cast <long long >(pressed_key.first ));
57+ EncodableValue logical_value (static_cast <long long >(pressed_key.second ));
58+ value[physical_value] = logical_value;
59+ }
60+ result->Success (EncodableValue (value));
61+ } else {
62+ result->NotImplemented ();
63+ }
64+ }
65+
2966void KeyboardKeyHandler::AddDelegate (
3067 std::unique_ptr<KeyboardKeyHandlerDelegate> delegate) {
3168 delegates_.push_back (std::move (delegate));
@@ -37,6 +74,12 @@ void KeyboardKeyHandler::SyncModifiersIfNeeded(int modifiers_state) {
3774 key_embedder_handler->SyncModifiersIfNeeded (modifiers_state);
3875}
3976
77+ std::map<uint64_t , uint64_t > KeyboardKeyHandler::GetPressedState () {
78+ // The embedder responder is the first element in delegates_.
79+ auto & key_embedder_handler = delegates_.front ();
80+ return key_embedder_handler->GetPressedState ();
81+ }
82+
4083void KeyboardKeyHandler::KeyboardHook (int key,
4184 int scancode,
4285 int action,
0 commit comments