2012-11-13 30 views
6

Tôi muốn tạo ứng dụng OS X hiển thị và tập trung với phím nóng toàn hệ thống, và sau đó, với cùng một phím nóng, nó sẽ biến mất và chuyển tiêu điểm trở lại. Giống như Alfred.Chuyển cụm ca cao sang ứng dụng và sau đó chuyển đổi trở lại

Vấn đề là tôi không thể tập trung lại vào ứng dụng đã sử dụng trước đó. Bằng cách tập trung trở lại, tôi có nghĩa là tôi không thể tiếp tục gõ vào ứng dụng trước đó.

Đây là xử lý phím nóng của tôi:

OSStatus OnHotKeyEvent(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData) 
{ 
    AppDelegate *me = (__bridge AppDelegate*) userData; 

    EventHotKeyID hkCom; 

    GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hkCom), NULL, &hkCom); 

    if([[me window] isVisible]) { 
     [[NSApplication sharedApplication] activateIgnoringOtherApps:NO]; 
     [[me window] orderOut:NULL]; 
    } 
    else { 
     [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; 
     [[me window] makeKeyAndOrderFront:nil]; 

    } 

    return noErr; 
} 

Trả lời

5

cũng kích hoạt trong cả hai trường hợp ... bạn nên tắt. TRƯỚC KHI bạn kích hoạt, lưu ứng dụng hoạt động cũ

 _oldApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; 

sau kích hoạt mà

 [_oldApp activateWithOptions:NSApplicationActivateIgnoringOtherApps]; 

--- nguồn đầy đủ

@implementation DDAppDelegate { 
    NSStatusItem *_item; 
    NSRunningApplication *_oldApp; 
} 

- (void)applicationWillFinishLaunching:(NSNotification *)notification { 
    NSLog(@"%@", [[NSWorkspace sharedWorkspace] frontmostApplication].bundleIdentifier); 

    _item = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength]; 
    _item.title = @"TEST"; 
    _item.target = self; 
    _item.action = @selector(toggle:); 
} 

- (void)applicationWillBecomeActive:(NSNotification *)notification { 
    NSLog(@"%@", [[NSWorkspace sharedWorkspace] frontmostApplication].bundleIdentifier); 
} 

//--- 

- (IBAction)toggle:(id)sender { 
    if(!_oldApp) { 
     NSLog(@"%@", [[NSWorkspace sharedWorkspace] frontmostApplication].bundleIdentifier); 
     _oldApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; 
     [NSApp activateIgnoringOtherApps:YES]; 
    } 
    else { 
     [_oldApp activateWithOptions:NSApplicationActivateIgnoringOtherApps]; 
     _oldApp = nil; 
    } 
} 
@end 
+1

này hoạt động, nhưng lưu ý rằng các frontmostApplication là ứng dụng của bạn theo thời gian thông báo applicationWilLBecomeActive đến. Ứng dụng foremost phải được kiểm tra trong trình xử lý phím nóng. –

Các vấn đề liên quan