Prevent keyboard closing

I am struggling a bit with this implementation. I am building my first Hello World! android (cordova), which requires the keyboard to always show and not hide it like when the user hits the back button or any other input.
Why? basically I don't have an input element in my HTML to trigger focus and show the keyboard, it's a kind of "terminal emulator" where the user is executing certain commands.
The keyboard was not showing at all, so I went and I added the following:

Installed Ionic Keyboard plugin

cordova plugin add https://github.com/driftyco/ionic-plugins-keyboard.git

      

Added permission to config.xml

 <feature name="Keyboard">
    <param name="android-package" value="com.ionic.keyboard.IonicKeyboard" />
    <param name="onload" value="true" />
</feature>

      

In my application module, the following lines:

var myApp = angular.module('myApp', ['ionic']);

myApp.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {

        if(window.cordova && window.cordova.plugins.Keyboard) {
            window.cordova.plugins.Keyboard.show(); // Show Keyboard on startup

// and here Trigger a show keyboard when hidden
            window.addEventListener('native.hidekeyboard', keyboardHideHandler); 

            function keyboardHideHandler(e){
                window.cordova.plugins.Keyboard.show();
            }

        }
    });
});

      

Now, the above implementation works, but I don't think it's elegant to handle it this way, and I don't like the feeling of the keyboard closing and then reappearing.

  • Is there an alternative way, besides the Ionic keyboard plugin, to configure my android app to display the keyboard all the time?
  • Is this the right way to do this with cord / ionic structures?

Hope I'm on the right track. Any hints would be appreciated.

thank

Screenshots

enter image description here

+3


source to share


2 answers


EDIT: I think that the standard way of doing this: fooobar.com/questions/126611 / ... . This will not prevent it from closing when you click on the stand, you can try editing Cordova's actual Android files platforms/android

to override the following method (taken from fooobar.com/questions/432171 / ... ):

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        InputMethodManager manager = (InputMethodManager) this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    }
    return false;
}

      


I haven't tested this, but what if you add a hidden input that you focus initially when your app loads, and then continually refocus it when it loses focus? I'm not sure if this will look any different than an explicit call to keyboard.show (), but it might prevent the keyboard opening / closing jitter.

Something like:



<input constant-focus id="hiddenFocus" type="hidden">

      

and then

document.getElementById('hiddenFocus').focus()

      

then refocus it constantly to keep the keyboard up: // HTML -tag = constant-focus

 .directive('constantFocus', function(){
      return {
        restrict: 'A',
        link: function(scope, element, attrs){

          element[0].addEventListener('focusout', function(e){
            element[0].focus();
          });
        }
      };
    })

      

+4


source


Well, I think I came up with another way, but I'm not sure if this is how it should be handled.

Add event listener on faucets

myApp.directive('detectGestures', function ($ionicGesture) {
  return {
    restrict: 'A',
    link: function (scope, elem, attrs) {
        var gestureType = attrs.gestureType;
        switch (gestureType) {
            case 'doubletap':
                $ionicGesture.on('doubletap', scope.reportEvent, elem);
                break;
    }}}
});

      



Then in My Controller if keyboard is Visible close else Show

$scope.reportEvent = function (event) {
      if (event.type == 'doubletap') {
          $timeout(function () {
              if (window.cordova && window.cordova.plugins.Keyboard) {
                  if(cordova.plugins.Keyboard.isVisible){
                      window.cordova.plugins.Keyboard.close();
                  } else {
                      window.cordova.plugins.Keyboard.show();
                  }

              }
           }, 500);
         }
      };

      

Let me know what you think. Thank!

0


source







All Articles