Java - invoke on-screen keyboard

2019-05-07 10:09发布

问题:

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

回答1:

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.



回答2:

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.