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

后端 未结 5 1010
一向
一向 2021-01-03 06:58

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

相关标签:
5条回答
  • 2021-01-03 07:16

    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.

    0 讨论(0)
  • 2021-01-03 07:24

    have you tried to call native setfocus() or setactivewindow() functions to move focus before you use robot class?

    0 讨论(0)
  • 2021-01-03 07:29

    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:

        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.

    0 讨论(0)
  • 2021-01-03 07:34

    I found jnativehook when I was trying to control a gamebot with actual keyboard and mouse commands (to be more "human-like").

    0 讨论(0)
  • 2021-01-03 07:38

    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.

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

    0 讨论(0)
提交回复
热议问题