简体   繁体   English

Java - 调用屏幕键盘

[英]Java - invoke on-screen keyboard

The application I am working on will run on Windows 7. It will be used to input some information via touchscreen. 我正在处理的应用程序将在Windows 7上运行。它将用于通过触摸屏输入一些信息。 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? 我的问题是,我应该从头开始创建键盘类,还是应该使用Windows 7内置的屏幕键盘?如何在Java应用程序中调用它? Thank you 谢谢

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 . 您只需使用Runtime.exec()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. 从java执行osk.exe有一些困难。 It throws IOException: java.io.IOException: Cannot run program "C:\\Windows\\System32\\osk.exe": CreateProcess error=740, The requested operation requires elevation 它抛出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 ): 诀窍是通过命令shell( cmd )运行命令:

Runtime.getRuntime().exec("cmd /c C:\\\\Windows\\\\System32\\\\osk.exe");

This line works fine on my machine. 这条线在我的机器上工作正常。

I just faced the same problem, in addition I am running a 32bit application on a 64bit Win7. 我刚遇到同样的问题,另外我在64位Win7上运行32位应用程序。 (actually I'm using Matlab, which is based on Java) I both used java command Runtime.getRuntime().exec(....) and Matlab system(....) . (实际上我使用的是基于Java的Matlab)我都使用了java命令Runtime.getRuntime().exec(....)和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) 我的解决方案是从重新指令文件夹sysnative显式启动64位cmd(这对osk.exe是不可能的,这会导致未找到或权限错误)

Runtime.getRuntime().exec('C:\\windows\\sysnative\\cmd /c C:\\Windows\\system32\\osk.exe');

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM