0

I have a UIWebView which I'm using to render styled HTML text. The content in the web view is editable thanks to contentEditable on the div set to true.

I would like to keep intercepting the key presses by the user so that I can keep resizing the UIWebView as the user types. How do I intercept the key-press events?

Bourne
  • 10,094
  • 5
  • 24
  • 51
  • try [this](http://stackoverflow.com/questions/12361423/is-there-a-way-to-detect-a-touch-on-the-onscreen-keyboard?answertab=votes#tab-top) – johnMa Dec 19 '13 at 08:37
  • @johnMa That's great but how do I differentiate between a delete key press and a normal keypress. In the delete case I'd need to decrease the webview's size and in the latter case I'd need to increase it. – Bourne Dec 19 '13 at 08:42

1 Answers1

2

First define a dummy url

#define kDummyURL @"dummyurl://dummy"

Then in view did load add a javascript

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$(function(){$('input, textarea').keypress(function() {window.location = \"%@\";});})",kDummyURL]];
}

and in should Start load with request try to capture that dummyurl which will be fired by javascript you just added

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if ([[request.URL absoluteString] isEqualToString:kDummyURL]) {
        //key is pressed on HTML page, here you can implement what you want
        return NO;
    }
    return YES:
}
Chahal
  • 995
  • 2
  • 14
  • 24