8

SurfaceView というカスタム NSView があります。これは NSWindow の contentView であり、マウス クリックや描画などの基本的なイベントを処理します。しかし、私が何をしてもかまいません。それは keyDown 関数を処理しません。私はすでにacceptsFirstResponderをオーバーライドしましたが、何も起こりません。

重要な場合は、以下に示すカスタム NSEvent ループを使用してアプリケーションを実行します。

NSDictionary* info = [[NSBundle mainBundle] infoDictionary];
NSString* mainNibName = [info objectForKey:@"NSMainNibFile"];

NSApplication* app = [NSApplication sharedApplication];
NSNib* mainNib = [[NSNib alloc] initWithNibNamed:mainNibName bundle:[NSBundle mainBundle]];
[mainNib instantiateNibWithOwner:app topLevelObjects:nil];

[app finishLaunching];

while(true)
{   
    NSEvent* event = [app nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate date] inMode:NSDefaultRunLoopMode dequeue:YES];
    [app sendEvent:event];

    // Some code is execute here every frame to do some tasks...

    usleep(5000);
}

SurfaceView コードは次のとおりです。

@interface SurfaceView : NSView
{
    Panel* panel;
}

@property (nonatomic) Panel* panel;

- (void)drawRect:(NSRect)dirtyRect;
- (BOOL)isFlipped;
- (void)mouseDown:(NSEvent *)theEvent;
- (void)mouseDragged:(NSEvent *)theEvent;
- (void)mouseUp:(NSEvent *)theEvent;
- (void)keyDown:(NSEvent *)theEvent;
- (BOOL)acceptsFirstResponder;
- (BOOL)becomeFirstResponder;

@end

--

@implementation SurfaceView

@synthesize panel;

- (BOOL)acceptsFirstResponder
{
    return YES;
};

- (void)keyDown:(NSEvent *)theEvent
{
    // this function is never called
};

...

@end

ビューを作成する方法は次のとおりです。

NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(left, top, wide, tall) styleMask:NSBorderlessWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask backing:NSBackingStoreBuffered defer:NO];

...

[window makeKeyAndOrderFront:nil];

SurfaceView* mainView = [SurfaceView alloc];
[mainView initWithFrame:NSMakeRect(0, 0, wide, tall)];
mainView.panel = panel;
[window setContentView:mainView];
[window setInitialFirstResponder:mainView];
[window setNextResponder:mainView];
[window makeFirstResponder:mainView];
4

2 に答える 2

26

keyDownイベントの呼び出しを妨げている原因がわかりました。それはNSBorderlessWindowMaskマスクで、ウィンドウがキーとメインウィンドウになるのを防ぎます。NSWindowだから私は呼び出されたのサブクラスを作成しましたBorderlessWindow:

@interface BorderlessWindow : NSWindow
{
}

@end

@implementation BorderlessWindow

- (BOOL)canBecomeKeyWindow
{
    return YES;
}

- (BOOL)canBecomeMainWindow
{
    return YES;
}

@end
于 2012-07-24T20:38:34.753 に答える
2

回答に加えて: の IB チェックボックスをオンにしますNSWindow

Title Barチェックする必要があります。それは似ていますNSBorderlessWindowMask

ここに画像の説明を入力

于 2014-10-21T14:00:22.690 に答える