Global Key and Shortcuts listeners for Java.
- Create global keyboard/shortcut listeners.
- As an alternative to excelent lib JNativeHook.
For shortcut listener:
// Initialize
JInputHook.initialize();
// Register shortcut listener
Shortcut shortcut = Shortcut.fromKeys(Key.LCONTROL, Key.LSHIFT, Key.D);
JInputHook.addShortcutListener(shortcut,
new ShortcutListener() {
@Override
public void shortcutTriggered(Shortcut shortcut) {
System.out.println("shortcutTriggered(" + shortcut + ").");
}
}
);
For keyboard listener:
// Initialize
JInputHook.initialize();
// Register global keyboard listener
JInputHook.addListener(new GlobalKeyListener() {
@Override
public void keyPressed(Key key) {
System.out.println(".keyPressed(" + key + ").");
}
@Override
public void keyReleased(Key key) {
System.out.println(".keyReleased(" + key + ").");
}
});
<dependency>
<groupId>com.github.dyorgio.runtime</groupId>
<artifactId>jinputhook</artifactId>
<version>1.0.2</version>
<!-- Optional classifier by OS, don't use classifier to support ALL -->
<!--<classifier>mac-universal</classifier>-->
<!--<classifier>linux-universal</classifier>-->
<!--<classifier>win-universal</classifier>-->
</dependency>
On Windows, include JNA as dependency.
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>${your.jna.version}</version>
</dependency>
On Linux you need to adjust permissions of /dev/input/event* to can read:
sudo chmod o+r /dev/input/event*
Or, according with distro, create a new dev rule file on /etc/udev/rules.d/YOUR-FILE-NAME.rules:
SUBSYSTEM=="input", KERNEL=="event*", MODE="644"