Git Product home page Git Product logo

phonegap-softkeyboard's Introduction

PhoneGap-SoftKeyboard

Android SoftKeyboard Plugin for PhoneGap.

NOTE: The development of this plugin is discontinued.

Usage

// Init callback for toggle event (every time when softkeyboard is opened or closed the function will be called)
SoftKeyboard.onToggle(function(softKeyBoardHeight) {
  if (softKeyBoardHeight === 0) {
    console.log('Keyboard is closed');
  } else {
    console.log('Keyboard is opened and its height is: ' + softKeyBoardHeight);
  }
});

// Open the soft keyboard.
SoftKeyboard.show();

// Close the soft keyboard.
SoftKeyboard.hide();

// Check whether the keyboard is open or closed.
SoftKeyboard.isShowing(function(isShowing){
  if (isShowing) {  
    console.log('soft keyboard open');
  } else {
    console.log('soft keyboard closed');
  }
}, function(){ 
  console.log('error');
})

HINT: (undocumented cordova callbacks) To have callback for hiding and showing the keyboard you can use: "showkeyboard" / "hidekeyboard" events wich are not mentioned in the docs.

Example for ExtJS/Sencha touch

if (Ext.os.is.Android) {

  SoftKeyboard.onToggle(function(keyboardHeight) {

      // get focused element and body
      var focusedElement = Ext.ComponentQuery.query('input{_isFocused}'),
          body = Ext.DomQuery.select('body');

      // there is nothing to do if there is no body element
      if (!body[0]) {
          return;
      }


      // if there is focused element
      if (focusedElement[0]) {

          // get:
          //  - focused element Y position
          //  - status bar height
          //  - keyboard height
          //  - available area (what is available after keyboard appeared)
          //  - slide height (the amount of slideing up in pixels)
          var fePosition = focusedElement[0].element.getY(),
              statusBarHeight = 50/window.devicePixelRatio,
              softKeyHeight = (keyboardHeight/window.devicePixelRatio) - statusBarHeight,
              availableArea = (Ext.getBody().dom.clientHeight - softKeyHeight)/2,
              slideHeight = fePosition - availableArea;

          // if view would be over slided we should set limit it to keyboard height
          if (slideHeight >= softKeyHeight) {
              slideHeight = softKeyHeight;
          }

          // check if sliding is needed (keyboard would hide the focused input element)
          if (fePosition >= softKeyHeight) {

              if (slideHeight <= 0) {

                  slideHeight = 0;

              }  else  {

                  slideHeight = slideHeight * -1;

              }

              // do the sliding
              body[0].style.webkitTransform = "translateY(" + slideHeight + "px)";
              body[0].style.webkitTransitionDuration = "150ms";

          }


      } else {

          //slide back if there is no focused element
          body[0].style.webkitTransform = "translateY(0px)";

      }

  });
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.