5

I have the following problem, I need to disable the native Keyboard completely. The keyboard should only show when I call the show() and hide when I call the close() function (this will be on a Button for the user to toggle the Keyboard). The Keyboard showing on clicking the Field and on Focus needs to be completely disabled.

On Stackoverflow I found this: InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(editText.getWindowToken(), 0);

So my thought was In the "Init"(Line 52 in the IonicKeyboard.java) I need to change it.

if ("init".equals(action)) {
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {

                  //new Logic on init
                  View v = cordova.getActivity().getCurrentFocus();
            ((InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);

              DisplayMetrics dm = new DisplayMetrics();
                cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
                final float density = dm.density;

                //http://stackoverflow.com/a/4737265/1091751 detect if keyboard is showing
                final View rootView = cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView();
                OnGlobalLayoutListener list = new OnGlobalLayoutListener() {
                    int previousHeightDiff = 0;
                    @Override
                    public void onGlobalLayout() {
                        Rect r = new Rect();
                        //r will be populated with the coordinates of your view that area still visible.
                        rootView.getWindowVisibleDisplayFrame(r);

                        PluginResult result;

                        int heightDiff = rootView.getRootView().getHeight() - r.bottom;
                        int pixelHeightDiff = (int)(heightDiff / density);
                        if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
                            String msg = "S" + Integer.toString(pixelHeightDiff);
                            result = new PluginResult(PluginResult.Status.OK, msg);
                            result.setKeepCallback(true);
                            callbackContext.sendPluginResult(result);
                        }
                        else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){
                            String msg = "H";
                            result = new PluginResult(PluginResult.Status.OK, msg);
                            result.setKeepCallback(true);
                            callbackContext.sendPluginResult(result);
                        }
                        previousHeightDiff = pixelHeightDiff;
                     }
                };

                rootView.getViewTreeObserver().addOnGlobalLayoutListener(list);


                PluginResult dataResult = new PluginResult(PluginResult.Status.OK);
                dataResult.setKeepCallback(true);
                callbackContext.sendPluginResult(dataResult);
            }
        });
        return true;
    }
    return false;  // Returning false results in a "MethodNotFound" error.
}

Sadly this does not work at all...

I think I have to change the close / show function too but I am not really sure what the correct code is, and I dont know if this change would affect other Keyboard behaviour. (But I basically need Focus without Keyboard)

I also found this Cordova Plugin

which looks very promising, but I decided to change this in the Ionic Keyboard Plugin, because I need the same behaviour in Windows too. Very glad if someone could help me out here.

Regards Christopher

stackg91
  • 716
  • 6
  • 22
  • make elements readonly using `ngReadonly` so that they cannot be focused and therefore wont trigger keyboard? then enable focus when you call keyboard show manually.. – mani Apr 25 '16 at 17:14
  • The main Problem is I need a Focus in the Field because I get external Input via Scanner. So I need Focus in the Field but Keyboard only when the user wants to show or hide via button click – stackg91 Apr 26 '16 at 11:12
  • @stackg91 did you had a look at this link - http://stackoverflow.com/questions/10611833/how-to-disable-keypad-popup-when-on-edittext – Gandhi Apr 28 '16 at 14:56
  • I pretty much tried everything described therer from changing the SoftInput in the Manifest, to calling the InputMethodManager in the init function of the Ionic Keyboard plugin but nothing seemed to work, I am not even sure if the changes i made even take effect since the behaviour didnt change at all but I only know about the Manifest and the Keyboard plugin which could change the behaviour – stackg91 Apr 29 '16 at 08:10
  • And I tried pretty much every possibility from stateHidden, stateAlwaysHidden, 0, Hide_implicit – stackg91 Apr 29 '16 at 08:17
  • Hello @stackg91, where you successful with this task? I have exactly the same problem and I couldn't make it work so far. – Francisco Fiuza Oct 10 '16 at 13:59
  • Hey @FranciscoFiuza sadly I was not at all able to disable the keyboard the only working solution was to install a null keyboard I maybe need to create my own keyboard view or even keyboard app to handle this problem, but It already cost me so much time to investigate and try stuff to get it working. If you find anything poke me – stackg91 Oct 10 '16 at 14:47

1 Answers1

0

This can be accomplished by disabling all inputs with ng-disabled and later showing the keyboard with the Ionic Keyboard Plugin.

If you do not want the inputs to appear differently when disabled, you can override this style with CSS using the :disabled selector.

Hide on load:

<input ng-disabled="hideKeyboard">
hideKeyboard = true;

Show on function:

<input ng-disabled="hideKeyboard">
function showKeyboard(){
  hideKeyboard = false;
  cordova.plugins.Keyboard.show();
}
Tyler
  • 2,924
  • 2
  • 20
  • 29
  • Well but i still need a focus without a keyboard when i disable the field i wont get a focus right? – stackg91 Apr 28 '17 at 05:08
  • True, but if you don't have a keyboard why do you need to focus? To paste text? – Tyler Apr 28 '17 at 06:07
  • scanning Barcodes from the physical Scanner and there are devices with physical keyboard where i dont need to see the softkeyboard – stackg91 Apr 28 '17 at 06:41
  • You can change the value of a disabled input with a function, such as in the callback of your barcode scanner. I see where this could potentially be an issue if you have a physical keyboard though since I'm yay case you would need to focus on an item. – Tyler Apr 28 '17 at 13:58
  • Are you sure that there is not a native button that allows the keyboard to be hidden when a physical keyboard is connected? I seem to remember being able to show or hide the soft keys when I had last used Bluetooth keyboard. – Tyler Apr 28 '17 at 13:59
  • Hey the problem is without focus i dont know where to inout the data from the scan lets say there are 4 inputfields wo focus i dont know where the user wants to put data in. And yes some devices have a native switch to physical keyboard button in settings but those phys keyboards dont support all characters and are only good for numbers so conpletely disabling the softkeyboard was not the intension – stackg91 Apr 28 '17 at 16:05
  • As for where to input the data from a scan, you could make the disabled inputs clickable and save the clicked input's ID as a variable. Then when you perform a scan you can reference this ID and write the scanned data to that input. – Tyler Apr 28 '17 at 17:14
  • Well thats a possibility but regarding usability its not that optimal – stackg91 Apr 29 '17 at 06:47