问题描述
我试图理解如何在不专注于JavaFX阶段的情况下获得键盘输入。
通常当我想获得一个键输入时,我这样做:
I'm trying to understand how can I get the keyboard inputs without being focused to my JavaFX stage .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 ?
推荐答案
这是不可能使用纯Java,因为Java正在运行JVM你无法听全局击键。
This isn't possible using pure Java, since Java is running in the JVM you are not able to listen to global keystrokes.
你可以使用像执行您想要的任务。我曾经在一个项目中使用它,这很简单。这里是一个代码片段如何完成:
You could use a JNI library like jnativehook to perform your desired task. 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) {}
}
你只需添加 jnativehook -2.0.3.jar 到你的构建路径,你很高兴。
You just have to add the jnativehook-2.0.3.jar to your build path and you are good to go.
编辑:一个简短而笨拙的例子关于评论。
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
}
这篇关于如果不专注于JavaFX阶段,如何获得键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!