JavaFX 全局键盘监听器

标签 java keyboard javafx listener

我有一个 JavaFX 应用程序,需要监听 ctrl+space+T,以便在应用程序处于后台或最小化时将应用程序窗口置于前面。请问有人有解决办法吗?

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

最佳答案

我认为你的要求无法用纯java实现,你需要一些 native 代码来完成这个。

如果您不想自己编写所有内容并且您的应用程序在 Windows 上运行,您可以查看 JIntellitype提供全局热键功能。

关于JavaFX 全局键盘监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19768614/

相关文章:

java - 如何使用 JavaFx 删除图像的颜色(使背景透明)?

java - 意外的顶级错误

Java 模型验证

ios - 在 keyboardWillShow 通知中从 Swift 2 转换为 3 时出现可选错误

java - 将查询列表插入组合框的问题

java - 在FXML文件中设置自定义控件的Button "onAction"方法?

java - Camel 2.10 更改 blueprint.xml 位置

java - JDBCTemplate INSERT INTO 语句 : Can't figure out error

javascript - React - 当按下 'Enter' 键时调用特定函数

android - 带有软键盘和 "Back"按钮的 EditText