Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate WebView keyboard events

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.

like image 961
kilianc Avatar asked Nov 07 '25 13:11

kilianc


1 Answers

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.

like image 79
auco Avatar answered Nov 12 '25 02:11

auco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!