2010-02-07 89 views
1

我想將NSTrackingArea安裝到全屏視圖中以獲取鼠標移動事件。NSTrackingArea with fullscreen window/view

但是,每當我這樣做,我得到一個斷言錯誤。我搜索了網頁,但一直未能找到任何線索。

*** Assertion failure in -[_NSFullScreenWindow _setTrackingRect:inside:owner:userData:useTrackingNum:install:], /SourceCache/AppKit/AppKit-1038.25/AppKit.subproj/NSWindow.m:3944 

下面是建立跟蹤區域碼(X = 1024,Y = 768):

cocoaWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0.0, 0.0, x,y) 
               styleMask: NSTitledWindowMask 
               backing: NSBackingStoreBuffered 
                defer:NO]; 
    glView = [[WLMacGLView alloc] initWithFrame:NSMakeRect(0.0, 0.0, x,y) pixelFormat:[WLMacGLView defaultPixelFormat]]; 
    [glView setCocoaController:self]; 

    //add the glView as a subview of the window's content view 
    [[cocoaWindow contentView] addSubview:glView]; 
    NSRect r = [glView frame]; 
    NSTrackingArea *track = [[NSTrackingArea alloc] initWithRect:r options: NSTrackingMouseMoved | NSTrackingActiveWhenFirstResponder | NSTrackingActiveInKeyWindow 
            owner:self userInfo:nil]; 
    [glView addTrackingArea:track]; 
    [glView enterFullScreenMode:[NSScreen mainScreen] withOptions:nil]; 
    [glView createContext]; 

斷言調用enterFullScreenMode之後發生的情況:withOptions:

任何人有什麼想法?這是不是我應該採取的方法來獲取全屏窗口中的鼠標移動事件?

回答

0

所以這個問題的答案竟然是我自己的代碼中的一個錯誤。

當初始化NSTrackingArea時,我傳遞了所有者的錯誤對象。正確的事情是NSView。經過糾正,所有工作都按預期進行。

0

如果你想在整個視圖追蹤的鼠標,我想是會更容易實現,以獲取鼠標事件mouseDown:mouseMoved:mouseUp:方法。

+0

這些方法已實現,但在窗口全屏顯示時不會被調用。 – wadesworld 2010-02-08 04:22:53

+1

有一個示例應用程序,您可以看看:http://developer.apple.com/mac/library/samplecode/GeekGameBoard/index.html(它使用NSView子類而不是OpenGL)。 – 2010-02-08 09:30:45

+1

如果您想使用'-mouseMoved:',請確保您調用'[[glView窗口] setAcceptsMouseMovedEvents:YES]'。 – 2010-02-09 01:02:32