Cancel Control + V (Paste) in JavaFX

1

I have a file .fxml where I have defined a control PasswordField and an event KeyPressed :

<PasswordField fx:id="clave" onKeyPressed="#teclaPulsada" prefWidth="170.0" />

I want to avoid having someone paste a copied key and thus force it to be typed. I'm trying with the following code to detect the key control or command (depending on whether it's win or mac) but I do not know how to detect the v key at the same time (control / command + v)

@FXML
    void teclaPulsada(KeyEvent keyEvent) {
       if (keyEvent.getCode() == KeyCode.COMMAND) {
          System.out.println("pulsado command");
          keyEvent.consume();

          if (keyEvent.getText() == "v") {
             System.out.println("pulsada la v");
             keyEvent.consume();
          }
       }
    } 
    
asked by Oundroni 06.05.2016 в 15:37
source

1 answer

0

You must use a listener for an instance of the class KeyCodeCombination :

final KeyCodeCombination keyComb = new KeyCodeCombination(KeyCode.V, KeyCombination.CONTROL_DOWN);
miPasswordField.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
    if (keyComb.match(event)) {
        System.out.println("Se ha presionado Ctrl+V.");          
    }
});
    
answered by 06.05.2016 / 21:36
source