0
我在純Objective-C中製作超簡單的單頁瀏覽器,除了鍵盤事件以外,一切都很好。我只是無法使用鍵盤在webforms中輸入任何數據!鼠標操作,如粘貼在webforms和點擊鏈接的作品,但輸入webforms不!Cocoa WebView不響應鍵盤事件
代碼:
#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;
}
生成文件:
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)
我做錯了嗎?我已閱讀文檔並在SO上搜索了類似的問題,但我找不到解決方案。我試圖捕獲keyDown事件,但它不會發生。我試圖讓我的窗口關鍵和主要,但看似不成功。
你好,歡迎來到stackoverflow。爲什麼不應該使用簡單的appkit XCode模板?僅僅實現一個webview可能會容易得多。 – Astoria
是的,@Astoria當然你是對的,但在這個小程序的情況下,我寧願不要使用大的XCode!說實話,我不喜歡這個IDE。 –
@Astoria有趣的事實:這個代碼實際上在將它插入XCode模板並刪除發送'release'消息的行之後就開始了。謝謝你的提示,我會更深入地挖掘它! –