Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mac): standardize interaction between OSK and physical keyboard #12836

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion mac/Keyman4MacIM/Keyman4MacIM/KMInputMethodAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ - (void)registerConfigurationWindow:(NSWindowController *)window {
}

- (void)showOSK {
[self.oskWindow prepareToShowOsk];
[[self.oskWindow window] makeKeyAndOrderFront:nil];
[[self.oskWindow window] setLevel:NSStatusWindowLevel];
[[self.oskWindow window] setTitle:self.oskWindowTitle];
Expand Down Expand Up @@ -1242,13 +1243,32 @@ - (NSEventModifierFlags) determineModifiers {
NSEventModifierFlags modifierFlags = 0;

if (self.receivedKeyDownFromOsk) {
/**
* the event was generated from the OSK, so use the oskEventModifiers in effect at time of event generation
*/
modifierFlags = self.oskEventModifiers;
os_log_debug([KMLogs eventsLog], "--- use modifiers from OSK, oskEventModifiers: 0x%lX", (unsigned long)modifierFlags);
self.oskEventModifiers = 0;
} else {
} else {
/**
* the event originated from the physical keyboard, use the modifiers on kCGEventFlagsChanged events
*/
NSEventModifierFlags originalModifiers = self.currentModifiers;
modifierFlags = [self.modifierMapping adjustModifiers:originalModifiers];
os_log_debug([KMLogs eventsLog], "--- use adjusted modifiers from current state: 0x%lX", (unsigned long)modifierFlags);

/**
* If the OSK is open, then we also need to apply its modifiers, if any, to this event originating from the physical keyboard.
* If the OSK is closed, its modifiers will be zero.
*/
NSEventModifierFlags oskModifiers = [self.oskWindow getOskEventModifierFlags];
if (oskModifiers != 0) {
os_log_debug([KMLogs eventsLog], "--- modifiers from OSK to apply to physical keyboard event: 0x%lX", (unsigned long)oskModifiers);

// combine osk modifiers with adjusted modifiers
modifierFlags = oskModifiers | modifierFlags;
os_log_debug([KMLogs eventsLog], "--- combined modifiers to apply to physical keyboard event: 0x%lX", (unsigned long)modifierFlags);
}
}

return modifierFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

@property (nonatomic, weak) IBOutlet OSKView *oskView;

- (void)prepareToShowOsk;
- (void)resetOSK;
- (NSEventModifierFlags)getOskEventModifierFlags;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,16 @@ - (void)windowDidResize:(NSNotification *)notification {
[self.oskView resizeOSKLayout];
}

- (void)prepareToShowOsk {
os_log_info([KMLogs oskLog], "OSKWindowController prepareToShowOsk");
}

- (void)windowWillClose:(NSNotification *)notification {
os_log_debug([KMLogs oskLog], "OSKWindowController windowWillClose");
[KMSettingsRepository.shared writeShowOskOnActivate:NO];
os_log_debug([KMLogs oskLog], "OSKWindowController windowWillClose, updating settings writeShowOsk to NO");

// whenever the OSK is closing clear all of its modifier keys
[self.oskView clearOskModifiers];
}

- (void)helpAction:(id)sender {
Expand Down Expand Up @@ -97,6 +104,16 @@ - (void)resetOSK {
}
}

/**
* returns current event flags representing the state of the modifiers on the OSK or zero if OSK is closed
*/
- (NSEventModifierFlags)getOskEventModifierFlags {
if (self.window.isVisible) {
return [self.oskView getOskEventModifierFlags];
} else {
return 0;
}
}
- (void)startTimerWithTimeInterval:(NSTimeInterval)interval {
if (_modFlagsTimer == nil) {
TimerTarget *timerTarget = [[TimerTarget alloc] init];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
- (void)setOskOptionState:(BOOL)oskAltState;
- (void)setPhysicalControlState:(BOOL)ctrlState;
- (void)setOskControlState:(BOOL)oskCtrlState;
- (void)clearOskModifiers;
- (NSEventModifierFlags)getOskEventModifierFlags;
- (void)resetOSK;
- (void)resizeOSKLayout;
- (int64_t)createOskEventUserData;
Expand Down
Loading
Loading