It is possible but standard Java does not have access to key-strokes or mouse events if the registered component is not in focus.
In order to achieve this you need to use native code via the Java Native Interface (JNI). This enables Java code to call native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C and C++.
Luckily, there is a third party library JNativeHook that was designed exactly for what you need. You can find it here: https://github.com/kwhat/jnativehook
If you are using Maven for dependency management you can install it easily. Here is a working example:
App.java
package com.sotest.globalkeylistener;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
public class App
{
public static void main(String[] args) {
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.exit(1);
}
GlobalScreen.addNativeKeyListener(new GlobalKeyListener());
}
}
GlobalKeyListener.java
package com.sotest.globalkeylistener;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class GlobalKeyListener implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException e1) {
e1.printStackTrace();
}
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
}
}
Key Output:

Mouse Output

This way you can detect the event even if your Java application is minimised.
Hope this helps.