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

时间:2015-11-03 15:45:13

标签: java javafx

我试图了解如何在不专注于JavaFX阶段的情况下获取键盘输入。 通常,当我想得到一个关键输入时,我这样做:

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

}

但是,当我没有专注于我的计划时,如何获得输入?

1 个答案:

答案 0 :(得分:3)

使用纯Java是不可能的,因为Java在JVM中运行,您无法收听全局击键。

您可以使用jnativehook之类的JNI库来执行所需的任务。我曾经在一个项目中使用过它,而且非常简单。这里有一段代码片段:

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

您只需将 jnativehook-2.0.3.jar 添加到您的构建路径中,就可以了。

编辑关于评论的简短笨拙的例子。

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
    }