简体   繁体   English

不专注于JavaFX阶段时如何获得键盘输入?

[英]How can I get keyboard inputs when not focused to the JavaFX stage?

I'm trying to understand how can I get the keyboard inputs without being focused to my JavaFX stage . 我试图了解如何在不专注于JavaFX阶段的情况下获得键盘输入。 Usually when I want to get a key input I'm doing this: 通常,当我想获取按键输入时,我正在这样做:

    public class Controller implements Initializable{

    @FXML
    Button btn ;

    @FXML
    VBox vBox ;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        vBox.setOnKeyPressed(new EventHandler<javafx.scene.input.KeyEvent>() {
            @Override
            public void handle(javafx.scene.input.KeyEvent event) {
                System.out.print(event.getCode());
            }
        });
    }

}

But how can I get the input when I'm not focused to my Program ? 但是当我不专注于我的计划时,如何获得输入?

This isn't possible using pure Java, since Java is running in the JVM you are not able to listen to global keystrokes. 使用纯Java不可能做到这一点,因为Java在JVM中运行,因此您无法侦听全局按键。

You could use a JNI library like jnativehook to perform your desired task. 您可以使用jnativehook之类的JNI库来执行所需的任务。 I once used it myself in a project and it's quite simple. 我曾经在项目中亲自使用过,而且非常简单。 Here a code snippet how it is done: 这里是一个代码片段是如何完成的:

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

class Example implements NativeKeyListener {

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

    public Example() {
        try {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException e) {
            e.printStackTrace();
        }
        GlobalScreen.addNativeKeyListener(this);
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent ev) {
        // check your specific key press here
        // example:
        if(ev.getKeyCode() == NativeKeyEvent.VC_H) {
            // the key "h" is pressed
            System.out.println("Hello!");
        }
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent arg0) {}

    @Override
    public void nativeKeyTyped(NativeKeyEvent arg0) {}
}

You just have to add the jnativehook-2.0.3.jar to your build path and you are good to go. 您只需将jnativehook-2.0.3.jar添加到您的构建路径中就可以了。

edit: A short and clumsy example regarding the comments. 编辑:有关评论的简短示例。

class Example implements NativeKeyListener {

        private Robot bot;
        private boolean ctrlPressed = false;
        private TextArea textArea1;
        private TextArea textArea2;
        // more TextAreas ...

        public Example(TextArea textArea1, TextArea textArea2) throws Exception {
            this.textArea1 = textArea1;
            this.textArea2 = textArea2;
            // ...
            bot = new Robot();

            Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
            logger.setLevel(Level.OFF);

            GlobalScreen.registerNativeHook();
            GlobalScreen.addNativeKeyListener(this);
        }

        @Override
        public void nativeKeyPressed(NativeKeyEvent ev) {
            if(ev.getKeyCode() == NativeKeyEvent.VC_CONTROL_L) {
                ctrlPressed = true; // ctrl key is pressed
            }
        }

        @Override
        public void nativeKeyReleased(NativeKeyEvent ev) {
            if(ev.getKeyCode() == NativeKeyEvent.VC_CONTROL_L) {
                ctrlPressed = false; // ctrl key is released
            }
            if(ev.getKeyCode() == NativeKeyEvent.VC_1 && ctrlPressed) { // check if ctrl + 1 is used
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                StringSelection stringSelection = new StringSelection(textArea1.getText()); // adding content of textArea1
                clipboard.setContents(stringSelection, stringSelection); // copy content to the system clipboard
                bot.keyPress(KeyEvent.VK_V); // use (ctrl+)v to paste current clipboard content
                bot.keyRelease(KeyEvent.VK_V);
            }
            if(ev.getKeyCode() == NativeKeyEvent.VC_2 && ctrlPressed) {
                // same as for ctrl+1 with the content of textArea2
            }
            // further if statements for the different key combinations and text areas
            // ....
        }

        @Override
        public void nativeKeyTyped(NativeKeyEvent arg0) {} // irrelevant
    }

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

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