Capture key stroke from separate process in Java - java

I want this to happen:
Start my java program
My java program launches notepad.exe
If I type a letter in notepad my java program should react and print out the letter
In short: Is it possible to make a key event listener for another process? The java program won't have focus.
If it's impossible I want to be able to change focus between my Java swing window and the notepad process. For example:
A) My java program has focus (small swing window), I type the letter "A"
B) Notepad is given focus quickly and the letter A is typed there (for example using the Robot class)
C) My java program gets focus again
Quoting the answer given in Creating a keyboard hook for a Java program that sits in the System Tray
so , creating a keyboard hook isn't as easy as it looks , java doesn't provide a direct way to interact with the system events for purposes of security ; as it might lead to serious problems concerning the JVM , system's memory and the portability of Java Framework..
you have 4 ways to make global keyboard hooks :
Learn JNI / JNA, and I prefer JNA since its much easier than JNI , in both cases you shall deal with .dll files.
Use JIntellitype , which - as you said - issues some problems.
the elegant solution by Sergei Biletnikov here http://biletnikov-dev.blogspot.com/2009/09/global-hotkeys-for-java-applications_25.html
ignore Java , and try Autoit V3 ( I'm not 100% sure about it , but I think you could send signals/events from Autoit to your Java app , so Autoit would just work as a bridge that catches the global key strokes)
Going with number 3, which is a good tutorial. If the link has 404'd maybe the project source is still up: gigapeta.com/dl/1917618aba749a

With your presented solution #1:
Learn JNI / JNA, and I prefer JNA since its much easier than JNI , in both cases you shall deal with .dll files.
You should take a look at this stackoverflow thread:
JNA Keyboard Hook in Windows
There is copy/pastable code that demonstrates a JNA key hook. You will need to download the JNA dependencies from http://jna.java.net/ for the example to work. You won't even need to fiddle with silly DLL's.
Additionally, you might want to give this library a try. While I have no experience with it, it popped up on my google search for "java keyboard hook." It seems to accomplish the goal of intercepting the keystrokes:
http://code.google.com/p/jnativehook/
Note that you would need some additional native code to see what the current "active window" is. If the active window matches "Notepad.exe" then you should record a native key event.
Good luck!

Related

Java block keyboard presses

Is there a way how to block keyboard input in java. I would like to catch the input in the java code, but stop it from being send to OS.
Example: i have notepad opened and i can write just fine, but when i press a combination of keys java app catches that input, and now i should not be able to write with my keyboard. Is this kind of behaviour possible?
I know how to capture key presses but the keyboard blocking part is a mystery to me.
I tried googling it but i did not find any solution.
You have to understand: java applications run with the JVM. A JVM isn't the operating system.
Therefore your ways to access resources belonging to the operating system are very limited.
In other words: there is no generic, cross plattform way of having a Java application being able to "intersect" arbitrary console user input for arbitrary other applications.
Imagine you are a person sitting in a bus - just a guy like everybody else. You have no authority to turn to fellow passengers and ask them for their passport or such things. You are just one guy in the bus, like everybody else. Same here: a Java application is lacking the means to control other processes.
As you are specifically asking about the Windows platform: there might be some options using JNI and specific native calls. See here for example.
So, to be precise: it is not possible in general, but depending on your operating system there might be ways, for example using JNI.
Yes, you can do this - I used this library:
https://github.com/tulskiy/jkeymaster
to successfully to register a "global" keyboard shortcut to open a window in my program that was running in the task tray.
You can't "stop" it from being sent to the OS, but you can register a keyboard shortcut, open your window, and give it focus, so that all other keystrokes go into that text box.

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 input when window is not active

Is there a way to detect key input when the window is not active? That is, another application is running but the program is triggered when say the F9 key is pressed or something along those lines.
Is that possible or is java not compatible for such functions? From what I found java can't get input unless it's the active window.
Note:
I typically use keylistener, which seems to stop working (with good reason) when I am not actively using the program.
It sounds like you want low level keyboard hooks.
Get global keyboard input with Java
It's not directly possible in Java (i.e. with pure Java code), but you could reference other libraries (or make your own) to acheieve this. A quick google search gives a lot of references and free/open libraries that can give you keyboard and mouse hooks (both very handy).
Hope that helps.

Get global keyboard input with Java

How can I get what is being inputed in any program outside my java program.. Is there a way ?
For example, I'm running my java program then I start typing in notepad, and I can see with my java program what is being typed.
The term you should looking for is Keyboard hooks, here is example using WinApi http://www.codeproject.com/KB/DLL/keyboardhook.aspx
You cannot do this in pure Java. At some point, it will involve talking to an external application or a native library, and will be platform specific in either case.
There is a library to handle native keyboard and mouse control:
http://code.google.com/p/jnativehook/

Categories

Resources