2014-02-10 66 views
0

如何知道在鍵盤上按下的鍵只是在javafx中的字符。 我想要處理以下的條件如何在javafx中只知道按下的鍵是字符

if(event.isControlDown() && event.getCode().ISCHARACTERKEY()){ 

// some Code 
} 

ISCHARACTERKEY()包括A-Z或僅A-Z。 是Javafx提供的ISCHARACTERKEY()類型的內置方法嗎?

回答

3

是的,它的作用:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package keycodetester; 

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.TextField; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

/** 
* 
* @author ottp 
*/ 
public class KeyCodeTester extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     TextField tf = new TextField(); 
     tf.setOnKeyPressed(new EventHandler<KeyEvent>() { 

      @Override 
      public void handle(KeyEvent event) { 

       if(event.isAltDown() && event.getCode().isLetterKey()) { 
        System.out.println("Character"); 
       } 
      } 

    }); 
     StackPane root = new StackPane(); 
     root.getChildren().add(tf); 

     Scene scene = new Scene(root, 300, 250); 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    /** 
    * The main() method is ignored in correctly deployed JavaFX application. 
    * main() serves only as fallback in case the application can not be 
    * launched through deployment artifacts, e.g., in IDEs with limited FX 
    * support. NetBeans ignores main(). 
    * 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 

event.getCode().isLetterKey()是你的方法..

帕特里克

相關問題