How can I call a Java application on background with keyboard keys? - java

I'm actually working on an screenshot saver on Windows, and i'd like to call to a method on the java application when I press a combination of keys to save the screen.
How can I call a method when the java application is not the "active" window? whether the main window is minimized or is running on background.

You're looking for a Hook in your keyboard for Windows. Note that hooking is highly relevant to the OS and your application may not be portable between different versions/editions of the OS. Still, you can do this using JNA as shown here: JNA Keyboard Hook in Windows or using a third party library like jnativehook.

Related

Capture Keystrokes when program is not active

I code in java. I wrote a keylistener for the frame and it prints all the keystrokes when the frame is active, but when i minimize it or deactivate it, the program obviously stops and no keystrokes are printed. I wanted to make a small game where i enter a key and using the robot class, it presses another set of keys but this game is in flash. any idea as to how i would capture keystrokes when window is deactivated.
edit: I only code in java so is it possible using only java or at most combining it with native machine... i use windows
By its nature, Java is sandboxed by the JVM, so you will have to incorporate some kind of native methods. There already exists a very flexible and helpful library to accomplish this under open source, called JNativeHook. It's very easy to hook in, especially if you're already familiar with Swing event handlers. Same basic concept, except it leverages native code written in C. It supports all of the basic operating systems (Windows, Mac, *Nix).

Kiosk mode for Linux Java Swing application

How can I disable OS-level keyboard shortcuts (e.g. Alt-Tab, Ctrl-Alt-Left/Right, etc.) on a [Ubuntu] Linux machine? I'm developing a full-screen Java Swing app and don't want the user to be able to task switch away from the program arbitrarily. It's not enough to toggle the "always on top" flag; users mustn't be allowed to switch workspaces, migrate focus or any other such things. The machine must function normally before and after the application is executed. Google says that this will require JNI or JNA but I'm looking for a bit more hand-holding.
There's no point in trying to do this in your application because any of these changes are going to need to be handled by X11 and/or the window manager since those are what respond to the commands. Assuming that you have control of the platform, choose a window manager which supports a kiosk mode. Then use the window manager's settings to start your application and enter kiosk mode.
Options for window managers which can do this include KDE or twm-kiosk.
(And if you don't have control of the platform, you're not likely to be able to have your application intercept things like ctrl-alt-backspace anyway.)
Edit:
In response to a scaled-down version of the question in which he's willing to let things like ctl-alt-backspace go and just wants most of the keys including alt-tab or other similar application switching key combinations, the following should work:
You should be able to do this using XLib's XGrabKeyboard method through JNI. This Java/XLib JNI keypress capture tutorial should be a good starting point. However, it uses XGrabKey which just passively listens for keys and does not prevent other applications from receiving them. You'll instead want to use XGrabKeyboard which actively snags all of the normal keyboard events (which, if the premise of this StackOverflow question is correct, includes the task switching keys).
Note that as a side-effect, key capture in Swing will also probably stop working because your Swing windows are going to be separate from the window you create in C. As such, you will probably have to use your JNI interface to get key presses to your program when needed. (Although I would definitely advise testing it first before writing the code.) You might be able to avoid this if you can get the window using Java AWT Native Interface to get the window ID. (Note that Swing is built on top of AWT, so this will work for Swing.) However, I'm not sure how to do this. It looks like you might be able to navigate the window tree by getting the root window from the Display and going from there to find your Window, but it's all kind of weird. It would be nice if the AWT NI just told you the window ID, but it doesn't look like it does that.
As this warning Reminder: XGrabKeyboard is not a security interface notes, this doesn't make it impossible for other programs to see the keys, but it seems likely that window managers will not be using XQueryKeyMap so it is likely to prevent task switching.

Help with accepting input from another focused window using Java

I want to write a program that responds when I press a certain key on the keyboard. However, there will be a completely unrelated full-screen program running in the foreground and my program won't have the window focus.
So, basically, what I want to do is start my program and have it run in the background, then start the full-screen program. And then I want to be able to get my program to do stuff in the background when I press a certain key.
Is there a way to do this in Java? If not, is there any way at all to do it?
My platform is Windows 7.
A keystroke is an O/S event. You can write a listener in C++ using hooks, then create a JNI interface on top of it.
Lookup MSDN entry for "SetWindowsHookEx" function for hooking procedure.
If you don't want to fuss with JNI, consider implementing the listener in Visual C++ or Visual Basic and cross-compiling the .NET binaries into Java bytecode. Here is a free tool to do that: http://www.ikvm.net/

Launch another instance of Java app on Mac

I have a Mac Java app bundle that has problems opening an OpenGL window from a SWT dialog in a single process. It just doesn't work.
To solve this problem I would like to open a SWT dialog in one instance of the Java app and then have it launch another instance of itself with a parameter saying "this time open the OpenGL window". The part I don't know how to do is finding out what "itself" is on a Mac.
How can I do that?
You might be able to adapt this Swing based Launcher that uses
exec() to run programs in a separate JVM. If you need to include your own dynamic libraries, you might be able to use one of the relative path symbols described in man dyld under the heading Dynamic Library Loading.

Java desktop app: How to maximize tray application when hotkey pressed?

I need to write a program that, when minimized, lives in the System Tray, and I'll use Java 6's SystemTray API to do that.
How can I make that application comes to the foreground when the user presses some hotkey?
For example, the app is running but minimized. When the user presses CTRL-SHIFT-Y or something (or, like Google Desktop's search, CTRL twice) and the application is maximized.
EDIT: I know about how to bring a Java window to the foreground. I'm asking more specifically about how to make a running Java app listen for a hotkey.
You're going to need to resort to JNI, check out an example.
Here's another nice example from Sun's forums.
You can use the following SWT extension library to create a keyboard hook that can listen for your hot key - http://feeling.sourceforge.net/
note, that this is windows only (but that may not be a problem for you).

Categories