Try global hooking in Java using the JNativeHook library

Writer environment

Windows10 Home / JDK1.8

Maven repository

<dependency>
    <groupId>com.1stleg</groupId>
    <artifactId>jnativehook</artifactId>
    <version>2.1.0</version>
</dependency>

Try it for the time being (click the key)

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class Example implements NativeKeyListener {
    public static void main(String[] args) {
        //If not hooked
        if (!GlobalScreen.isNativeHookRegistered()) {
            try {
                //Register hook
                GlobalScreen.registerNativeHook();
            } catch (NativeHookException e) {
                e.printStackTrace();
                System.exit(-1);
            }
        }
        //Register key listener
        GlobalScreen.addNativeKeyListener(new Example());
    }

    //When you press a key
    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
        System.out.println(e.paramString());
    }
    
    //When you release the key
    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
        System.out.println(e.paramString());
    }

    //When you type the key
    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {
        System.out.println(e.paramString());
    }
}

This code allows you to have a key listener for all running threads, not just the thread running this code.

The event will occur no matter which software you press the key on, such as Chrome or Minecraft.

Also, each key event constant is the same as each java.awt.event.KeyListener constant.

What about other listeners?

//Mouse listener
GlobalScreen.addNativeMouseListener(NativeMouseListener);

//Mouse motion listener
GlobalScreen.addNativeMouseMotionListener(NativeMouseMotionListener);

//Mouse wheel listener
GlobalScreen.addNativeMouseWheelListener(NativeMouseWheelListener);

And mouse related listeners are available.

Erase the log

By default, a considerable amount of logs are output, so if you do not need it LogManager.getLogManager().reset(); Let's add even around the main method.

Make the event a textual representation

There is a paramString () method, so let's use it.

Recommended Posts

Try global hooking in Java using the JNativeHook library
Try using the Stream API in Java
Try using the COTOHA API parsing in Java
Try using RocksDB in Java
Try implementing the Eratosthenes sieve using the Java standard library
Try adding text to an image in Scala using the Java standard library
[Java] Try editing the elements of the Json string using the library
[Java] Use cryptography in the standard library
Try using JSON format API in Java
Try calling the CORBA service in Java 11+
Try using the Wii remote with Java
ChatWork4j for using the ChatWork API in Java
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Try accessing the dataset from Java using JZOS
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Differences in code when using the length system in Java
Try scraping using java [Notes]
Try calling JavaScript in Java
Try functional type in Java! ①
Try using gRPC in Ruby
Implement Thread in Java and try using anonymous class, lambda
I tried using the CameraX library with Android Java Fragment
Access the network interface in Java
[Java] Get date information 10 days later using milliseconds in the Date class
Try implementing Android Hilt in Java
[Java] Try to solve the Fizz Buzz problem using recursive processing
Try implementing GraphQL server in Java
Specify the java location in eclipse.ini
Java comparison using the compareTo () method
Unzip the zip file in Java
Try running Selenuim 3.141.59 in eclipse (java)
Try an If expression in Java
Parsing the COTOHA API in Java
Try using the messaging system Pulsar
HTTPS connection using tls1.2 in Java 6
I tried using JWT in Java
Try running AWS X-Ray in Java
How to solve the unknown error when using slf4j in Java
Try to implement Yubaba in Java
Try using IBM Java method tracing
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
Call the super method in Java
NLP4J [002] Try parsing Japanese using Yahoo! Developer Network Japanese Parsing Analysis (V1) in Java
Parse and objectize JSON using the @JsonProperty annotation of the Java library Jackson
About the phenomenon that StackOverflowError occurs in processing using Java regular expressions
Try using Java framework Nablarch [Web application]
Get the result of POST in Java
Try using || instead of the ternary operator
Try to solve Project Euler in Java
Java reference to understand in the figure
Try using the service on Android Oreo
Newcomer training using the Web-Basic programming using Java-
Call the Windows Notification API in Java
Try using the Rails API (zip code)
Study Java Try using Scanner or Map
Using JavaScript from Java in Rhino 2021 version
Organized memo in the head (Java --Array)