Je suis en train de créer un navigateur d'une seule page en Objective-C, et tout va bien, sauf les événements clavier. Je ne peux pas entrer de données dans des formulaires Web en utilisant le clavier! Les opérations de la souris comme coller dans des formulaires Web et cliquer sur des liens fonctionne, mais pas dans les formulaires Web ne fonctionne pas!Cocoa WebView ne répond pas aux événements clavier
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)
Ce que je fais mal? J'ai lu la documentation et recherché sur SO pour des questions similaires, mais je n'ai pas trouvé la solution. J'ai essayé de capturer l'événement keyDown, mais ça n'arrive pas. J'ai essayé de faire ma clé de la fenêtre et la principale, mais apparemment sans succès.
Bonjour et bienvenue sur stackoverflow. Pourquoi n'utilisez-vous pas un simple template XCode appkit? Il pourrait être beaucoup plus facile d'implémenter simplement un webview. – Astoria
Oui, @Astoria, bien sûr que tu as raison, mais dans le cas de ce petit programme je préférerais ne pas utiliser de gros XCode monstous! Pour être honnête, je n'aime pas cet IDE. –
@Astoria fait amusant: ce code woks juste après l'avoir inséré dans le modèle XCode et en supprimant les lignes qui envoient des messages 'release'. Merci pour un conseil, je vais creuser un peu plus profond! –