将键盘事件从 java 发送到任何应用程序(屏幕键盘)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/156912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending a keyboard event from java to any application (on-screen-keyboard)
提问by Mario Ortegón
I am working on developing an on-screen keyboard with java. This keyboard has a JComponentfor 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 JFramewith no decorations and set to always-on-top.
我正在使用 java 开发屏幕键盘。这个键盘有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 JComponentwould mean that the key-press is received on the JFrame, and I wouldn't be able to receive it in the other application
我发现可以使用 Robot 类来模拟本机队列上的这些键盘事件。但是,在这种情况下,选择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?
我怎样才能让我的屏幕键盘“始终没有焦点”?是否可以使用另一种方法来发送按键?
采纳答案by Mario Ortegón
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 层来实现从 Java 到本机的转换。Java 没有简单的方法来提供这样的功能。
This could be an interesting concept for a small, third party library for someone who wants to learn JNI...
对于想要学习 JNI 的人来说,这对于小型第三方库来说可能是一个有趣的概念......
回答by Batman0730
I found jnativehookwhen I was trying to control a gamebot with actual keyboard and mouse commands (to be more "human-like").
当我尝试使用实际的键盘和鼠标命令(更“像人”)来控制游戏机器人时,我发现了jnativehook。
回答by Mario Ortegón
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.
现在,当使用机器人类时,我可以通过单击键将事件发送到任何聚焦的窗口。唯一的限制是它似乎只适用于属于同一虚拟机的窗口,并且它根本不适用于任何其他系统窗口。
回答by iny
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 服务器通信。
回答by Tom
have you tried to call native setfocus() or setactivewindow() functions to move focus before you use robot class?
在使用机器人类之前,您是否尝试过调用本机 setfocus() 或 setactivewindow() 函数来移动焦点?

