2012-03-27 18 views
0

基本上,我想要一個覆蓋整個屏幕的「隱形」NSView。我將添加一個NSTrackingArea,以便在光標在屏幕上移動時獲取全局鼠標事件。在未連接到窗口的NSView中使用NSTrackingArea?

-(void)setTrackingArea 
{ 
    view = [[NSView alloc] initWithFrame:[NSScreen currentScreenForPoint:[NSEvent mouseLocation]].frame]; 

    NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[NSScreen currentScreenForPoint:[NSEvent mouseLocation]].frame options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways owner:view userInfo:nil]; 

    [view addTrackingArea:area]; 

    [area release]; 

    //[[window contentView] addSubview:view]; 

    //I don't want to add the view to a window, as all tutorials say. 
} 

- (void)mouseExited:(NSEvent *)theEvent 
{ 
    NSLog(@"Exit"); //Never firing 
} 

這可能嗎?使用NSViews和NSTracking區域沒有窗口?

+1

你不能有一個觀點,這不是在一個窗口,但你應該能夠創建一個不可見的窗口涵蓋整個屏幕。 – 2012-03-27 08:29:30

回答

2

使用不可見視圖絕對不是你想要做的事情。在NSEvent上查看addGlobalMonitorForEventsMatchingMask::類的方法。

例如,這裏是如何添加一個監視器鼠標的運動:

[NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask handler:^(NSEvent *mouseMovedEvent) { 
    //do something with that event 
}]; 
相關問題