简体   繁体   English

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

[英]Sending a keyboard event from java to any application (on-screen-keyboard)

I am working on developing an on-screen keyboard with java. 我正在开发一个带有java的屏幕键盘。 This keyboard has a JComponent for every possible key. 该键盘具有每个可能键的JComponent 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. 键盘本身位于一个没有装饰的JFrame ,并设置为始终在顶部。

I found that the Robot class can be used to simulate these keyboard events on the native queue. 我发现Robot类可以用来模拟本机队列上的这些键盘事件。 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 但是,在这种情况下,选择JComponent意味着在JFrame上收到按键,我将无法在其他应用程序中接收它

How can I keep my on-screen keyboard "Always-without-focus"? 如何保持我的屏幕键盘“始终无焦点”? Is it maybe possible to use another approach to send the key-press? 是否可以使用另一种方法发送按键?

当我试图用实际的键盘和鼠标命令控制一个游戏机器人时(我更像“人类”),我找到了jnativehook

The only solution I could find so far, is to make every key a JComponent (so it can not have focus), and set the following properties on the JFrame: 到目前为止我唯一能找到的解决方案是将每个键都设为JComponent(因此它不能有焦点),并在JFrame上设置以下属性:

    setUndecorated(true);
    setFocusableWindowState(false);
    setFocusable(false);
    enableInputMethods(false);

Now when using the robot class I can send events to any focused window by clicking on the keys. 现在,当使用机器人类时,我可以通过单击键将事件发送到任何聚焦窗口。 The only limitation, is that it only seems to work for windows that belong to the same virtual machine, and it doesn't work at all with any other system window. 唯一的限制是,它似乎只适用于属于同一个虚拟机的Windows,并且它根本不适用于任何其他系统窗口。

Apparently the only way to do this is to have a JNI layer that will make the conversion from java to native. 显然,唯一的方法是使用一个JNI层来进行从java到native的转换。 Java has no easy way to provide such funcionality. Java没有简单的方法来提供这样的功能。

This could be an interesting concept for a small, third party library for someone who wants to learn JNI... 对于想要学习JNI的人来说,对于一个小型的第三方图书馆来说,这可能是一个有趣的概念......

您是否尝试在使用机器人类之前调用​​本机setfocus()或setactivewindow()函数来移动焦点?

I am not aware of any way of doing this in OS independent way. 我不知道以独立于操作系统的方式执行此操作的任何方法。 I don't know about Windows, but it would be possible talk with X server over the X protocol. 我不知道Windows,但可以通过X协议与X服务器通话。

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

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