views:

3419

answers:

8

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.

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?

A: 

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

Tom
A: 

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.

Mario Ortegón
A: 

There is a reason every OS out there has this natively. Why would you want to make a on screen keyboard in Java?

Jorn
Because it has to look a very specific, certain way in linux and windows, and I don't want to code this twice. Also, the application that is going to use this keyboard is in java. Having it working for other applications is just an extra bonus.
Mario Ortegón
A: 

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.

iny
+1  A: 

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...

Mario Ortegón
A: 

a reason to do this rather than use the OS's own pop up keyboard is if you are running the program in Keiosk mode.. so the "user" doesnt have access to the actual in build keyboard... is there a method to call the OS's in build pop up keyboard?

A: 

i have many buttons and want move between there by kyboard. i must do?

A: 

i would like to thesame thing send keyboard or keys to other applications outside mine. and control other applications etc. where do i find the robot class to see if it can do what i want.

tolu