简体   繁体   English

创建一个忽略鼠标和按键事件的 JavaFX 透明窗口

[英]Creating a JavaFX transparent window that ignores mouse and key events

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.我想制作一个 JavaFX 应用程序,它基本上用Canvas对象覆盖整个用户屏幕,所以基本上我可以在用户屏幕上绘制任何内容。

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/使用本教程可以实现基本透明: 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.举一个更具体的例子,想象一下我想在用户鼠标光标所在的地方制作一个红色圆圈,但用户输入不会被中断。

What you want isn't possible in plain JavaFX.您想要的东西在普通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.但是您不能在整个桌面上覆盖透明画布并将鼠标事件转发到底层窗口。

Having the Canvas semi-transparent would catch all events, but you could see the underlying windows.使 Canvas 半透明将捕获所有事件,但您可以看到底层窗口。 But when you have the Canvas fully transparent, your application wouldn't catch any events.但是当您让 Canvas 完全透明时,您的应用程序将不会捕获任何事件。

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

This at least solves "I want to make a red circle surround the user's mouse cursor wherever it goes, but the user input will not be interrupted"这至少解决了“我想在用户的鼠标光标所到之处做一个红色圆圈,但用户输入不会被中断”

Note: Here AWT classes are mixed with FX classes.注意:这里 AWT 类与 FX 类混合在一起。 You may need to use an EDT & FX thread handling.您可能需要使用 EDT 和 FX 线程处理。 It does work without though.不过它确实可以工作。

Screenshot:截图:

在此处输入图片说明

You may have a look at the Robot class.您可以查看Robot类。 I have abused its powers many times, although I consider most solutions I used that class for as hacky.我已经多次滥用它的权力,尽管我认为我使用该类的大多数解决方案都是笨拙的。

Maybe you could do it like this:也许你可以这样做:

  1. intercept MouseEvent and save its properties拦截 MouseEvent 并保存其属性
  2. do your things like drawing做你喜欢画画的事
  3. make the Window invisible使窗口不可见
  4. invoke the same MouseEvent with the help of RobotRobot的帮助下调用相同的 MouseEvent
  5. make the Windows visible again使 Windows 再次可见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM