Can Java see activity of my keyboard? - java

I would like to write a program which can monitor activity of my keyboard. In more details, the Java program should "see" which key is pressed/released and when. All this information should be stored in a given file.
First of all I would like to know if it's possible to do it with Java. I know that it's possible if I type in a text field generated by Java program. But is it possible for Java to monitor the keyboard activity if I type, let say, in a text field of a browser or, for example, in word (or open office) document?

These events are directed to the window which has the focus, from all events on the desktop you can only get the mouse position.
import java.awt.MouseInfo;
public class Mouse {
public static void main(String[] args) {
while ( true ) {
System.out.println( MouseInfo.getPointerInfo().getLocation() );
}
}
}
For capturing sysem wide (what you need for word) you need to include a native lib
Code example for windows: Native keyboard mouse hook

Forget JNI and JNA.
There is now a new library for native mouse&keyboard access that works exactly the same as MouseXxxListener and KeyListener. In other words, it's a familiar interface so you don't have to learn anything new (except for how you link an external library).
https://code.google.com/p/jnativehook/

Forget JNI
JNA is the solution (is based on JNI but it's way easier although most people don't use it thinking it's as difficult as JNI, but it's not)
Check https://github.com/twall/jna/
and specifically in the middle of the page where it says: Platform / Platform Documentation
If you download the platform.jar (and check the sourcecode) you'll find a wrapper around
User32.dll where you can find the win32 function
GetAsyncKeyState(int vKey)
mapped to a corresponding java function
(see h**ps://jna.dev.java.net/javadoc/platform/com/sun/jna/platform/win32/User32.html)
There u can check the state of a key (pressed or released)
If you need a global key listener have a look at my other reply to:
h**p://stackoverflow.com/questions/696170/java-keylistener-without-window-being-open/4394398
Thanks to Stackoverflow i can't post more than one working link in my replies :(
Cheers

It's likely possible to write a Java-based key logger using some native libraries, although be aware that such a program is likely more noticeable than one with a different technology, since the Java VM will need to be running for it to work. Keep that in mind if you're trying to be clandestine.
Also, if you just need such a program for use, and don't have to develop it yourself, there are many hardware and software keylogging systems already out there that you could use instead.

I think that native functions can do it.
It's something like you can connect c++ with Java.
http://en.wikipedia.org/wiki/Java_Native_Interface

What you are requesting is not possible using the Java API. In order to do system-wide key logging you need to register with win32 (or other OS native) hooks. Specifically, this will be done using native libraries and interfacing with the JNI.
There are some code snippets over at http://forums.sun.com/thread.jspa?messageID=3808163#3808163. It's a good example of how to get started with JNI and registering a Java callback to a win32 hook.

Related

It is possible to control a java application from a delphi written application?

My problem is pretty simple i have an application written in java and i want to send commands to it ex ( click a button , send some key strokes , click a menu item ) from my application witch i will write in delphi. Is this concept even possible ?
I actually had to do this at the last place I worked, you can get around it with complex window events etc... as mentioned above but if you have access to the Java source simply write other access methods either that call a specific runtime that closes (i.e. trigger a public static void main(String[] args); via a native call or via the command line.
OR
Implement simple a simple message system between Java/Delphi over TCP/IP and send either XML or some simple string mappings (I think it took about an hour to set up Maps that could pass back and forth).
In my case we were simply handling reporting and talking to the database so it was pretty easy to work around without getting into a native call. Alternatively, there is(was) a port of the JNI for Delphi that worked pretty well with Delphi 7. I have no clue what runtime you're using but it might be an option.
Honestly, the TCP/IP method is probably the easiest. It doesn't take a lot to implement, it doesn't eat a lot of resources and it allows you to execute "myMenuItem.onClick()" pretty easily as a packet, you just have to expose the methods.
http://home.pacifier.com/~mmead/jni/delphi/
Well It depends on which Java GUI technology is used . If SWT or AWT is used , you can get handle of UI components, because these two toolkit uses native libararies.. On the other hand, if that java application GUI is created by beans of SWING, you can not get any handle. Because, swing toolkit is implemented by pure Java..
If the Java app can be modified, the Java Robot API (included in JRE 1.3 and newer) might be helpful. This would allow to control a Swing application which does not provide windows handles as Gursel wrote. Obviously there would be some IPC required, which could be implemented using sockets for example.
The short: YES, but depending on the Java application, it might be difficult and unreliable.
I'm not a Java guy so I don't know if this is the norm, but the one Java application I had to automate displayed a single dialog that only used 1 (one) window handle! It was made up of several edit boxes, buttons, what looked like combo-boxes, but those were not true Windows controls but widgets re-created by whatever GUI toolkit the original developer used. I wasn't able to use normal Windows messages to manipulate those because, as far as Windows was concerned, it was a single window.
Happily the only thing I had to do was click a single button. I used mouse_event to move the mouse over the expected area for the button and then again to click the button. It works, but manipulating input this way is both unreliable and fragile.
To clarify Daniel ChapmanĀ  and mjnĀ  comments, find below a code extract showing Delphi controlling a Java Swing UI component (TextField) contained in a Jframe based on NetBeans ClientEditor sample.
Please notice, that this example does not use the Java source code or use TCP, XML, Windowing events handling technics or IPC, it's just simply Delphi code calling some Java code.
procedure TForm1.Button1Click(Sender: TObject);
begin
FJFrame := Tjavax_swing_JFrame.Create('Client Editor');
FClientEditor := Tclienteditor_ClientEditor.Create;
FJFrame.GetContentPane().Add(FClientEditor);
FJFrame.Pack;
FJFrame.SetVisible(True);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// Delphi setting a value in a Java Swing UI component
FclientEditor.FirstNameTextField.SetText('Delphi 1stName');
end;
The long type names are just as a matter of clarity for this example and can be shorter of course.Additionally there is no problem with JNI in this example.
Have you had a look at Java for Delphi?
It let's you call Java from Delphi exposing the Java types as Delphi types.

Check for keystroke

Im making an application that should see if your online or away.
So i need to somehow see when the user hit a key on the keyboard last time.
The application is running in the background and you can only access it from the trey.
Is this possible and if it is how would i check.
Best regards
I believe that what you're looking for can be done via a global keyboard hook, but that doesn't have direct support in the JVM. From Googling, it would appear that your only options are to write a C++ shim which you can use via JNI, or go via libffi with JRuby.
From what I've read here it is not possible to add KeyListener to the SystemTray. The only listener that is supported, is the PropertyChangedListener.
Furthermore from that question it seems to be possible on Windows but not on Linux and MacOS.
This is possible, but not using Java. You'll have to use JNI to access OS APIs that provide this information (and implent it differently for each OS).

Making a Windows Taskbar Jump-List in Java

I know the following things, and was wondering if they can be combined to make Java use jump-lists in Windows:
Windows displays Jump-Lists for supporting programs when a taskbar icon is right-clicked
C++, C#, F#, and VB support this natively (as shown here)
Java can import native capabilities using the JNA (as shown here)
Anybody have experience they can lend to help me create a jump-list for a Java app?
The J7Goodies library won't work, as it no longer exists.
The word "natively" is overstating the case a bit. WPF provides jump list support. That's not the same as C# providing it. (For Windows Forms people there's the Code Pack which is a set of managed wrappers.) And MFC provides jump list support which is also not the same as C++ providing it. Anyway, there are two things going on here. One is adding files you opened recently to that jumplist, which under some circumstances you can get for free. The other is adding arbitrary files (typically starting point templates etc) to the jumplist.
To add a file to the recent/frequent list, you call SHAddToRecentDocs, though you may not have to if, for example, you use the Common File Dialog control to open files, and/or the user double-clicks files to launch your app and open them (you have the file type registered.) Lots of folks suggest calling it anyway to be on the safe side. To add any old thing to the jumplist see http://msdn.microsoft.com/en-us/library/dd378402(v=VS.85).aspx.
How to call those from Java, I forget, but I hope they get you started.
There is a Java library providing the new Windows 7 features for Java. It's called J7Goodies by Strix Code. You can create your own jump lists with it.

Intercept / Disable System Keys in Java

Is there a way to intercept the system keys in Java so that the events are not propagated to the operating system? Ctrl+Alt+Del or other security related combinations do not matter, the main problem is for example the Windows key.
The program in question is a for a full screen application that performs some remote operations via a proprietary protocol. Currently my only idea would be to solve this via JNI, whereas the solution for Windows seems to be simple, I'm not sure about Linux and MAC OS X.
I'd prefer a somewhat standard solution, maybe there is something for Java games or so.
Java processes the key strokes after the operating system (OS), so Java can't "intercept" them. Although, you could code OS specific stuff in C/C++ that intercepted the keystrokes and call it in Java using JNI.
This appears to be fixed in Java 5, so you could have a shot at it. Apparently, the KeyEvent class in the Java API exposes two Microsoft Windows keyboard specific events - VK_WINDOWS (for the left and right winkeys) and VK_CONTEXT_MENU (for the context menu key).
It is quite possible to trap these events by implementing a KeyListener, but be forewarned that if you attempt to capture the Winkey event alone, you're bound to trip the event handler of the OS first, before Java can process it.

Listening to print job event invoked using File menu from Java

I'm interested in knowing how can my Java program listen to the Print event generated by the underlying OS. As my project is based on Java, the Print job event listener should not be platform depended.
To be more specific my program will be running on the background and should be notified of the print job if a user is trying to print pages from MS Word (for an example). The event should notify not only the print job being started but also other details such as No. of pages, the document location etc (if that's possible). A link to complete tutorial or some snippets would be much appreciated. Thanks!
We are a C++ and Java shop. I have written code that does pretty much what you are asking for. It was written in C++ for Win32 (and was a right bugger to get correct - this is one of the least well documented areas of the Win32 API - and different printers send different event streams, so it can be really tough to develop robust print queue handling).
If someone has created a JNI library for doing this stuff (unlikely), there is no way it will be cross-platform. The effort involved would be enormous (different OSes handle print queues and notifications in completely different ways).
My recommendation would be to brush up on your Win32 programming (with C/C++). Once you have things working there, if you absolutely must have them interact with your Java app, you can use JNI to wrap it.
Here's MSDN articles on printer change notification monitoring (you'll have to call OpenPrinter first - but the next call is FindFirstPrinterChangeNotification): http://msdn.microsoft.com/en-us/library/dd162722(VS.85).aspx
If you are trying to support *nix and mac as well, you'll need to dig for those separately. Good luck.

Categories