Skip to content

dyorgio/jinputhook

Repository files navigation

JInputHook

Build Status Maven Central

Global Key and Shortcuts listeners for Java.

Why use it?

  • Create global keyboard/shortcut listeners.
  • As an alternative to excelent lib JNativeHook.

Usage

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 + ").");
    }
});

Maven

<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>

Windows

On Windows, include JNA as dependency.

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna-platform</artifactId>
    <version>${your.jna.version}</version>
</dependency>

Linux

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"