java system-wide keyboard and mouse state - java

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/

Related

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.

mouse event, not on Jframe

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

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.

Global keylogger in Java

I'm writing an application that monitors the person’s mouse and keyboard. If they have not used the keyboard or mouse for 1 minute, it will pop up a message that says “You have not used the mouse or keyboard for 1 minute” along with an OK button.
How do I do this in Java?
You need a bit of C/C++ code and call SetWindowsHookEx This function allows you to hook into Windows events and receive a copy.
This question contains code to get you started: JNA Keyboard Hook in Windows
If you want to do this for only your application, then its very simple. You can simply add listerns i.e Toolkit.getDefaultToolkit.addAwtEventListener(..).
But for the system as a whole, I am afraid, it cannot be done in java, you may use JNI though.
If you only want to monitor activity in Java application windows, it's trivial – all you have to do is register to the appropriate event.
But to monitor all mouse and keyboard activity in the OS you will have to hook to the API which is platform dependent and will require using the JNI

Copying selected text to a Swing Java app?

My goal is assign a global hotkey (JIntellitype, JXGrabKey) that would pass an arbitrary selected text to a java app.
The initial plan is to utilize the java.awt.Robot to emulate Ctrl-C keypress and then get the value from clipboard.
Probably there's a more elegant solution?
EXAMPLE: Open Notepad, type in some text, select that text. Now, that text needs to be copied into a Java app.
I guess you want to implement a global input monitor, Java is not so straightforward to do the job. You may have to write an API hook and pack it in a DLL, then invoke it via JNI.
The Robot only sends events inside your JVM. I don't know of anything to send events out to the operating system. Though there is plenty of examples out there of making JNI calls to the windows API, it would then be platform specific.
I've gone with with Robot and that works just fine.

Categories

Resources