Flutter macOS Embedder
FlutterKeyboardManager Class Reference

#import <FlutterKeyboardManager.h>

Inheritance diagram for FlutterKeyboardManager:

Instance Methods

(nonnull instancetype) - initWithViewDelegate:
 
(void) - handleEvent:
 
(BOOL) - isDispatchingKeyEvent:
 
(void) - syncModifiersIfNeeded:timestamp:
 
(nonnull NSDictionary *) - getPressedState
 

Detailed Description

Processes keyboard events and cooperate with |TextInputPlugin|.

A keyboard event goes through a few sections, each can choose to handled the event, and only unhandled events can move to the next section:

  • Pre-filtering: Events during IME are sent to the system immediately.
  • Keyboard: Dispatch to the embedder responder and the channel responder simultaneously. After both responders have responded (asynchronously), the event is considered handled if either responder handles.
  • Text input: Events are sent to |TextInputPlugin| and are handled synchronously.
  • Next responder: Events are sent to the next responder as specified by |viewDelegate|.

Definition at line 27 of file FlutterKeyboardManager.h.

Method Documentation

◆ getPressedState

- (nonnull NSDictionary *) getPressedState

Returns the keyboard pressed state.

Returns the keyboard pressed state. The dictionary contains one entry per pressed keys, mapping from the logical key to the physical key.

Definition at line 357 of file FlutterKeyboardManager.mm.

357  {
358  // The embedder responder is the first element in _primaryResponders.
359  FlutterEmbedderKeyResponder* embedderResponder =
360  (FlutterEmbedderKeyResponder*)_primaryResponders[0];
361  return [embedderResponder getPressedState];
362 }

References FlutterEmbedderKeyResponder::getPressedState.

◆ handleEvent:

- (void) handleEvent: (nonnull NSEvent*)  event

Processes a key event.

Unhandled events will be dispatched to the text input system, and possibly the next responder afterwards.

Definition at line 179 of file FlutterKeyboardManager.mm.

179  :(nonnull NSEvent*)event {
180  // The `handleEvent` does not process the event immediately, but instead put
181  // events into a queue. Events are processed one by one by `processNextEvent`.
182 
183  // Be sure to add a handling method in propagateKeyEvent when allowing more
184  // event types here.
185  if (event.type != NSEventTypeKeyDown && event.type != NSEventTypeKeyUp &&
186  event.type != NSEventTypeFlagsChanged) {
187  return;
188  }
189 
190  [_pendingEvents addObject:event];
191  [self processNextEvent];
192 }

◆ initWithViewDelegate:

- (nonnull instancetype) initWithViewDelegate: (nonnull id<FlutterKeyboardViewDelegate>)  viewDelegate
Initial value:
{
NextResponderProvider _getNextResponder

Create a keyboard manager.

The |viewDelegate| is a weak reference, typically implemented by |FlutterViewController|.

Definition at line 116 of file FlutterKeyboardManager.mm.

116  :(nonnull id<FlutterKeyboardViewDelegate>)viewDelegate {
117  self = [super init];
118  if (self != nil) {
119  _processingEvent = FALSE;
120  _viewDelegate = viewDelegate;
121 
122  FlutterMethodChannel* keyboardChannel =
123  [FlutterMethodChannel methodChannelWithName:@"flutter/keyboard"
124  binaryMessenger:[_viewDelegate getBinaryMessenger]
125  codec:[FlutterStandardMethodCodec sharedInstance]];
126 
127  [keyboardChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
128  [self handleKeyboardMethodCall:call result:result];
129  }];
130 
131  _primaryResponders = [[NSMutableArray alloc] init];
132 
133  __weak __typeof__(self) weakSelf = self;
134  [self addPrimaryResponder:[[FlutterEmbedderKeyResponder alloc]
135  initWithSendEvent:^(const FlutterKeyEvent& event,
136  FlutterKeyEventCallback callback,
137  void* userData) {
138  __strong __typeof__(weakSelf) strongSelf = weakSelf;
139  [strongSelf.viewDelegate sendKeyEvent:event
140  callback:callback
141  userData:userData];
142  }]];
143 
144  [self
145  addPrimaryResponder:[[FlutterChannelKeyResponder alloc]
146  initWithChannel:[FlutterBasicMessageChannel
147  messageChannelWithName:@"flutter/keyevent"
148  binaryMessenger:[_viewDelegate
149  getBinaryMessenger]
151  sharedInstance]]]];
152 
153  _pendingEvents = [[NSMutableArray alloc] init];
154  _layoutMap = [NSMutableDictionary<NSNumber*, NSNumber*> dictionary];
155  [self buildLayout];
156  for (id<FlutterKeyPrimaryResponder> responder in _primaryResponders) {
157  responder.layoutMap = _layoutMap;
158  }
159 
160  [_viewDelegate subscribeToKeyboardLayoutChange:^() {
161  [weakSelf buildLayout];
162  }];
163  }
164  return self;
165 }

