Java - 调用屏幕键盘

发布于 2025-01-05 10:33:32 字数 139 浏览 1 评论 0原文

我正在开发的应用程序将在 Windows 7 上运行。它将用于通过触摸屏输入一些信息。每当提示用户输入信息时,我需要弹出一个屏幕键盘。我的问题是,我应该从头开始创建一个键盘类还是应该使用 Windows 7 内置屏幕键盘以及如何在 Java 应用程序中调用它?谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满天都是小星星 2025-01-12 10:33:32

我刚刚玩过屏幕键盘,发现这很简单。您只需使用 Runtime.exec() 或 ProcessBuilder 来调用它。然后,如果您的应用程序失去焦点,请将其带回应用程序,而活动元素必须是当前可编辑元素(文本字段、文本区域等)。现在,当用户在虚拟键盘上键入时,字符将被写入您的应用程序。

编辑

从java执行osk.exe有一些困难。它抛出 IOException:
java.io.IOException: 无法运行程序“C:\Windows\System32\osk.exe”: CreateProcess error=740, 请求的操作需要提升

技巧是通过命令 shell 运行命令( cmd):

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

此行运行良好我的机器。

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.

·深蓝 2025-01-12 10:33:32

我刚刚遇到了同样的问题,此外我正在 64 位 Win7 上运行 32 位应用程序。
(其实我用的是Matlab,它是基于Java的)
我都使用了 java 命令 Runtime.getRuntime().exec(....) 和 Matlab system(....)。行为是相同的:无法启动屏幕键盘。
我无法在谷歌上找到任何可行的解决方案,所以我尝试将两个想法结合起来并升级上面的答案:

我的解决方案是从重定向文件夹sysnative显式启动64位cmd(这对于osk来说是不可能的) .exe,这会导致未找到或权限错误)

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

我希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文