1

I tried all answers from here:

open the Windows virtual keyboard in a Java program

I executed Runtime.getRuntime().exec("cmd /c osk"); and I cant close it as advised and I need to close it after some processes.

I tried:

import java.io.IOException;

public class ShowVirtualKeyboard{

    public static void main(String argv[]) throws IOException {
    String sysroot = System.getenv("SystemRoot");
    Process proc = Runtime.getRuntime().exec(sysroot + "/system32/osk.exe");
}
}

and I got the error

Cannot run program "C://Windows/system32/osk.exe": CreateProcess error=740, The requested operation requires elevation
2

1 Answer 1

1

I think is a little bit late but I managed to open and close the virtual keyboard (TabTip.exe) using cmd as you do and JNA (Java Native Access) library:

To open the keyboard:

// Java

String command = "cmd /c \"C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\TabTip.exe\"";
Process = Runtime.getRuntime().exec(command);
process.waitFor();
process.destroy();

// Kotlin

val command = "cmd /c \"C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\TabTip.exe\""
val process = Runtime.getRuntime().exec(command)
process.waitFor()
process.destroy()

To close it:

//Java

String KEYBOARD_CLASS_NAME = "IPTIP_Main_Window";
int WM_SYS_COMMAND = 0x0112;
WinDef.WPARAM SC_CLOSE = WinDef.WPARAM(0xF060);
WinDef.HWND handle = User32.INSTANCE.FindWindow(KEYBOARD_CLASS_NAME, "");
if (handle != null) {
    User32.INSTANCE.SendMessage(handle, WM_SYS_COMMAND, SC_CLOSE, WinDef.LPARAM(0));
}

//Kotlin

val KEYBOARD_CLASS_NAME = "IPTIP_Main_Window"
val WM_SYS_COMMAND = 0x0112
val SC_CLOSE = WinDef.WPARAM(0xF060)
val handle = User32.INSTANCE.FindWindow(KEYBOARD_CLASS_NAME, "")
if (handle != null) {
    User32.INSTANCE.SendMessage(handle, WM_SYS_COMMAND, SC_CLOSE, WinDef.LPARAM(0))
}

Closing the process or killing it from cmd was not working for me so I had to do it in the native way. I hope this works for you too.

Not the answer you're looking for? Browse other questions tagged or ask your own question.