Ivan Zuboff
Ivan Zuboff

Reputation: 57

Cocoa WebView doesn't respond to keyboard events

I'm making super-simple one-page browser in pure Objective-C, and everything is fine except the keyboard events. I just can't enter any data in webforms using keyboard! Mouse operations like pasting in webforms and clicking on links works, but typing in webforms doesn't!

code:

#import <WebKit/WebKit.h>

@interface MyWebView : WebView
{
}
@end

@implementation MyWebView

-(void)windowWillClose:(NSNotification *)notification
{
    [NSApp terminate:self];
}

-(BOOL)becomeFirstResponder
{
    printf("becomeFirstResponder\n"); // this message always appears
    return YES;
}

-(void)keyDown:(NSEvent *)event
{
    printf("keydown\n"); // this message never appears
    // should I use this code to make keyboard work?
    [self interpretKeyEvents:[NSArray arrayWithObject:event]];
}

@end

int main(void) {
    NSRect contentRect;
    NSWindow *window = nil;
    WebView *webView = nil;
    NSString *urlForAuthentication = nil;

    NSApp = [NSApplication sharedApplication];

    contentRect = NSMakeRect(100, 100, 700, 700);
    window = [[NSWindow alloc] initWithContentRect:contentRect styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask backing:NSBackingStoreBuffered defer:NO];
    if (!window) {
        printf("!window\n");
        goto end;
    }
    webView = [[MyWebView alloc] initWithFrame:window.contentLayoutRect frameName:nil groupName:nil];
    if (!webView) {
        printf("!webView\n");
        goto end;
    }

    urlForAuthentication = [[NSString alloc] initWithCString:(const char *)"https://yahoo.com" encoding:NSUTF8StringEncoding];
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlForAuthentication]]];
    window.contentView = webView;
    window.delegate = (id)webView;
    [window makeFirstResponder:webView];
    [window makeKeyAndOrderFront:nil];
    [window makeMainWindow];

    if (!window.keyWindow)
        printf("not keyWindow\n"); // this message always appears
    if (!window.mainWindow)
        printf("not mainWindow\n"); // this message always appears
    [NSApp run];

end:
    [urlForAuthentication release];
    [webView release];
    [window release];
    [NSApp release];
    return 0;
}

makefile:

CC=clang

FRAMEWORKS:=/System/Library/Frameworks/WebKit.framework/WebKit /System/Library/Frameworks/AppKit.framework/AppKit
LIBRARIES:=
WARNINGS=-Wall -Werror

SOURCE=app.m

CFLAGS=-g -v $(WARNINGS) $(SOURCE)
LDFLAGS=$(LIBRARIES) $(FRAMEWORKS) $(LINK_WITH)
OUT=-o app

all:
    $(CC) $(CFLAGS) $(LDFLAGS) $(OUT)

What I'm doing wrong? I've read documentation and searched on SO for similar questions, but I couldn't find the solution. I tried to capture keyDown event, but it doesn't happens. I tried to make my window key and main, but seemingly unsuccessful.

Upvotes: 2

Views: 398

Answers (1)

Ivan Zuboff
Ivan Zuboff

Reputation: 57

To whom it may concern: place your executable 'abc' in folder 'abc.app/Contents/MacOS/', and it will finally work properly!

Edit: my colleague found out that this simple measure affects NSApplicationActivationPolicy. With this path, app's activationPolicy is 0 (NSApplicationActivationPolicyRegular). Without this path, app's activationPolicy is 2 (NSApplicationActivationPolicyProhibited).

Binary with some name should be placed inside "<Insert Name Here>.app" folder, which also should contain "Contents" folder. This way your executable pretends to be a bundled application for macOS.

Upvotes: 1

Related Questions