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.
Related
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.
Right, my title probably made no sense. However I'll explain it now.
I have a program coded in C++ that I wish to allow a keycombo to be pressed to run the program. My program does not run all the time it only runs when clicked then closes when the operation is complete. It simply does it's function then closes itself. Now here is the question.
Would it be simpler to attempt to edit my C++ program to run all the time via a thread in a class that then called the other class to run that did the function then stopped and listened for keypress again.
Or
Create a Java program that runs all the time and listens for a keyEvent and upon doing so runs the other programs exe. Then bundle the exe and jar in to one exe that is installed and run so they have access to each other.
If I did it this way is it even possible as far as I know key events need to have focus to be able to be detected. Meaning that I would need to be running the program as my main window to detect it? Or is that not true.
I'd looked at the java route as my Java is better than my C++. Is this do-able and if so what approach would be the better one?
Hope this is explained well enough let me know otherwise.
i want to write a clock program, that should run at background and broadcast the current time according to the system if the keys "1" and "2" are pressed together. i already have a program itself (including audiofiles and appendings) so everything i need, is to find the way to make the program window inactive, but to do it in such way that it will activate when the keys are pressed. what can i do?
On Linux with KDE you can use khotkeys to set up a keyboard hotkey which will send e.g. a dbus message to your program to tell it to reactivate. I don't know if 1 and 2 together is an allowable hotkey - it doesn't make much sense because it will likely cause a 1 or 2 to be inputted into the program you are currently using, which may or may not do anything but it's better to use a key like ctrl, alt or the windows key to avoid that problem.
In other environments / operating systems there may be something similar to khotkeys, I don't know.
I dont think Java can help you here - you're looking at something like a TSR, which unfortunately is not a Java thing. They went the way of the dinosaurs anyway, along with MSDOS.
You've to go native for something like this on the modern operating systems.
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.
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