开发者

Activate WebView keyboard events

开发者 https://www.devze.com 2023-04-06 09:27 出处:网络
I have a webview inside a simple/plain NSWindow. When i load a url inside the WebView like \"http://google.com\" i can\'t interact with textfield selecting all characters (cmd+a), cutting/pasting (cm

I have a webview inside a simple/plain NSWindow.

When i load a url inside the WebView like "http://google.com" i can't interact with textfield selecting all characters (cmd+a), cutting/pasting (cmd+cv) and so on.

How can i activate the default browser behaviors? My mac emit the classic NSBeep() instead.

I tried adding a UIDelegate wich call

- (BOOL)webView:(WebView *)webView shouldPerformAction:(SEL)action fromSender:(id开发者_开发问答)sender`

but the action is never called.


You have to subclass WebView or any view above in the view hierarchy, where your WebView is a subview and override the NSResponder methods of NSView; in particular acceptFirstResponder: and canBecomeFirstResponder:. Then implement the keyUp: or keyDown: or whatever event method of NSResponder you're interested in.

This should at least omit the NSBeep.

To intercept events deeper inside WebView (for cut/paste/etc), WebView itself also provides a couple of methods to override, though I think there must be additional steps taken to intercept events accordingly. Sorry for being vague here.

The webView:shouldPerformAction:fromSender you mention is not what you'd expect it to be. The WebUIDelegate Protocol says that this method "Returns a Boolean value that indicates whether the action sent by the specified object should be performed." - But the WebUIDelegationProtocol methods will only be "invoked as a result of handling JavaScript or other plug-in content". That means shouldPerformAction: will only be called if triggered by the website itself (JS or plugins) and not if an NSResponder Event occurs.

0

精彩评论

暂无评论...
验证码 换一张
取 消