ios Cordova javascript focus event never comes if KeyboardShrinksView=true

1,764 views
Skip to first unread message

Alexey Pilipchuk

unread,
Oct 1, 2013, 8:39:46 AM10/1/13
to phon...@googlegroups.com
Hello everyone,

Developing iOS application with GWT 2.5.1 and Cordova 2.9.0.0.

Met quite a big issue: upon tapping any text box virtual keyboard appears but no javascript focus event is fired (or any other events except window resize).

So i cannot determine which text box was tapped. Moreover i cannot even tell why window size has changed - because of screen orientation change or keyboard appearance.

document.activeElement points to BODY element.

The second tap (when the keyboard is visible) does fire onFocus event.

Tried on iphone simulators 5.0 and 6.0

The issue happens only when config.xml has KeyboardShrinksView=true. If i set to to false all works fine but whole screen slides up including the title bar which is not acceptable.

So the question is how can i get active text box on the first tap or check if virtual keyboard is visible (without writing phonegap plugin)?

Thanx in advance


Chris Hjorth

unread,
Oct 27, 2013, 4:48:40 PM10/27/13
to phon...@googlegroups.com
I had a similar issue, though on PhoneGap 3.1 and iOS 7. When KeyboardShrinksView=true the focus event was not being registered which is pretty annoying and totally useless in some cases.

This Stack Overflow post gave me the answer. The second answer suggests listening for a tap event, but this is an event only present in jQuery Mobile. As a solution I listen for a touchstart event on the input, then in the handler I call jQuery's .focus() on the input element. Fx:

$('body').on('touchstart', '#your-form input[name="inputname"]', function(event) {
   $
(this).focus();
});

This is just a quick fix and haven't tested it thoroughly. It works on iPad and iPhone simulators on iOS 7, and also on iPhone 4s running iOS 6. Make sure that KeyboardDisplayRequiresUserAction=false otherwise you cannot trigger focus programmatically and of course be careful triggering focus twice in cases where this issue is not present.

Hope this is useful. Cheers.
Reply all
Reply to author
Forward
0 new messages