how to hide on EditText soft keyboard windows 8 Metro Application?
Asked Answered
M

7

1

I'm having an EditText and a Button in my Frame using C#. After writing inside the edit field and clicking on the Button, I want to hide the virtual soft keyboard.

Magdala answered 23/5, 2012 at 6:12 Comment(2)
This is not possible with Win8 CP. Possible Duplication: #10130050Forbear
if any other alternative in this question.Magdala
R
2

You cannot. There is more information on the behavior of the Input Hosting Manager and Soft Keyboard and you can register to know when it shows or becomes hidden. But, you cannot programmatically control whether it's up or down.

Responsory answered 24/5, 2012 at 18:22 Comment(1)
I do not understand why this is the accepted answer. Setting focus on a hidden button as Robert suggests below works perfectly. One have to catch some of the corner cases but the final user experience is much better...Khalid
N
7

Add a dummy button and set focus to it and the keyboard will be hidden.

Nestor answered 17/9, 2012 at 11:53 Comment(1)
I did this but set IsTabStop="true" and set the Height/Width to "0", rather than Visibility="Collapsed". That was the magic combination of settings for me.Knowle
W
3

Thanks for your question. i have get a better solution for this problem. like this

first we can add handler in xaml

<Grid x:Name= Tapped="Grid_Tapped_1">
  ......
 </Grid >

then we focus current page like follow. it works well.

private void Grid_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            this.Focus(FocusState.Programmatic);
        }
Welladvised answered 10/6, 2013 at 2:46 Comment(0)
R
2

You cannot. There is more information on the behavior of the Input Hosting Manager and Soft Keyboard and you can register to know when it shows or becomes hidden. But, you cannot programmatically control whether it's up or down.

Responsory answered 24/5, 2012 at 18:22 Comment(1)
I do not understand why this is the accepted answer. Setting focus on a hidden button as Robert suggests below works perfectly. One have to catch some of the corner cases but the final user experience is much better...Khalid
G
2

When the textbox that showed the virtual keyboard has it’s propery IsEnabled set to false, the virtual keyboard disappears. We can immediately set is to true after that and the virtual keyboard will remain hidden. Just like this:

MyTextBox.KeyDown += (s, a) => {
    if (a.Key == VirtualKey.Enter) {
        MyTextBox.IsEnabled = false;
        MyTextBox.IsEnabled = true;
    }
};
Gilman answered 2/5, 2014 at 20:13 Comment(0)
D
0

Try to set the IsReadOnly property of the Textbox`.

I'm doing something "similar"

    private void textbox_input_LostFocus(object sender, RoutedEventArgs e)
    {
        textbox_input.IsReadOnly = false;
    }

    private void textbox_input_Tapped(object sender, TappedRoutedEventArgs e)
    {
        if(e.PointerDeviceType != Windows.Devices.Input.PointerDeviceType.Mouse)
            textbox_input.IsReadOnly = true;
        else
            textbox_input.IsReadOnly = false;
    }

With this snipped I suppress the keyboard if the user isn't using the mouse...

Also the KeyDown event is fired while the textbox is readonly so you could use the data directly to set your viewmodel and update over it your textbox ;)

Duntson answered 25/6, 2012 at 18:34 Comment(0)
M
0

There is a solution which can hide the touch-keyboard by setting the container's IsTabStop=true automaticly after clicking your Button as "submit".

But, btw, I've noticed that the next time entering that page, the EditText (supposed to be a TextBox) will be auto-focused, and have the touch-keyboard showed. Maybe you'd better Disable the EditText after submitting. (seems to finish and block the input operation)

Monoatomic answered 25/3, 2015 at 1:34 Comment(0)
T
0

I had the same problem, only with a little difference. When I switched from a textbox to a datepicker the softkeyboard won't disappear.

I tried all of your suggestions, but nothing worked like it should. Every time my datepicker had a strange behaviour, after I tried one of the above solutions (Or some of other stackoverflow threads).

After some time I found something via Google, which worked like a charm. HERE

In the comment section Dusher16 wrote a very clean solution, which works also for WinRT / Win8 / Win8.1 / Metro or how you will call it.

Create a new class:

using System.Runtime.InteropServices;
using Windows.Devices.Input;

namespace Your.Namespace
{
    public static class TouchKeyboardHelper
    {
        #region < Attributes >

        private const int WmSyscommand = 0x0112; // Flag to received/send messages to the system.
        private const int ScClose = 0xF060; // Param to indicate we want to close a system window.

        #endregion < Attributes >

        #region < Properties >

        public static bool KeyboardAttached
        {
            get { return IsKeyboardAttached(); }
        }

        #endregion < Properties >

        #region < Methods >

        [DllImport("user32.dll")]
        private static extern int FindWindow(string lpClassName, string lpWindowName); // To obtain an active system window handler.

        [DllImport("user32.dll")]
        private static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam); // To send a message to the system.

        /// <summary>
        /// To detect if a real keyboard is attached to the dispositive.
        /// </summary>
        /// <returns></returns>
        private static bool IsKeyboardAttached()
        {
            KeyboardCapabilities keyboardCapabilities = new KeyboardCapabilities(); // To obtain the properties for the real keyboard attached.
            return keyboardCapabilities.KeyboardPresent != 0 ? true : false;
        }

        /// <summary>
        /// To close the soft keyboard
        /// </summary>
        public static void CloseOnscreenKeyboard()
        {
            // Retrieve the handler of the window 
            int iHandle = FindWindow("IPTIP_Main_Window", ""); // To find the soft keyboard window.
            if (iHandle > 0)
            {
                SendMessage(iHandle, WmSyscommand, ScClose, 0); // Send a close message to the soft keyboard window.
            }
        }

        #endregion < Methods >
    }
}

And in for example some XAML.cs file you add the following lines:

private void DatePicker_GotFocus(object sender, RoutedEventArgs e)
{
    if (TouchKeyboardHelper.KeyboardAttached)
        TouchKeyboardHelper.CloseOnscreenKeyboard();
}
Throne answered 11/10, 2016 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.