40static void OnKeyboardLayoutChanged(CFNotificationCenterRef center,
44 CFDictionaryRef userInfo) {
46 if (controller != nil) {
47 [controller onKeyboardLayoutChanged];
51- (instancetype)initWithDelegate:(
id<FlutterKeyboardLayoutDelegate>)delegate {
56 CFNotificationCenterRef cfCenter = CFNotificationCenterGetDistributedCenter();
58 CFNotificationCenterAddObserver(cfCenter, (__bridge
void*)weakSelf, OnKeyboardLayoutChanged,
59 kTISNotifySelectedKeyboardInputSourceChanged, NULL,
60 CFNotificationSuspensionBehaviorDeliverImmediately);
66 CFNotificationCenterRef cfCenter = CFNotificationCenterGetDistributedCenter();
67 CFNotificationCenterRemoveEveryObserver(cfCenter, (__bridge
void*)
self);
70- (void)onKeyboardLayoutChanged {
71 _keyboardLayoutData = nil;
72 [_delegate keyboardLayoutDidChange];
75- (
flutter::LayoutClue)lookUpLayoutForKeyCode:(uint16_t)keyCode shift:(
BOOL)shift {
76 if (_keyboardLayoutData == nil) {
77 _keyboardLayoutData = CurrentKeyboardLayoutData();
79 const UCKeyboardLayout* layout =
reinterpret_cast<const UCKeyboardLayout*
>(
80 CFDataGetBytePtr((__bridge CFDataRef)_keyboardLayoutData));
82 UInt32 deadKeyState = 0;
83 UniCharCount stringLength = 0;
86 UInt32 modifierState = ((shift ? shiftKey : 0) >> 8) & 0xFF;
89 bool isDeadKey =
false;
91 UCKeyTranslate(layout, keyCode, kUCKeyActionDown, modifierState,
keyboardType,
92 kUCKeyTranslateNoDeadKeysBit, &deadKeyState, 1, &stringLength, &resultChar);
94 if (status == noErr && stringLength == 0 && deadKeyState != 0) {
97 UCKeyTranslate(layout, keyCode, kUCKeyActionDown, modifierState,
keyboardType,
98 kUCKeyTranslateNoDeadKeysBit, &deadKeyState, 1, &stringLength, &resultChar);
101 if (status == noErr && stringLength == 1 && !std::iscntrl(resultChar)) {