The application I am working on will run on Windows 7. It will be used to input some information via touchscreen. I need to have an on-screen keyboard popup whenever user is prompted for information. My question is, should I create a keyboard class from scratch or should I use the Windows 7 built-in on-screen keyboard and how would I invoke it in my Java application? Thank you
2 Answers
I have just played with on-screen keyboard and saw that it is easy. You just have to invoke it using either Runtime.exec()
or ProcessBuilder
. Then if your application lost focus take it back to the application, while the active element must be the current editable element (text field, text area etc). Now when user is typing on the virtual keyboard the characters are being written to your application.
EDIT
There are some difficulties to execute osk.exe from java. It throws IOException:
java.io.IOException: Cannot run program "C:\Windows\System32\osk.exe": CreateProcess error=740, The requested operation requires elevation
The trick is to run the command via command shell (cmd
):
Runtime.getRuntime().exec("cmd /c C:\\Windows\\System32\\osk.exe");
This line works fine on my machine.

- 114,158
- 16
- 130
- 208
-
Alex I have one more question. When I implement String sysroot = System.getenv("SystemRoot") + "/system32"; Process proc = Runtime.getRuntime().exec(sysroot + "/osk.exe"); I receive an error message "Could not start on-screen keyboard." Would you happen to know why I receive this? – jadrijan Feb 14 '12 at 18:56
-
Also, if I use notepad.exe instead of osk.exe, notepad runs okay. Maybe its got something to do with permissions? – jadrijan Feb 14 '12 at 19:03
-
@jadrijan, sorry for delay. You are right, but I found the trick. Run command line with prefix `cmd /c`. Please see my edited answer. – AlexR Feb 14 '12 at 19:36
-
@AlexR I used your `cmd /c` tipp to get the osk working under win8. Is there a possibility to also close it again? Under other windows' I use osk directly (not over `cmd /c)` and therefore can just destroy the process gain. With that trick, this doesn't work anymore. :( – brimborium May 24 '13 at 15:14
-
The reason I want to do this: When someone clicks on a textfield, I want the osk to appear, when someone clicks on it again, I want it to disappear again (also on lost focus of the element). Is there another way to implement this maybe? – brimborium May 24 '13 at 15:16
I just faced the same problem, in addition I am running a 32bit application on a 64bit Win7.
(actually I'm using Matlab, which is based on Java)
I both used java command Runtime.getRuntime().exec(....)
and Matlab system(....)
. The behaviour was the same: The on-screen keyboard could not be started.
I was not able to find any working solution on google, so I tried to mesh up two ideas and upgrade the answer above:
My solution was to explicit start a 64 bit cmd from the re-directive folder sysnative (which is not possible for osk.exe, this causes a not-found or permissions error)
Runtime.getRuntime().exec('C:\windows\sysnative\cmd /c C:\Windows\system32\osk.exe');
I hope this helps.

- 31
- 2