0

I have a Java swing application in which it became necessary to work with global keyboard input keys for Windows, Mac and Linux OS. For this purpose I decided to use the jnativehook library version: '2.1.0', since it is said that it allows you to do this.

The goal is to catch a key press from the keyboard when you are inside the window of another application, namely Warcraft III.

This code works great, after launching the application, the data on the pressed buttons of the keyboard, mouse and mouse movements are displayed in the console.

Here is example how it works inside Notepad++. The code itself will be presented below at the end of the question: enter image description here

But the problem is, when the Warcraft III application is focused and active, nothing happens when you press a key, mouse, and move your mouse. All my activities are not showing up in the app console.

UPDATE: jnativehook doesn't work forTotal Commander window too when it's in focus.

This is what the Warcraft III app looks like in windowed mode that runs as administrator. enter image description here

Here is the sample code I am trying to use, taken from the official source:

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.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 {
                GlobalScreen.unregisterNativeHook();
            } catch (NativeHookException nativeHookException) {
                nativeHookException.printStackTrace();
            }
        }
    }

    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 {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());

            System.exit(1);
        }

        GlobalScreen.addNativeKeyListener(new GlobalKeyListenerExample());
    }
}

Questions: Why does jnativehook stop working when ​​the Warcraft III application is in focus? As it turned out, the same situation is when Total Commander window is in focus. But it works in Notepad ++ when it's in focus. Does it work selectively somehow?

Please tell me what could be the problem. My OS is Windows 10.

Thank you in advance for your advice.

Neo
  • 69
  • 7
  • Please don't post a duplicate of your question. Edit the original closed question so it can be re-opened. – Daniel Widdis Nov 22 '20 at 22:31
  • @DanielWiddis Sorry, I am new here.I already deleted the old question. – Neo Nov 22 '20 at 22:36
  • Is it possibly a permissions issue? Who is owner of the warcraft process? Is there the possibility of duplicate keybindings where the Warcraft key binding is taking precedence? – Daniel Widdis Nov 22 '20 at 23:38
  • @DanielWiddis The war3.exe (Warcraft III) process starts after clicking on the ("Play Warcraft III") button in the JFrame. There are no hooks when this process starts. This is difficult to reproduce as not everyone has this game installed. But you can try to reproduce it using Total Commander. I mean after running the code, open Total Commander and keyhook won't work while Total Commander is in focus. I don't even know what to do now. I read that it is necessary to use User32.LowLevelKeyboardProc () to achieve a low-level global key listener, regardless of which application is in focus. – Neo Nov 22 '20 at 23:58
  • I missed that it's a JFrame. I've found Swing components seem to have an abstraction level above that of Native, with other code. You might expand your search to include the issues you're seeing and other Swing programs. – Daniel Widdis Nov 23 '20 at 01:40
  • @DanielWiddis Thanks for your response. But for this question, I have attached the specific code that I am running, there is no swing or awt connection there. I don't know how else to explain, just after running this code key and mouse listeners don't work in certain windows like Total Commander and Warcraft III. – Neo Nov 23 '20 at 11:01
  • 1
    This may be related to https://github.com/kwhat/jnativehook/issues/296 – weisj Nov 23 '20 at 11:48

0 Answers0