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