kwhat / jnativehook

Global keyboard and mouse listeners for Java.
Other
1.73k stars 344 forks source link

JNativeHook Latest Stable+Night Release (2.1.0) reports false Control Key Released inside JavaFX Stage #153

Open goxr3plus opened 7 years ago

goxr3plus commented 7 years ago

First of all i want to thank all of you for this amazing project!!

->Both the (jnativehook-2.1.0.jar) and latest night release tested under:

Windows 10 + Java 1.8.0_112 are reporting false Control Key Released inside JavaFX Stage.

The problem When JavaFX Window is out of focus then it works nicely , but when i focus JavaFX Window i am getting false CTL KEY RELEASED , image below :

image

Sometimes it works well as you can see and some other times is reporting those false CTRL KEY RELEASED...


The code is below:


import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

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

/**
 * JavaFXNativeHookExample
 *
 */
public class JavaFXNativeHookExample extends Application {

    @Override
    public void start(final Stage primaryStage) {
        final StackPane root = new StackPane();
        final Scene scene = new Scene(root, 300, 250);

        // Clear previous logging configurations.
        LogManager.getLogManager().reset();

        // Get the logger for "org.jnativehook" and set the level to off.
        Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
        logger.setLevel(Level.OFF);

        final Text info = new Text();
        root.getChildren().add(info);

        try {
            GlobalScreen.setEventDispatcher(new SwingDispatchService());
            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 NativeKeyListener() {
            public void nativeKeyPressed(NativeKeyEvent e) {
                info.setText(NativeKeyEvent.getKeyText(e.getKeyCode()));

                System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
            }

            public void nativeKeyReleased(NativeKeyEvent e) {
                System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
            }

            public void nativeKeyTyped(NativeKeyEvent e) {
                System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
            }
        });

        Stage widget = new Stage();
        Platform.setImplicitExit(true);
        widget.setTitle("JavaFXNativeHook");
        widget.initStyle(StageStyle.UTILITY);
        widget.initModality(Modality.APPLICATION_MODAL);
        widget.setScene(scene);
        widget.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {
        /* fix for osx */
        System.setProperty("javafx.macosx.embedded", "true");
        java.awt.Toolkit.getDefaultToolkit();

        /*
         * In OSX you must activate accessibility.
         * 
         * To turn it on, type this in Terminal: > sudo touch
         * /private/var/db/.AccessibilityAPIEnabled
         * 
         * To then disable it, type this: > sudo rm
         * /private/var/db/.AccessibilityAPIEnabled
         */

        /* start the app */
        launch(args);

    }
}
xunkar commented 7 years ago

I'm facing a very similar problem. I'm not using JavaFX though, just Swing. The Ctrl key is being falsely realeased every input and every second key input has the left control key mask (value = 2) active even though it is not. As with your issue, it only happens when the window has the focus, otherwise it works as expected. I'm running Windows 10 x64.

Also, it started happening overnight but I'm not using the nightly, just the 2.1.0 stable so I'm not exactly sure. It seems that this is pointing towards a system update but I guarantee that none have happen recently.

kwhat commented 7 years ago

There is something wrong with the nightly masks on linux. I have some time this weekend so I am probably going to try a different approach without xkbcommon.

kwhat commented 7 years ago

Ok this should address a number of issues that people have been having including this one. Please test and let me know if this issue can be closed.

JNativeHook-2.1.1-rc

JNativeHook-2.0.4-rc

sirgyula commented 7 years ago

The 2.1.1-rc link seems to be broken, so I've used the jnativehook-2.1.20170802.062507-36.jar, but it doesn't seem to work. I've tried both the keyboard and mouse examples, the log keeps filling the console, but no actual println was called. When the 2.1.0 was included, everything was fine (except the ctrl misfire)! (Java 1.8.0_144, win10 prof 1703)

goxr3plus commented 7 years ago

I will try the latest released to check if it works correctly ->

https://oss.sonatype.org/content/repositories/snapshots/com/1stleg/jnativehook/2.1.SNAPSHOT/

Fer-Jg commented 3 years ago

Nothing on https://oss.sonatype.org/content/repositories/snapshots/com/1stleg/jnativehook/2.1.SNAPSHOT/ is working to download, was this issue fixed? I'm having the same error (I'm using Swing, I think).

kwhat commented 3 years ago

@Fer-Jgu It moved, https://oss.sonatype.org/content/repositories/snapshots/com/github/kwhat/jnativehook/2.2-SNAPSHOT/

No 2.2 release yet, I have some more stuff to deprecate but it should be stable in snapshot state.