Creating a window always focused for Windows - java

I am using Java to create an foreground application that uses the Robot class to send mouse clicks to other applications real-time.
Here's my issue at the moment: my application needs to read from a device in real time and keep sending Robot commands. However, mouse clicks cause the focus to be shifted to other applications. Therefore, I cannot continue reading or sending commands.
I've tried jDialog's and jFrame's setAutoRequestFocus(false) and true but I couldn't keep the window's focus.
Can anyone help me with this?

Related

Way to capture events from two seperate Java Swing Application windows

I have the following simple question. Is it possible for someone to have two Java Swing application windows from which events could be captured?
I have the following scenario. I have an app which runs on a touch enabled device running Windows 7. Said app spawns another child JFrame. Now, if I click on the parent frame, I get the window's focus and I can perform actions. Doing the same on the child frame also does the same getting the focus from the parent.
What I want to do, is to be able to handle click events on both screens - that is don't block the other frame when someone is interacting with the other one.
Is there a way to do something like that?

Windows 7 Touch Screen + Java Swing = Delayed Mouse Events

I am developing a Swing application in Java. The program is to be run on dedicated Windows 7 Touch Screen machines, and as far as I know, this program should be the only thing running on them under normal operation.
I've noticed that Windows 7's touch screen interface has this thing where it will hold back mousedown events. Due to the "tap-and-hold right click" gesture, Windows 7 won't actually send the application a mouse down event until:
the user "touch ups" (raise their finger from a touch, at which point both down and up are sent)
moves their finger (at which point the click becomes a drag)
or the right click circle gesture times out (after about 5-6 seconds)
Trivially, this means that buttons don't look depressed on the application until the mouse down event is sent (and never draw depressed at all with a quick tap). But, recently, more seriously, this means functionality that requires a press-and-hold cannot happen without major amounts of confusion to the average user.
At this point in development it is far too late to turn around and start from scratch with one of the many touch libraries offered on other questions. (They require redoing the entire application in their component hierarchy, and that simply isn't feasible. Plus things coded to use mouse events would then have to be recoded using touch events, and we don't really need multi-touch...)
Yes, I have attempted to simply turn off the touch-and-hold right click gesture. Unfortunately, doing so simply disables the drawing of a circle indicating a right click gesture around your finger and the fire of the subsequent right click. It does NOT remove the underlying problem: the delayed mouse down event for up to six seconds.
My Question: Is there a way to:
Ideally, indicate to Windows that my application is a touch-ready application, so it sends down events immediately (but, I suppose, still send mouse events instead of touch events)? I can see that the built-in soft keyboard works correctly, it seems. I just want my Java application to do the same.
Disable the delay entirely on the computer, but still provide basic touch functionality?
Potentially something as simple as this question, which seems to be asking about the same phenomenon, but in Java?
Typically this type of behavior is controlled by the touch device driver installed on the device. All of the touch manufacturers I have seen have control panel apps that allow you to customize touch behavior. So for instance you can tell the driver to send a mouse click immediately up receipt of a touch rather than delaying briefly in order to prevent an incidental touch from initiating an input event.

How to make a JFrame visible only if the control button is pressed twice

I am creating an desktop application that runs at background and while clicking the control button it should be visible i have made the setVisible(false) to the JFrame any idea how to do it . The application should triggered if you click the control button twice in desktop or in any application this should work.
This can't be done in your application.
If it runs in the background, it doesn't have the focus, so it can't react on that event. Any other application might have the focus and being interested in consuming the Ctrl button.
Think of multiple such programs, all in concurrence for that button. Which should get informed? Think of 3 Editors, all getting the next character typed.
Your OS or DE might have a way to define a hotkey, look whether your program is running (once?), and send a message to your application. The application might then react.

java.awt.Robot: how to send mouse/keyboard events to a specific window? with cross-platform support?

So from this question In Java Swing how do you get a Win32 window handle (hwnd) reference to a window? it appears that I can get the window32 handle .
would it be possible for java.awt.Robot to send mouse/keyboard events to that window handle?
sometimes when I am sending keys via Robot, if the window gets minimized, it will start typing into other background irrelevant windows that are open. I want to prevent this by allowing Robot to send keys and mouse events to that specific window of interest.
Would it be possible to achieve the same deal in Mac and Linux as well? be able to send Robot events to those respective specific window handles?
This is a classic problem with Robot. As they have quoted in the other thread, its not possible with pure AWT/Swing. You have to get into sun's internal API or use native code. There is not getting around that problem.
It is exactly because of the problem that you have i.e. make it work across OS's is why Java has not exposed such a control.
It would be useful to know what you are using this for.

give an application focus in java

I want to partially automate some integration level tests via the use of the Robot class to send mouse/keyboard events to the the Java application I'm testing. I want the user to be able to click a button and have a number of keyboard events be automatically sent at once.
I spawn the application I want to test from inside my Robot test (using Runtime.exec) and then generate the appropriate events. Unfortunately when the user clicks a button the button has focus and receives the events instead of the events going to the child process as I would like. I would like a way to ensure that the Robot keyboard events are sent to the application I'm trying to test instead.
I've thought of fetching the child process PID and then using the FG command to bring the application I want to the foreground; but this is a Linux specific approach. I would prefer a method that works for Linux or Windows equally as well.
I don't know exactly what you are testing or how you are testing it but you should look into Sikuli - its a visual technology and there are methods that will let you either automatically click on a part of the screen you want to get focus, or use App.focus("application name"). I really recommend it. However using it will not let you use the computer to do other things while testing the program.
Hope that helps!

Categories