Try JNativeHook. Here is a sample that shows how to use it to capture global key presses:
try
{
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(new NativeKeyListener()
{
@Override
public void nativeKeyTyped(NativeKeyEvent nativeEvent)
{
}
@Override
public void nativeKeyReleased(NativeKeyEvent nativeEvent)
{
String keyText=NativeKeyEvent.getKeyText(nativeEvent.getKeyCode());
System.out.println("User typed: "+keyText);
}
@Override
public void nativeKeyPressed(NativeKeyEvent nativeEvent)
{
}
});
}
catch (NativeHookException e)
{
e.printStackTrace();
}
If you are using maven, add this to your pom.xml
:
<dependency>
<groupId>com.1stleg</groupId>
<artifactId>jnativehook</artifactId>
<version>2.1.0</version>
</dependency>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…