Java monitors Mac keyboard input for instant functionality

Background requirements

When you want to use Java to register news on the Mac, you can use jnavivehook. Then you find that the downloaded example can't run. This article is about how to solve this problem and how to realize system level update on MAC through this jnavivehook.


Jnatehook provides keyboard and mouse event listening for Java programs. You can handle keyboard input and mouse action outside the program. Of course, jnatehook uses JNI technology to call the system method to realize this function.

github address: GitHub - kwhat/jnativehook: Global keyboard and mouse listeners for Java.

Reference the correct dependency

If you search in maven warehouse, you will find that it has several sources

At this time, you should not click the first one, because when you use the first one, the subsequent Simple Code will not be executed.

The right one should be the one who chooses the latest update in 2021, that is, it

Simple example

Reading on jnaturehookgithub actually tells a very practical example, which can meet your needs.

Well, at this time, you created a Maven project and introduced the latest dependencies. I think it's right.

  Scan VX for Java data, front-end, test, python and so on

Then, when you copy the Demo to start the main method, a prompt will pop up when you execute the mac for the first time, saying to grant Applicaiton permission.

Similar to the following screen, the permission is OK

After successfully executing the performance of this tool, you can monitor your operations in real time.

Registered community news

OK, after that, it's time for us to talk about the code. In the process of using jnaturehook, it seems that there are no specific popular implementations, that is, some of them need to be completed by us.

Of course, after completing the above demo, I guess you should also guess what to do. When asked that we can monitor all the inputs of the mac, the corresponding analog input and then the small button triggers the corresponding function is to realize the fast function.

OK, let's start with a small function, which is to know how to only monitor all keyboard inputs

Simple: jnativehook/ at 2.2 · kwhat/jnativehook · GitHub

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;

public class GlobalKeyListenerExample implements NativeKeyListener {
	public void nativeKeyPressed(NativeKeyEvent e) {
		System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

		if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
            		try {
            		} catch (NativeHookException nativeHookException) {

	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()));

	public static void main(String[] args) {
		try {
		catch (NativeHookException ex) {
			System.err.println("There was a problem registering the native hook.");


		GlobalScreen.addNativeKeyListener(new GlobalKeyListenerExample());

The function of expression is to output and print any input of the keyboard.

The next step is to transform the method to be implemented, nativeKeyPressed, and make a simple shortcut. Now I want to implement the shortcut: Ctrl + Command + P to trigger

The method nativeKeyPressed at that time was modified

    protected final static LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue();

    public void nativeKeyPressed(NativeKeyEvent e) {
        //When there is keyboard input, it is put into the queue
        try {
        } catch (InterruptedException ex) {

        // Ctrl + command + p
        int[] hotKeyArray = {NativeKeyEvent.VC_CONTROL, NativeKeyEvent.VC_META, NativeKeyEvent.VC_P};
        //If the data in the queue is greater than or equal to 3, judge whether it contains continuous and equal to the order of the keys we specify
        //If there is, execute the punishment
        if (queue.size() >= 3 && judgeCombinationKey(hotKeyArray)){
            try {
                do something.........
            } catch (InterruptedException ex) {
        if (queue.size() == 4){
    protected Boolean judgeCombinationKey(int[] hotKeyArray){
        Object[] queueKey = queue.toArray();

        Predicate<int[]> keyArrayPredicateOne = hotKeies -> (int)queueKey[0] == hotKeies[0]
                && (int)queueKey[1] == hotKeies[1]
                && (int)queueKey[2] == hotKeies[2];

        Predicate<int[]> keyArrayPredicateTwo = hotKeies -> (int)queueKey[1] == hotKeies[0]
                && (int)queueKey[2] == hotKeies[1]
                && (int)queueKey[3] == hotKeies[2];

        return queue.size() == 3 ? keyArrayPredicateOne.test(hotKeyArray) :


Well, when the above content is completed, it is actually completed!

  Scan VX for Java data, front-end, test, python and so on

Tags: Java network Back-end

Posted on Tue, 30 Nov 2021 17:15:49 -0500 by alego