How to detect keyboard enter key?

痞子三分冷 提交于 2019-12-01 12:22:03

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;
    }
}

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.

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.,.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!