Listening for Input Without Focus in Java

Listening for input without focus in Java

This is not a trivial problem and Java doesn't give you a way to do it elegantly. You can use a solution like banjollity suggested but even that won't work all the time if your errant mouse clicks open another fullsized window currently open in your taskbar for example.

The fact is, Java by default gives developers very little control over the OS. This is due to 2 main reasons: security (as citied by java documentation) and the fact that different operating systems handle events completely differently and making one unified model to represent all of these would probably not make a whole lot of sense.

So to answer your question, I imagine what you want is some kind of behaviour for your program where it listens for keypresses globally, not just in your application. Something like this will require that you access the functionality offered by your OS of choice, and to access it in Java you are going to need to do it through a Java Native Interface (JNI) layer.

So what you want to do is:

  1. Implement a program in C that will listen for global keypresses on your OS, if this OS is Windows than look for documentation on windows hooks which is well docuemented by Microsoft and MSDN on the web and other places. If your OS is Linux or Mac OS X then you will need to listen for global keypresses using the X11 development libraries. This can be done on an ubunutu linux distro according to a Howto that I wrote at http://ubuntuforums.org/showthread.php?t=864566

  2. Hook up your C code to your Java code through JNI. This step is actually the easier step. Follow the procedure that I use in my tutorial at http://ubuntuforums.org/showthread.php?t=864566 under both windows and linux as the procedure for hooking up your C code to your Java code will be identical on both OSes.

The important thing to remember is that its much easier to get your JNI code working if you first code and debug your C/C++ code and make sure that it is working. Then integrating it with Java is easy.

Event listener in Java without app having focus? (Global keypress detection)

It's my understanding that this can't be done. At a fundamental level, the JVM only generates events for O/S events it receives, and it only receives O/S input events when it has focus.

I am sure you could use JNI to trigger the O/S to generate events for all input, but that would be very O/S dependent.

Detect Key Press on Windows Without Window Focus

If you want to detect 'key press' events occurred in other processes, you should implement Global Hook. You can define a callback function for keyboard input events using SetWindowsHookEx().

Note that the callback function must be in a DLL in order to make it Global Hook.

So your myprogram.exe should link a dll implementing the hook. Then myprogram.exe would be able to detect any keyboard events on Windows.

Following is a good example with an explanation.
http://www.codeproject.com/Articles/1264/KeyBoard-Hooks

Register Key Event when not focusing on JavaFX application window?

No there is nothing within JavaFX to my knowledge that allows for an unfocused window to be targeted with keypresses.

However, you don't need JavaFX to do this, just third party libraries. I personally can verify that the JNativeHook library works well for this purpose.

Read this thread to get the gist of what you are trying to accomplish.

Java easiest keylistener

When you setup the JFrame, add a KeyListener like this:

JFrame jf = new JFrame("title");
jf.addKeyListener(new MyKeyListener());
jf.setVisible(false);

(The jf.setVisible(false); stops the program window from appearing (only command line)

Then create a new class called MyKeyListener that extends KeyAdapter.

class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode()== KeyEvent.VK_Q)
System.out.println("Key Q pressed!");
}
}

Now let me explain things a bit.

First, when you create a JFrame, it has no default KeyListener attached. Therefore, we have to create a class MyKeyListener to do that.

Secondly, we extended KeyAdapter instead of implementing KeyListener because there are a lot more methods than what you need in there. You only need to override the keypressed() method when you extends KeyAdapter but you have to implement all (I think it's 3) the other methods that you don't need for your purposes.

Lastly, if you want to do other methods like keyreleased(), just add it in to the MyKeylistener class and it will work.

Hope this helps!

EDIT: Per OP's request, it should be like this:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = "";

while (line.equalsIgnoreCase("q") == false) {
line = in.read();

System.out.println("Q is pressed!");
}

in.close();

Listening for keypress when the browser isn't focused in javascript

to pause/stop a song played in background

As others have said, listening for keypress isn't possible. However, there is another way to do what you need.

If you use the Media Session API, all the standard media keys on the system will be relayed to your web application. Here's an example from the documentation:

navigator.mediaSession.setActionHandler('play', function() { /* Code excerpted. */ });
navigator.mediaSession.setActionHandler('pause', function() { /* Code excerpted. */ });

This works for Bluetooth remotes, and phone lock screens as well. Browser support is Chrome-only at the moment, but this is definitely a useful up-and-coming API that will likely see more compatibility in the near future.



Related Topics



Leave a reply



Submit