| Flutter Windows Embedder |

keyboard_key_handler.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_KEYBOARD_KEY_HANDLER_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_KEYBOARD_KEY_HANDLER_H_
7
8 #include <windows.h>
9 #include <deque>
10 #include <map>
11 #include <memory>
12 #include <string>
13
14 #include "flutter/fml/macros.h"
15 #include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
16 #include "flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h"
17 #include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h"
18 #include "flutter/shell/platform/windows/keyboard_handler_base.h"
19
20 namespace flutter {
21
22 // Handles key events.
23 //
24 // This class detects whether an incoming event is a redispatched one,
25 // dispatches native events to delegates and collect their responses,
26 // and redispatches events unhandled by Flutter back to the system.
27 // See |KeyboardHook| for more information about dispatching.
28 //
29 // This class owns multiple |KeyboardKeyHandlerDelegate|s, which
30 // implements the exact behavior to asynchronously handle events. In
31 // reality, this design is only to support sending events through
32 // "channel" (RawKeyEvent) and "embedder" (KeyEvent) simultaneously,
33 // the former of which shall be removed after the deprecation window
34 // of the RawKeyEvent system.
35 class KeyboardKeyHandler : public KeyboardHandlerBase {
36 public:
37 // An interface for concrete definition of how to asynchronously handle key
38 // events.
39 class KeyboardKeyHandlerDelegate {
40 public:
41 // Defines how to how to asynchronously handle key events.
42 //
43 // |KeyboardHook| should invoke |callback| with the response (whether the
44 // event is handled) later for exactly once.
45 virtual void KeyboardHook(int key,
46 int scancode,
47 int action,
48 char32_t character,
49 bool extended,
50 bool was_down,
51 KeyEventCallback callback) = 0;
52
53 virtual void SyncModifiersIfNeeded(int modifiers_state) = 0;
54
55 virtual std::map<uint64_t, uint64_t> GetPressedState() = 0;
56
57 virtual ~KeyboardKeyHandlerDelegate();
58 };
59
60 // Create a |KeyboardKeyHandler| by specifying the messenger
61 // through which the messages are sent.
62 explicit KeyboardKeyHandler(flutter::BinaryMessenger* messenger);
63
64 ~KeyboardKeyHandler();
65
66 // Init the keyboard channel used to answer to pressed state queries.
67 void InitKeyboardChannel();
68
69 // Add a delegate that handles events received by |KeyboardHook|.
70 void AddDelegate(std::unique_ptr<KeyboardKeyHandlerDelegate> delegate);
71
72 // Synthesize modifier keys events if needed.
73 void SyncModifiersIfNeeded(int modifiers_state) override;
74
75 // Handles a key event.
76 //
77 // Returns whether this handler claims to handle the event, which is true if
78 // and only if the event is a non-synthesized event.
79 //
80 // Windows requires a synchronous response of whether a key event should be
81 // handled, while the query to Flutter is always asynchronous. This is
82 // resolved by the "redispatching" algorithm: by default, the response to a
83 // fresh event is always true. The event is then sent to the framework.
84 // If the framework later decides not to handle the event, this class will
85 // create an identical event and dispatch it to the system, and remember all
86 // synthesized events. The fist time an exact event (by |ComputeEventHash|) is
87 // received in the future, the new event is considered a synthesized one,
88 // causing |KeyboardHook| to return false to fall back to other keyboard
89 // handlers.
90 //
91 // Whether a non-synthesized event is considered handled by the framework is
92 // decided by dispatching the event to all delegates, simultaneously,
93 // unconditionally, in insertion order, and collecting their responses later.
94 // It's not supported to prevent any delegates to process the events, because
95 // in reality this will only support 2 hardcoded delegates, and only to
96 // continue supporting the legacy API (channel) during the deprecation window,
97 // after which the channel delegate should be removed.
98 //
99 // Inherited from |KeyboardHandlerBase|.
100 void KeyboardHook(int key,
101 int scancode,
102 int action,
103 char32_t character,
104 bool extended,
105 bool was_down,
106 KeyEventCallback callback) override;
107
108 // Returns the keyboard pressed state.
109 //
110 // Returns the keyboard pressed state. The dictionary contains one entry per
111 // pressed keys, mapping from the logical key to the physical key.
112 std::map<uint64_t, uint64_t> GetPressedState() override;
113
114 private:
115 struct PendingEvent {
116 // Self-incrementing ID attached to an event sent to the framework.
117 uint64_t sequence_id;
118 // The number of delegates that haven't replied.
119 size_t unreplied;
120 // Whether any replied delegates reported true (handled).
121 bool any_handled;
122
123 // Where to report the delegates' result to.
124 //
125 // Typically a callback provided by KeyboardManager32.
126 KeyEventCallback callback;
127 };
128
129 void ResolvePendingEvent(uint64_t sequence_id, bool handled);
130
131 // Called when a method is called on |channel_|;
132 void HandleMethodCall(
133 const flutter::MethodCall<EncodableValue>& method_call,
134 std::unique_ptr<flutter::MethodResult<EncodableValue>> result);
135
136 std::vector<std::unique_ptr<KeyboardKeyHandlerDelegate>> delegates_;
137
138 // The queue of key events that have been sent to the framework but have not
139 // yet received a response.
140 std::deque<std::unique_ptr<PendingEvent>> pending_responds_;
141
142 // The sequence_id attached to the last event sent to the framework.
143 uint64_t last_sequence_id_;
144
145 // The Flutter system channel for keyboard state messages.
146 std::unique_ptr<flutter::MethodChannel<EncodableValue>> channel_;
147
148 FML_DISALLOW_COPY_AND_ASSIGN(KeyboardKeyHandler);
149 };
150
151 } // namespace flutter
152
153 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_KEYBOARD_KEY_HANDLER_H_
encodable_value.h
flutter::KeyboardKeyHandler
Definition: keyboard_key_handler.h:35
scancode
int scancode
Definition: keyboard_key_handler_unittests.cc:115
was_down
bool was_down
Definition: keyboard_key_handler_unittests.cc:119
extended
bool extended
Definition: keyboard_key_handler_unittests.cc:118
character
char32_t character
Definition: keyboard_key_handler_unittests.cc:117
flutter::KeyboardKeyHandler::SyncModifiersIfNeeded
void SyncModifiersIfNeeded(int modifiers_state) override
Definition: keyboard_key_handler.cc:71
flutter::KeyboardKeyHandler::KeyboardKeyHandlerDelegate::GetPressedState
virtual std::map< uint64_t, uint64_t > GetPressedState()=0
flutter::KeyboardHandlerBase::KeyEventCallback
std::function< void(bool)> KeyEventCallback
Definition: keyboard_handler_base.h:20
binary_messenger.h
flutter::KeyboardKeyHandler::KeyboardKeyHandlerDelegate::SyncModifiersIfNeeded
virtual void SyncModifiersIfNeeded(int modifiers_state)=0
flutter::KeyboardKeyHandler::KeyboardKeyHandlerDelegate
Definition: keyboard_key_handler.h:39
flutter::KeyboardKeyHandler::KeyboardHook
void KeyboardHook(int key, int scancode, int action, char32_t character, bool extended, bool was_down, KeyEventCallback callback) override
Definition: keyboard_key_handler.cc:83
flutter::BinaryMessenger
Definition: binary_messenger.h:28
flutter::KeyboardKeyHandler::InitKeyboardChannel
void InitKeyboardChannel()
Definition: keyboard_key_handler.cc:40
flutter::MethodCall
Definition: method_call.h:18
flutter::KeyboardKeyHandler::GetPressedState
std::map< uint64_t, uint64_t > GetPressedState() override
Definition: keyboard_key_handler.cc:77
flutter::KeyboardKeyHandler::~KeyboardKeyHandler
~KeyboardKeyHandler()
flutter::KeyboardKeyHandler::KeyboardKeyHandler
KeyboardKeyHandler(flutter::BinaryMessenger *messenger)
Definition: keyboard_key_handler.cc:31
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::KeyboardKeyHandler::KeyboardKeyHandlerDelegate::~KeyboardKeyHandlerDelegate
virtual ~KeyboardKeyHandlerDelegate()
flutter::KeyboardKeyHandler::KeyboardKeyHandlerDelegate::KeyboardHook
virtual void KeyboardHook(int key, int scancode, int action, char32_t character, bool extended, bool was_down, KeyEventCallback callback)=0
keyboard_handler_base.h
flutter::MethodResult< EncodableValue >
flutter::KeyboardHandlerBase
Definition: keyboard_handler_base.h:18
method_channel.h
flutter::KeyboardKeyHandler::AddDelegate
void AddDelegate(std::unique_ptr< KeyboardKeyHandlerDelegate > delegate)
Definition: keyboard_key_handler.cc:66
action
int action
Definition: keyboard_key_handler_unittests.cc:116
key
int key
Definition: keyboard_key_handler_unittests.cc:114
callback
FlutterDesktopBinaryReply callback
Definition: flutter_windows_view_unittests.cc:46