JavaFX:如何检测屏幕上任何位置的鼠标/按键事件? [英] JavaFX: How to detect mouse/key events anywhere on screen?

查看:123
本文介绍了JavaFX:如何检测屏幕上任何位置的鼠标/按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使应用程序窗口未聚焦,我仍试图在Java(fx)中捕获鼠标/键事件 ...我正在创建类似屏幕录像机之类的东西,并且想通过按"F9" 之类的键来停止录制,因此我需要检测该事件.这可能吗?我可以使用类似系统侦听器的东西吗?

〜亨利

解决方案

可以,但是如果注册的组件不在焦点之内,标准Java将无法访问按键或鼠标事件.

为此,您需要通过 Java本机接口(JNI)使用本机代码.这样,Java代码就可以调用本机应用程序(特定于硬件和操作系统平台的程序)以及用其他语言(如C和C ++)编写的库.

幸运的是,有一个第三方库 JNativeHook 专为您的需要而设计.您可以在这里找到它:

鼠标输出

通过这种方式,即使您的Java应用程序已最小化,您也可以检测到该事件.

希望这会有所帮助.

I´m trying to catch mouse/key events in Java(fx), even if the application window isn´t focused... I´m creating something like a screenrecorder and I want to stop the recording by pressing a key like "F9", so I need to detect the event. Is this possible? Is there something like a system listener I can use?

~Henri

解决方案

It is possible but standard Java does not have access to key-strokes or mouse events if the registered component is not in focus.

In order to achieve this you need to use native code via the Java Native Interface (JNI). This enables Java code to call native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C and C++.

Luckily, there is a third party library JNativeHook that was designed exactly for what you need. You can find it here: https://github.com/kwhat/jnativehook

If you are using Maven for dependency management you can install it easily. Here is a working example:

App.java

package com.sotest.globalkeylistener;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;

public class App 
{
    public static void main(String[] args) {
        try {
            GlobalScreen.registerNativeHook();
        }
        catch (NativeHookException ex) {
            System.exit(1);
        }

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

GlobalKeyListener.java

package com.sotest.globalkeylistener;

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

public class GlobalKeyListener 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 e1) {
                e1.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()));
    }
}

Key Output:

Mouse Output

This way you can detect the event even if your Java application is minimised.

Hope this helps.

这篇关于JavaFX:如何检测屏幕上任何位置的鼠标/按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