简体   繁体   English

JavaFX 全局键盘侦听器

[英]JavaFX global keyboard listener

I have a JavaFX application which requires the listening of a ctrl+space+T to bring the application window to the front when the application is in the background or minimized.我有一个 JavaFX 应用程序,当应用程序在后台或最小化时,它需要侦听 ctrl+space+T 以将应用程序窗口置于最前面。 May I know if anyone has a solution to it?我可以知道是否有人有解决方案吗?

public class MainApp extends Application {

@Override
public void start(Stage primaryStage) {
    primaryStage = setStagePositionToBottomRight(primaryStage);
    loadAndStartMainScreen(primaryStage);
}

private void loadAndStartMainScreen(Stage primaryStage) {
    MainViewController scenes = new MainViewController();
    AnchorPane mainScene = scenes.getMainView();
    Scene scene = new Scene(mainScene);
    primaryStage.setScene(scene);
    primaryStage.show();

}



private Stage setStagePositionToBottomRight(Stage primaryStage) {
    final int coordinateX = 335;
    final int coordinateY = 510;

    Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
    // primaryStage.initStyle(StageStyle.UNDECORATED);
    primaryStage.setX(primaryScreenBounds.getMaxX() - coordinateX);
    primaryStage.setY(primaryScreenBounds.getMaxY() - coordinateY);

    return primaryStage;

}

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

I think your requirement can't be implemented in pure java, you'll need some native code to accomplish this.我认为您的要求无法在纯 Java 中实现,您需要一些本机代码来实现这一点。

If you don't want do write everything on your own and your application is running on windows you can take a look at JIntellitype that offers global hotkey functionality.如果您不想自己编写所有内容并且您的应用程序在 Windows 上运行,您可以查看提供全局热键功能的JIntellitype

For anyone having a similar problem:对于任何有类似问题的人:

By listening to all userinputs, you can simply listen for the wanted key-combination, but this requires additional libraries.通过侦听所有用户输入,您可以简单地侦听所需的键组合,但这需要额外的库。 Thought this is not a good solution, it's a working one.认为这不是一个好的解决方案,它是一个有效的解决方案。

Link #1: about global eventlisteners in Java and the source of Link #2链接 #1:关于 Java 中的全局事件侦听器和链接 #2 的来源

Listening for input without focus in Java 在 Java 中没有焦点地监听输入

Link #2: a library with the needed functions for Windows, Linux, MacOS X链接 #2:具有 Windows、Linux、MacOS X 所需功能的库

https://github.com/kwhat/jnativehook https://github.com/kwhat/jnativehook

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

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