Sending keyboard events from java to any application (on-screen keyboard)

I am working on creating an on-screen keyboard with java. This keyboard has a JComponent for every possible key. When a down mouse is detected on the button, I want to send a specific keyboard code to the application that is currently in focus. The keyboard itself is located within the JFrame without decoration and is always installed on top.

I found that the Robot class can be used to simulate these keyboard events in its own queue. However, in this case, choosing JComponent means that the keystroke is accepted on the JFrame , and I could not get it in another application

How can I keep the on-screen keyboard “Always out of focus”? Is it possible to use a different approach to send keystrokes?

+4
source share
5 answers

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

This might be an interesting concept for a small, third-party library for those who want to learn JNI ...

+2
source

I discovered jnativehook when I tried to control the game drive using the keyboard and mouse commands themselves (to be more human,).

+3
source

The only solution that I have been able to find so far is to make each JComponent key (so it cannot have focus) and set the following properties in the JFrame:

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

Now, when I use the robot class, I can send events to any focused window by pressing the keys. The only limitation is that it only works for windows that belong to the same virtual machine, and it does not work with any other system window at all.

+1
source

Have you tried calling your own setfocus () or setactivewindow () functions to move the focus before using the robot class?

0
source

I do not know how to do this regardless of the OS. I do not know about Windows, but one could talk to the X server over the X protocol.

0
source

Source: https://habr.com/ru/post/1277348/


All Articles