gpt4 book ai didi

objective-c - Cocoa WebView不响应键盘事件

转载 作者:行者123 更新时间:2023-12-03 17:18:57 25 4
gpt4 key购买 nike

我正在用纯 Objective-C 制作 super 简单的单页浏览器,除了键盘事件之外,一切都很好。我只是无法使用键盘在网络表单中输入任何数据!诸如在网络表单中粘贴和单击链接之类的鼠标操作可以工作,但在网络表单中键入则不行!

代码:

#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 事件,但它没有发生。我试图让我的窗口成为关键窗口和主窗口,但似乎不成功。

最佳答案

致相关人员:将可执行文件“abc”放入文件夹“abc.app/Contents/MacOS/”中,它最终将正常工作!

编辑:我的同事发现这个简单的措施会影响 NSApplicationActivationPolicy 。使用此路径,应用程序的activationPolicy为0(NSApplicationActivationPolicyRegular)。如果没有此路径,应用程序的activationPolicy为2(NSApplicationActivationPolicyProhibited)。

具有某些名称的二进制文件应放置在“<在此处插入名称>.app”文件夹中,该文件夹还应包含“Contents”文件夹。这样,您的可执行文件就可以伪装成 macOS 的捆绑应用程序。

关于objective-c - Cocoa WebView不响应键盘事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43322425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com