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.