mouse event, not on Jframe - java

i want to make my program to work after a mouse button was clicked,
but not necessarily on a Jframe of some sort.
for example, i want my program to start working after the PLAY button was clicked
on windows media player.
is there a way of doing that?

Yes, but not in standard Java. You want to listen for an event in the OS not in your Java GUI which Java can't do by itself. This can work, but in order for Java to interact closely with the operating system you will need to use the Java Native Interface (JNI) and C/C++ code or Java Native Access (JNA) (which is a bit easier to use in my opinion). I've also done similar stuff in Windows using a Windows scripting language such as AutoIt, and then connect this to the Java application either via sockets or via standard input and output.
Java Native Interface
Java Native Access
AutoIt Windows Scripting Language

Related

how to link my app to any text in windows?

I've just learned Java SE basics and want to make a utility program that popups a small window when selecting a text and when click the small window the selected text is converted to another equivalent characters in other language.
I wrote a class that should take any selected text from windows and convert it to the targeted language, how can I configure my app to windows backend to allow my program take the text as parameter?
You can't -- not with core Java, since the key functionality that you're looking for, for the program to be able to listen to user interaction with other programs and the OS, is something that Java was not built to do. One of Java's prime directives from the very beginning was to be able to run on multiple platforms, and in order to achieve this, the creators made the language and its tools as OS agnostic as possible, and so tasks that require a close integration with the OS don't work well with Java.
I'd suggest using JNA or JNI which would allow your Java program to make OS calls including mouse and keyboard hooks, or integrating another tool such as an OS scripting tool into your Java program via processes/streams.

Controlling applications through Java

I am looking for a way to mimic operating-system (Windows in specific) actions through Java. Preferably, the program should run in the background, but it is not a big deal if it does not. I got the background part covered thanks to this question. I was looking for the following specific features :
Maximizing/Minimizing the currently active window. (Can be any window, not just the Java application window.)
Closing the currently active window.
Open installed programs, and system utilities like the calculator, paint, etc. (I figured out this one from this question.)
Shutdown/Restart (This one's done too, thanks to the question here.)
So, my actual question is:
Is it possible to minimize/maximize or close an application window from a java program? (in Windows)
Example Scenario:
Firstly the java program is started, and it runs either as a background process or as a window. Bottom-line is that it should be able to accept triggers like maybe a keyboard shortcut or microphone input to trigger the action. After that suppose a Chrome window is opened and is currently active. Now on pressing the pre-defined shortcut, the Chrome window will minimize/maximize or close.
If the answer to the question is yes, I could use some pointers to start with my application. Thanks!
What you need is like an OS shell programming interface.
In Java side you will define a few interfaces.
Another Java layer will detect which OS is used and will return an implementation of interface: Windows, Linux, Macosx.
Some functionality you can have with simple bash command: in windows cmd, in linux .. to many. Eg shut down, launch MSPaint, Calculator.
Other functionality you can have it with windows API: you will need to write some JNI functions and call it. eg minimize, maximize. It is possible.
Edit:
I see there is no accepted answer, although it is answered properly.
Here is a C# code which does what you need in Java.
Now you need to migrate this code to Java:
In your java class declare a function:
private native maximizeOrMinimizeWindowWithName(String windowName, boolean maximize);
Compile -it
use Javah.exe - it will generate the necesary .h files
Use a C editor, configure environment, use the generated .h file.
-include windows api headers
-load user32.dll
- do more stuf..
compile your C code to .dll
put the your.dll into your app PATH environment variable. ( windows has the . in path, linux not)
-text, bugfix,
for more info you should see a basic JNI tutorials.
-upvote accept :)
This can be initiated from Java, but not actually implemented in Java. In other words, it will take a lot of platform-specfiic JNI library code to get it working.
Java will give you almost no benefit for your use case; you should avoid it altogether for this project.
You should look into Autohotkey. It's an system dedicated to simulate user programmaticly.
Using AH scripts you can easily access all open windows, installed programs and even control mouse and keyboard.

java system-wide keyboard and mouse state

Is there a way to listen to the mouse and keyboard events system-wide without taking these out of system queue?
E.g. is there a way to set a demon, let's say, which would listen and report each and every keyboard and mouse event?
It's not possible using pure Java. But you can use JNI (Java Native Interface), which is working on code written in C++ and natively compiled. So, this way you can write the Global KeyListener in C++ and let make Java use of it.
There is an active project implementing this for Linux, OS X and Windows: http://code.google.com/p/jnativehook/

Global hooks JAVA LINUX (Ubuntu)

I want to hook into another GUI app (probably using Qt).
I want to know, when some button is ready to click and etc.
My app technology is JAVA, my OS is Ubuntu.
Michal.
I can't help you with the button thing, but the most commom way of interacting with other desktop applications if there is no public API is using the Robot class. With a Robot you have control over the cursor (point&click) as well as keyboard input.
Javadoc java.awt.Robot
I don't know which application you want to hook into, but some expose data and methods via DBUS. It would be less costly.
I seriously doubt you can do this. Especially from java. Unless the app you're trying to control has some public API, or can be controlled with dbus.
This sounds like a job for some native hack via either X11 apis or something from Qt.

How do you record keystrokes when operating on another window in Java?

I'm looking to record keystrokes and mouse positions while I'm in a browser window on various websites, but I'm not sure how to do this outside the SWING (or some other GUI) window.
How can I record keystrokes when a different application's window is the one active?
The support you need is not exposed by Java. It is a fairly simple matter to do this if you have access to the RECORD extension in X, or Win32 on Windows.
Here is a Python project that implements support for both of these platforms: pykeylogger.
(Windows only) In theory you could use JNA or JNI to bind to GetAsyncKeyState and you'd be able to monitor key presses.
I don't think you can do this from pure Java. If it is possible at all, it would require the assistance of a (highly) platform specific native code library.

Categories

Resources