1

I have on UIWebView in that i am allowing editing at runtime.

Now when i click on UIWebView then iPad keyboard opens for writing, now my requirement is how to detect that I have clicked on enter key of iPad keyboard?.

I know that we can achieve this if we are editing in UITextView or UITextField with using it's delegates.Help me!

I have one function in javascript that detects it.

function returnEnterPress(e){
    var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     
          // ENTER KEY MUNBER IS 13 // KD
     if(key == 13)
     {
        //alert('enter');
     }
     return false;
}

Now can i get call it using objective-c code and can get return value like true or false?

Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32

4 Answers4

1

I done my work by javascript like this...

In HTML file

function returnEnterPress(e){
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     
          // ENTER KEY MUNBER IS 13 // KD
     if(key == 13)
     {
        window.location.href = 'enterClicked/0';
     }
     else
     {
         var range = window.getSelection().getRangeAt(0).endOffset;
         if(range == 0)
         {
            window.location.href = 'enterClicked/'+range;
         }
     }
     return true;
}

<body onKeyPress="return returnEnterPress(event)">

In my Objective-c Code

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    if([[[request URL] absoluteString] rangeOfString:@"enterClicked"].location!=NSNotFound)    // When "enterClicked" found
    {
        //Do your desired work
        return NO;
    }
}
Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
0

I don't think there is a public API for dealing with the keyboard at that level. It's quite a big effort (because of international keyboards) but one option would be to listen to UIKeyboardWillShow notifications and overlay the keyboard with transparent UIViews for the buttons you want to listen to. You could capture the touch, then pass it on to the views below (the keyboard) and let it do its thing.

You could even override (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; in your transparent button. So you capture the fact that it's been touch but return NO anyway so that the keyboard gets the touch.

The difficulty is then to know where each key is, depending on the keyboard types you need to support.

mprivat
  • 21,582
  • 4
  • 54
  • 64
0

Use some keyboard notification. i used the following code for detecting the keyboard is going to hide

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nothing)    name:UIKeyboardWillHideNotification object:nil];
}
-(void)nothing
{
     NSLog(@"do Whatever u want");
}

try it.,.

Manikandan
  • 321
  • 2
  • 12
  • I'm not sure this works if the user has a hardware keyboard - if you use this, you should check the simulator using the "Simulate Hardware Keyboard" option. – Mike M Sep 18 '12 at 11:59
  • @MikeM here the Question is if the user clicked on enter key of iPad keyboard. i am answering for the question he asked. add:if the user have hardware keyboard also notification will work. – Manikandan Sep 18 '12 at 12:06
0

You might be able to figure it out using some kind of Javascript but I would just use a form/submit button.

Mike M
  • 4,358
  • 1
  • 28
  • 48