0

In my libgdx-project (for android) I am using a TextField. When the TextField receives focus, the soft keyboard appears. Everything is still fine. When now the user hits the BACK-Button of his smartphone, the keyboard disappears. How can I catch that event? Does anyone have any idea or experience with this scenario?

P.S.: I have set Gdx.input.setCatchBackKey(true) which actually only prevents my app from exitting. Therefore it is not firing when the user hits the BACK-Button while the keyboard is visible.

Best, Starcracker

Starcracker
  • 9
  • 1
  • 1

2 Answers2

1

Here is a workaround that seems to do the trick for me:

First, create the following method and call it from your application's onCreate() method:

private static final int KEYBOARD_HEIGHT_THRESHOLD = 150;
private static final int CHECK_KEYBOARD_DELAY = 200; //ms
private void addKeyboardListener() {
        final View rootView = graphics.getView();
        final Rect layoutChangeRect = new Rect();

        rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(CHECK_KEYBOARD_DELAY);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        rootView.getWindowVisibleDisplayFrame(layoutChangeRect);
                        if (Math.abs(rootView.getHeight() - layoutChangeRect.height()) < KEYBOARD_HEIGHT_THRESHOLD) {
                            //keyboard closed
                        } else {
                            //keyboard opened
                        }
                    }
                }).start();
            }
        });

I have used arbitrary values for CHECK_KEYBOARD_DELAY and KEYBOARD_HEIGHT_THRESHOLD that work OK for me. These values seem necessary, since the keyboard is animated.

Hope this helps.

nikos
  • 137
  • 2
  • 9
0

You can create a class that implements the InputProcessor interface and catch the event of the back key being touched. Something like this (based on the example in the wiki):

public class MyInputProcessor implements InputProcessor {
  // ... Rest of the InputProcessor methods
  @Override
   public boolean keyDown(int keycode) {
        if(keycode == Keys.BACK){
           // This will be executed when back button is pressed
           return true; // Means this event has been handled
        }
        return false;
   }
   // ...
}

If all you need is to catch the back key event, then an even cleaner way is to extend InputAdapter which basically allows you to override only the methods you need, instead of implementing the whole InputProcessor interface:

public class MyInputProcessor extends InputAdapter {
  @Override
   public boolean keyDown(int keycode) {
        if(keycode == Keys.BACK){
           // This will be executed when back button is pressed
           return true; // Means this event has been handled
        }
        return false;
   }

Don't forget to set the input processor:

MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);
asherbar
  • 4,024
  • 4
  • 27
  • 47
  • Thank you for your detailed explanation, user2016436. I followed your advice. Unfortunately, the event listener still doesn't detect the hit of the back-button while the soft keybaord is visible. When I hit the back-button, the keyboard shuts down, but no event is fired in my implementation. To ensure that the implemented listener really detects keyDowns at all, I added a print-output to see whether the events It does. – Starcracker May 27 '15 at 11:09
  • ... The event gets correctly fired when I hit any key on the soft keyboard of my test device, but not when I hit the back-button of my test device (I am using a Sony Xperia z2). I wonder, whether it is possible at all to detect and handle that event in my case (?). – Starcracker May 27 '15 at 11:13
  • @Starcracker Maybe try one of the following solutions: http://stackoverflow.com/questions/4312319/howto-capture-the-virtual-keyboard-show-hide-event-in-android and http://stackoverflow.com/questions/3793093/android-edittext-soft-keyboard-show-hide-event – asherbar May 27 '15 at 12:08
  • I basically understand the idea behind your suggested postings (thank you very much for it!), but I wonder, what is an effective way to implement that in a libgdx-project? Should I check (for example) for a changed view-size in the render-function? – Starcracker May 27 '15 at 12:53
  • This doesn't work for me either, when the keyboard is being displayed and the back button is fired - no input. – Oliver Dixon Aug 29 '15 at 16:03