First jQuery Mobile does not have any pre-defined event handler for this case.
You will need to figure out the way yourself.
Android
When virtual keyboard is open, it fires windows resize event.
So you can check if the sum of windows width and height changed to detect keyboard is open or not.
iOS
This does not fire resize event, so simply bind focus and blur event as mentioned by @RamizWachtler
So I have some codes for your here:
You just add your own handling code into onKeyboardOnOff()
function.
function onKeyboardOnOff(isOpen) {
// Write down your handling code
if (isOpen) {
// keyboard is open
} else {
// keyboard is closed
}
}
var originalPotion = false;
$(document).ready(function(){
if (originalPotion === false) originalPotion = $(window).width() + $(window).height();
});
/**
* Determine the mobile operating system.
* This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
*
* @returns {String}
*/
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Windows Phone must come first because its UA also contains "Android"
if (/windows phone/i.test(userAgent)) {
return "winphone";
}
if (/android/i.test(userAgent)) {
return "android";
}
// iOS detection from: https://mcmap.net/q/48374/-detect-if-device-is-ios
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
return "ios";
}
return "";
}
function applyAfterResize() {
if (getMobileOperatingSystem() != 'ios') {
if (originalPotion !== false) {
var wasWithKeyboard = $('body').hasClass('view-withKeyboard');
var nowWithKeyboard = false;
var diff = Math.abs(originalPotion - ($(window).width() + $(window).height()));
if (diff > 100) nowWithKeyboard = true;
$('body').toggleClass('view-withKeyboard', nowWithKeyboard);
if (wasWithKeyboard != nowWithKeyboard) {
onKeyboardOnOff(nowWithKeyboard);
}
}
}
}
$(document).on('focus blur', 'select, textarea, input[type=text], input[type=date], input[type=password], input[type=email], input[type=number]', function(e){
var $obj = $(this);
var nowWithKeyboard = (e.type == 'focusin');
$('body').toggleClass('view-withKeyboard', nowWithKeyboard);
onKeyboardOnOff(nowWithKeyboard);
});
$(window).on('resize orientationchange', function(){
applyAfterResize();
});