Skip to content
Open
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
34 changes: 32 additions & 2 deletions Sources/OpenKey/macOS/ModernKey/OpenKey.mm
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
NSArray* _unicodeCompoundApp = @[@"com.apple.",
@"com.google.Chrome", @"com.brave.Browser",
@"com.microsoft.edgemac.Dev", @"com.microsoft.edgemac.Beta", @"com.microsoft.Edge.Dev", @"com.microsoft.Edge"];
NSArray* _recommendWorkaroundDisabledApp = @[@"com.apple.Spotlight"];

CGEventSourceRef myEventSource = NULL;
vKeyHookState* pData;
Expand Down Expand Up @@ -173,6 +174,28 @@ BOOL containUnicodeCompoundApp(NSString* topApp) {
}
return false;
}

BOOL isSpotlightVisible() {
NSArray *windows = CFBridgingRelease(CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements,
kCGNullWindowID));
for (NSDictionary *window in windows) {
if ([[window objectForKey:(__bridge NSString *)kCGWindowOwnerName] isEqualToString:@"Spotlight"]) {
return true;
}
}
return false;
}

BOOL shouldUseRecommendWorkaround(NSString* topApp) {
if (!vFixRecommendBrowser) return false;
if (isSpotlightVisible()) return false;
if (topApp == nil) return true;
return ![_recommendWorkaroundDisabledApp containsObject:topApp];
}

BOOL shouldUseSelectionReplacement(NSString* topApp) {
return isSpotlightVisible() || [_recommendWorkaroundDisabledApp containsObject:topApp];
}

void saveSmartSwitchKeyData() {
getSmartSwitchKeySaveData(savedSmartSwitchKeyData);
Expand Down Expand Up @@ -520,7 +543,7 @@ void switchLanguage() {

void handleMacro() {
//fix autocomplete
if (vFixRecommendBrowser) {
if (shouldUseRecommendWorkaround(FRONT_APP)) {
SendEmptyCharacter();
pData->backspaceCount++;
}
Expand Down Expand Up @@ -716,7 +739,7 @@ CGEventRef OpenKeyCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef e
} else if (pData->code == vWillProcess || pData->code == vRestore || pData->code == vRestoreAndStartNewSession) { //handle result signal

//fix autocomplete
if (vFixRecommendBrowser && pData->extCode != 4) {
if (shouldUseRecommendWorkaround(FRONT_APP) && pData->extCode != 4) {
if (vFixChromiumBrowser && [_unicodeCompoundApp containsObject:FRONT_APP]) {
if (pData->backspaceCount > 0) {
SendShiftAndLeftArrow();
Expand All @@ -729,6 +752,13 @@ CGEventRef OpenKeyCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef e

}
}

if (shouldUseSelectionReplacement(FRONT_APP) && pData->backspaceCount > 0) {
for (_i = 0; _i < pData->backspaceCount; _i++) {
SendShiftAndLeftArrow();
}
pData->backspaceCount = 0;
}

//send backspace
if (pData->backspaceCount > 0 && pData->backspaceCount < MAX_BUFF) {
Expand Down
7 changes: 1 addition & 6 deletions Sources/OpenKey/macOS/ModernKey/OpenKeyManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,12 @@ +(BOOL)initEventTap {
// Enable the event tap.
CGEventTapEnable(eventTap, true);

// Set it all running.
CFRunLoopRun();

return YES;
}

+(BOOL)stopEventTap {
if (_isInited) { //release all object
CFRunLoopStop(CFRunLoopGetCurrent());

CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CFRelease(runLoopSource);
runLoopSource = nil;

Expand Down
16 changes: 16 additions & 0 deletions Sources/OpenKey/macOS/ModernKey/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ @implementation ViewController {
__weak IBOutlet NSButton *CustomBeepSound;
NSArray* tabviews, *tabbuttons;
NSRect tabViewRect;
NSView* tabButtonBackground;
}

- (void)viewDidLoad {
Expand All @@ -66,6 +67,16 @@ - (void)viewDidLoad {
//set correct tabgroup
tabviews = [NSArray arrayWithObjects:self.tabviewPrimary, self.tabviewMacro, self.tabviewSystem, self.tabviewInfo, nil];
tabbuttons = [NSArray arrayWithObjects:self.tabbuttonPrimary, self.tabbuttonMacro, self.tabbuttonSystem, self.tabbuttonInfo, nil];
NSButton* firstTabButton = [tabbuttons objectAtIndex:0];
NSRect tabButtonBackgroundRect = firstTabButton.frame;
for (NSButton* button in tabbuttons) {
tabButtonBackgroundRect = NSUnionRect(tabButtonBackgroundRect, button.frame);
}
tabButtonBackgroundRect = NSInsetRect(tabButtonBackgroundRect, -2, -2);
tabButtonBackground = [[NSView alloc] initWithFrame:tabButtonBackgroundRect];
[tabButtonBackground setWantsLayer:YES];
tabButtonBackground.layer.backgroundColor = [[NSColor windowBackgroundColor] CGColor];
[self.view addSubview:tabButtonBackground];
tabViewRect = self.tabviewPrimary.frame;
for (NSBox* b in tabviews) {
b.frame = tabViewRect;
Expand Down Expand Up @@ -137,6 +148,11 @@ -(void)showTab:(NSInteger)index {

NSButton* button = [tabbuttons objectAtIndex:index];
[button setState:NSControlStateValueOn];

[self.view addSubview:tabButtonBackground positioned:NSWindowAbove relativeTo:b];
for (NSButton* tabButton in tabbuttons) {
[self.view addSubview:tabButton positioned:NSWindowAbove relativeTo:nil];
}
}

- (IBAction)onTabButton:(NSButton *)sender {
Expand Down