How to get keyboard height?

I’m trying to calculate the amount of ios app space in the screen, I specifically need the height of the keyboard when it is out.

I’m trying to do it like so:

I’m using libgdx and this is what I got so far, but “keyboardWillChange” is not getting called.

public IOSVisibleScreenArea() {
    NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(
            IOSMoeLauncher.app,
            new SEL("keyboardWillChange:"),
            "UIKeyboardDidShowNotification",
            null);
}

@Selector("keyboardWillChange:")
void keyboardWillChange(NSNotification notification) {
    NSValue keyboardFrameBegin = (NSValue) notification.userInfo().valueForKey("UIKeyboardFrameBeginUserInfoKey");
    System.out.println("KWC WIDTH: " + keyboardFrameBegin.CGRectValue().size().width());
    System.out.println("KWC HEIGHT: " + keyboardFrameBegin.CGRectValue().size().height());
}

Hey Ricardo,

Have found a solution for this yet?
I am now facing the same problem and sadly, no one answered your post.

If you have a solution, I would be happy if you could share it with me!

Best regards,
David

Hi!

Please try this code:

public class IOSVisibleScreenArea extends NSObject {

    protected IOSVisibleScreenArea(Pointer peer) {
        super(peer);
    }

    @Owned
    @Selector("alloc")
    public static native IOSVisibleScreenArea alloc();

    @Selector("init")
    public IOSVisibleScreenArea init() {
        IOSVisibleScreenArea visibleScreenArea = (IOSVisibleScreenArea) super.init();
        visibleScreenArea.addObserver();
        return visibleScreenArea;
    }

    private void addObserver() {
        NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(
                this,
                new SEL("keyboardWillChange:"),
                UIKit.UIKeyboardDidShowNotification(),
                null);
    }

    @Selector("keyboardWillChange:")
    void keyboardWillChange(NSNotification notification) {
        NSValue keyboardFrameBegin = (NSValue) notification.userInfo().valueForKey(UIKit.UIKeyboardFrameBeginUserInfoKey());
        System.out.println("KWC WIDTH: " + keyboardFrameBegin.CGRectValue().size().width());
        System.out.println("KWC HEIGHT: " + keyboardFrameBegin.CGRectValue().size().height());
    }
}

Best Regards,
Roland

2 Likes

Thank you Roland, this finally worked for me!

Best Regards,
David