Addev
Addev

Reputation: 32253

Listen the show / hide events for soft keyboard (Windows 8.1 RT, XAML)

How can I know when the soft keyboard is shown/dismissed in my app or within a page?

Found this snipplet in THIS article. But is not valid for WP 8.1 RT. How can I translate it or get a similar behavior?

public class MyApplication
{
    public MyApplication()
    {
        // Grab the input pane for the main application window and attach
        // touch keyboard event handlers.
        Windows.Foundation.Application.InputPane.GetForCurrentView().Showing
            += new EventHandler(_OnInputPaneShowing);
        Windows.Foundation.Application.InputPane.GetForCurrentView().Hiding
            += new EventHandler(_OnInputPaneHiding);
    }

    private void _OnInputPaneShowing(object sender, IInputPaneVisibilityEventArgs eventArgs)
    {
        // If the size of this window is going to be too small, the app uses 
        // the Showing event to begin some element removal animations.
        if (eventArgs.OccludedRect.Top < 400)
        {
            _StartElementRemovalAnimations();

            // Don't use framework scroll- or visibility-related 
            // animations that might conflict with the app's logic.
            eventArgs.EnsuredFocusedElementInView = true;
        }
    }

    private void _OnInputPaneHiding(object sender, IInputPaneVisibilityEventArgs eventArgs)
    {
        if (_ResetToDefaultElements())
        {
            eventArgs.EnsuredFocusedElementInView = true;
        }
    }

    private void _StartElementRemovalAnimations()
    {
        // This function starts the process of removing elements 
        // and starting the animation.
    }

    private void _ResetToDefaultElements()
    {
        // This function resets the window's elements to their default state.
    }
}

Upvotes: 0

Views: 1965

Answers (2)

Addev
Addev

Reputation: 32253

Found the answer, registering for those events is enough:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
        InputPane.GetForCurrentView().Showing += onKeyboardShowing;
        InputPane.GetForCurrentView().Hiding += onKeyboardHidding;
}

private void onKeyboardShowing(InputPane sender, InputPaneVisibilityEventArgs args)
{
    KeyboardVisible = true;
}

private void onKeyboardHidding(InputPane sender, InputPaneVisibilityEventArgs args)
{

    KeyboardVisible = false;
}

Upvotes: 3

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21919

The article you quote is intended for Windows Runtime apps, but it has a minor bug. The InputPane is in Windows.UI.ViewManagement not in Windows.Foundation.Application. Change that and the rest should work fine.

I've reported the doc bug so it can be fixed.

Upvotes: 3

Related Questions