Controlling mouse when Java window is out of focus - java

I'm interested in writing a program that will assist me in marking exam papers online. I would like to use the keyboard to control the mouse eg if I press '1' then the mouse will be sent to a specified location and click there. This will double my work output at least. The problem is marking is done through Internet Explorer so the Java program will be out of focus. From searching this site I found that someone has written a library that can receive keyboard input out of focus but I couldn't find any such thing for mice (I don't think Java Robot works out of focus).
Does anyone know whether such a program is possible in Java using standard libraries?

The problem of course is capturing key presses when Java is not in focus. You have three main options as far as I can tell:
Write your own JNA or JNI code to register your hot keys, or
Find a library that does this and call its methods, or
Use a scripting program like AutoIt (if this is Windows) that is linked to your Java program, such as with sockets linking the standard inputs and outputs of both programs.
I have used the 3rd option successfully, but in fact for me, it was usually easier just to do everything in AutoIt.
Note that this statement is not true:
(I don't think Java Robot works out of focus).
The Java Robot doesn't require that a GUI has focus, and in fact does not require that a GUI be running at all.

Related

interaction with windows in java

I currently want to build a simple application using java or python.
I started coding not long ago, I do not know where to start from.
these are things I would like to know :
basically I want to build a background application(or program) that detects current keyboard layout(as in, which language), and just displays the next language on the screen everytime I push shift + alt so that I can know to which language I am switching in windows 10.(because task bar is now set to automatically be hidden when mouse cursor is not around it. and I speak russian, korean english and spanish, I need to know which language I am currently using in keyboard, but with the hidden task bar, it is inconvenient)
considering 1, how do I make java detect the language within windows system?
I think I need to learn how to study programs or system in order to build anything, I do not require direct answer to this question.
I want to understand the design process in java(or any language), and this is a part of study pertaining to this.
so in lieu of direct answer, I would like to read sources or books.
thanks for reading!

How to automate the manipulation of a Java GUI program by control hierarchy (from C#)?

I'm new to the world of C# automation so apologies if this is obvious. My colleagues and I work frequently within an app written in Java, and I'm looking to automate some of the tasks. However, I can't seem to identify any of the elements (various menus, textboxes, etc.) within the Java windows using the tools I'm aware of (Inspect and Spy++). Nothing within the main "window" of the app shows up in Inspect (just the title bar and its' children).
I've gotten the automation working by using P/Invoke SendInput commands to click on the various parts of the window I need to click on (based on x/y coordinates) and enter text as if it's coming from the keyboard. However, this seems a bit ... fiddly. I'd feel better using this if I could formalize element names instead of just sending mouseclick instructions via code, is there a way to do this? For example with something like the Windows.Automation library when I don't have the element IDs?
TestStack.White framework written in C# should help you. It's based on UI Automation API and should see any lightweight controls like WPF ones.

Java Detecting when a key is pressed

I am trying to make a program that will run infinitely until I press a button. This program will run in the background so there is no display open at all times.
while(!certainButtonIsPressed)
{
//Do Something
}
How do I make it so that while certainButtonIsPressed is valid while not making a KeyBoardListener class? Is this possible without a Key listener of some sort?
Thanks!
You ask:
I am trying to make a program that will run infinitely until I press a button. This program will run in the background so there is no display open at all times. while(!certainButtonIsPressed) { //Do Something } How do I make it so that while certainButtonIsPressed is valid while not making a KeyBoardListener class? Is this possible without a Key listener of some sort?
So basically what you're trying to do is to trap all the keypresses of platform from a background process, and this is something that core Java cannot do without use of platform-specific native code that you provide, either through some library that you've obtained, or by meshing your Java program with some key-trapping utility. So if your question is, can this be done via just Core Java? And the answer is: no.
If you are looking for non-Java platform specific solutions, then you will need to give further details. My recommendation though is not to use Java for a task that can be performed much more easily and fully with another language, perhaps a scripting language such as AutoIt, if this were for a Windows environment.

Java: how to disable mouse and keyboard system wide

Alright, I am not sure if this is even possible with Java specifically, but I am working on a a small program very similar to synergy and I need to be able to completely disable input from the mouse and keyboard on the host computer, but still record the input within the program. I can not think of any clean and robust ways to do this with Java. Is this possible?
Any way you will have to use JNI for such a purpose.Have a look at this blog ,it will give you some idea.

How to handle a GUI of an external software from within Java code

I need to run an external application from within my Java code. I can run the application with Runtime r = Runtime.getRuntime() and then r.exec(...), however, this brings up the GUI of the application. And I still need enter some settings in some fields and press enter. So:
Is there some way to handle a GUI (filling out fields, pressing "return"..etc) from within Java code?
Thanks in advance for any answers,
Anas
Use the AWT Robot class:
"This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed."
Thanks RichieHindle and Vanya for your comments. AWT Robot class does work with an external softwatre (in this instance, I only need to press an enter. It did that, no problem). But further handling seems quite difficult, since every key stroke (entering a username) needs a java line (unless there is someshort cut that I missed). I will try to automata the process more, or find some work around.
Thank you, this was informative.
Anas

Categories