Skip to content

Commit

Permalink
Merge pull request #12836 from keymanapp/fix/mac/12584-standardize-osk
Browse files Browse the repository at this point in the history
fix(mac): standardize interaction between OSK and physical keyboard
  • Loading branch information
sgschantz authored Dec 19, 2024
2 parents 125b76a + 31280ec commit b47f3a0
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 89 deletions.
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

0 comments on commit b47f3a0

Please sign in to comment.