Java - invoke on-screen keyboard

邮差的信 提交于 2019-12-10 07:48:02

问题


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.



来源:https://stackoverflow.com/questions/9281315/java-invoke-on-screen-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!