将键盘事件从 java 发送到任何应用程序(屏幕键盘)

我正在使用 java 开发屏幕键盘.这个键盘对每个可能的键都有一个 JComponent.当在按钮上检测到鼠标按下时,我想将特定的键盘代码发送到当前处于焦点的应用程序.键盘本身位于 JFrame 内,没有任何装饰,并设置为始终位于顶部.

I am working on developing an on-screen keyboard with java. This keyboard has a JComponent for every possible key. When a mouse down is detected on the button, I want to send a specific keyboard code to the application currently on focus. The keyboard itself is within a JFrame with no decorations and set to always-on-top.

我发现Robot类可以用来模拟原生队列上的这些键盘事件.但是,在这种情况下,选择 JComponent 将意味着在 JFrame 上接收到按键,而我将无法在其他应用程序中接收到它

I found that the Robot class can be used to simulate these keyboard events on the native queue. However, in this case, selecting the JComponent would mean that the key-press is received on the JFrame, and I wouldn't be able to receive it in the other application

如何让我的屏幕键盘始终没有焦点"?是否可以使用另一种方法来发送按键?

How can I keep my on-screen keyboard "Always-without-focus"? Is it maybe possible to use another approach to send the key-press?

推荐答案

显然,做到这一点的唯一方法是拥有一个 JNI 层,该层将使从 java 到 native 的转换.Java 没有简单的方法来提供这样的功能.

Apparently the only way to do this is to have a JNI layer that will make the conversion from java to native. Java has no easy way to provide such funcionality.

对于想要学习 JNI 的小型第三方库来说,这可能是一个有趣的概念...

This could be an interesting concept for a small, third party library for someone who wants to learn JNI...

相关文章