创建一个忽略鼠标和按键事件的JavaFX透明窗口 [英] Creating a JavaFX transparent window that ignores mouse and key events

查看:113
本文介绍了创建一个忽略鼠标和按键事件的JavaFX透明窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个JavaFX应用程序,该应用程序基本上用Canvas对象覆盖整个用户屏幕,因此基本上我可以在用户屏幕上绘制任何东西.

I want to make a JavaFX application that basically overlays the entire user screen with a Canvas object, so basically I can draw anything on the user's screen.

制作一个覆盖整个屏幕的窗口很简单.可以通过以下教程实现使其基本透明: https://assylias.wordpress.com /2013/12/08/383/

Making a window that covers the whole screen is simple. Making it essentially transparent can be achieved with this tutorial: https://assylias.wordpress.com/2013/12/08/383/

所以阻止我的唯一一件事是,很明显,尽管窗口是透明的,它仍然可以捕获用户的鼠标和按键事件.

So the one and only thing stopping me is the fact that obviously, the window, albeit being transparent it will still capture user mouse and key events.

有没有办法可以做到这一点?举一个更具体的例子,想象一下我想在用户鼠标光标的任何地方画一个红色圆圈,但是用户输入不会被打断.

Is there a way I can achieve this? For a more concrete example, imagine I want to make a red circle surround the user's mouse cursor wherever it goes, but the user input will not be interrupted.

推荐答案

plain JavaFX中无法实现所需的功能.

What you want isn't possible in plain JavaFX.

您可以查看我的答案

You can check out my answer here, that's the closest thing. But you can't overlay a transparent canvas over the entire desktop and forward the mouse events to the underlying windows.

使Canvas为半透明会捕获所有事件,但是您可以看到基础窗口.但是当您具有完全透明的Canvas时,您的应用程序将不会捕获任何事件.

Having the Canvas semi-transparent would catch all events, but you could see the underlying windows. But when you have the Canvas fully transparent, your application wouldn't catch any events.

但是,您的具体示例"可以通过其他方式解决.这是代码:

However, your "concrete example" could be solved in a different way. Here's the code:

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class CircleAroundCursor extends Application {

    double radius = 50;

    @Override
    public void start(Stage primaryStage) {

        Group root = new Group();

        Circle circle = new Circle( radius * 2,radius * 2,radius);
        circle.setStroke(Color.RED);
        circle.setFill(Color.TRANSPARENT);

        root.getChildren().add(circle);

        Scene scene = new Scene(root, Color.TRANSPARENT);

        scene.getRoot().setStyle("-fx-background-color: transparent");

        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setAlwaysOnTop(true);


        AnimationTimer loop = new AnimationTimer() {

            @Override
            public void handle(long now) {

                PointerInfo info = MouseInfo.getPointerInfo();
                Point p = info.getLocation();

                primaryStage.setX(p.getX() - radius * 2);
                primaryStage.setY(p.getY() - radius * 2);

            }
        };
        loop.start();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这至少解决了我想在用户鼠标光标所在的位置上画一个红色圆圈,但用户输入不会被打断"

注意:此处AWT类与FX类混合在一起.您可能需要使用EDT& FX线程处理.它确实可以正常工作.

Note: Here AWT classes are mixed with FX classes. You may need to use an EDT & FX thread handling. It does work without though.

屏幕截图:

这篇关于创建一个忽略鼠标和按键事件的JavaFX透明窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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