how to close the keyboard after the focus of a textBox is lost?

4

also if you can indicate in which position of the screen where it appears

    static string dirTeclado = "C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";

    public static void ShowTeclado()
    {
        Process.Start(dirTeclado);
    }
    public static void hideTeclado()
    {
        //Cerrar Teclado
    }
    
asked by Luis Alberto Acosta 30.01.2018 в 21:13
source

2 answers

0

This depends on some things, like the architecture or the Windows version, I leave the case of Windows 10 x64:

First: You must go to the properties of the project and in BUILD / Compile you must place the Platform Target / Destination of platform in X64 and not in AnyCPU, if you do not do this it is possible that what follows does not work.

Second: You open the keyboard like this:

Process.Start("osk.exe");

And to close it you can do it like this (with clear try-catch):

foreach (var process in Process.GetProcessesByName("osk"))
{
   process.Kill();
}

Now, there are several points to consider as well, for example:

It is possible that on certain computers you must run the software with Administrator privileges so you can close the on-screen keyboard

EDIT: The event to close the keyboard is located in the LostFocus, like this:

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (var process in Process.GetProcessesByName("osk"))
                {
                    process.Kill();
                }
            }
            catch(Exception ex)
            {
               //algo  falló al intentar cerrar el teclado  
            }
        }

Greetings.

    
answered by 31.01.2018 в 19:23
0

I think something like this will help you:

private static string dirTeclado = "C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
private static Process process = null;

public static void ShowTeclado()
{
    if (process != null) HideTeclado();
    Process.Start(dirTeclado);
}
public static void HideTeclado()
{
    if (process != null) {
        try {
            process.kill();
        } catch() { }
        process = null;
    }
}

Good luck!

    
answered by 11.10.2018 в 19:59