References <FlutterKeyPrimaryResponder>::layoutMap, FlutterBasicMessageChannel::messageChannelWithName:binaryMessenger:codec:, FlutterMethodChannel::methodChannelWithName:binaryMessenger:codec:, FlutterMethodChannel::setMethodCallHandler:, and <FlutterMessageCodec>::sharedInstance.

◆ isDispatchingKeyEvent:

- (BOOL) isDispatchingKeyEvent: (nonnull NSEvent *)  event

Returns yes if is event currently being redispatched.

In some instances (i.e. emoji shortcut) the event may be redelivered by cocoa as key equivalent to FlutterTextInput, in which case it shouldn't be processed again.

◆ syncModifiersIfNeeded:timestamp:

- (void) syncModifiersIfNeeded: (NSEventModifierFlags)  modifierFlags
timestamp: (NSTimeInterval)  timestamp 

Synthesize modifier keys events.

If needed, synthesize modifier keys up and down events by comparing their current pressing states with the given modifier flags.

Definition at line 344 of file FlutterKeyboardManager.mm.

344  :(NSEventModifierFlags)modifierFlags
345  timestamp:(NSTimeInterval)timestamp {
346  for (id<FlutterKeyPrimaryResponder> responder in _primaryResponders) {
347  [responder syncModifiersIfNeeded:modifierFlags timestamp:timestamp];
348  }
349 }

References <FlutterKeyPrimaryResponder>::syncModifiersIfNeeded:timestamp:.


The documentation for this class was generated from the following files:
+[FlutterBasicMessageChannel messageChannelWithName:binaryMessenger:codec:]
instancetype messageChannelWithName:binaryMessenger:codec:(NSString *name,[binaryMessenger] NSObject< FlutterBinaryMessenger > *messenger,[codec] NSObject< FlutterMessageCodec > *codec)
Definition: FlutterChannels.mm:82
FlutterBasicMessageChannel
Definition: FlutterChannels.h:37
FlutterMethodChannel
Definition: FlutterChannels.h:220
-[FlutterEmbedderKeyResponder getPressedState]
nonnull NSDictionary * getPressedState()
Definition: FlutterEmbedderKeyResponder.mm:796
-[FlutterMethodChannel setMethodCallHandler:]
void setMethodCallHandler:(FlutterMethodCallHandler _Nullable handler)
FlutterChannelKeyResponder
Definition: FlutterChannelKeyResponder.h:20
+[FlutterMethodChannel methodChannelWithName:binaryMessenger:codec:]
instancetype methodChannelWithName:binaryMessenger:codec:(NSString *name,[binaryMessenger] NSObject< FlutterBinaryMessenger > *messenger,[codec] NSObject< FlutterMethodCodec > *codec)
FlutterEmbedderKeyResponder
Definition: FlutterEmbedderKeyResponder.h:23
FlutterStandardMethodCodec
Definition: FlutterCodecs.h:469
+[FlutterMessageCodec-p sharedInstance]
instancetype sharedInstance()
FlutterJSONMessageCodec
Definition: FlutterCodecs.h:81