怎么知道按下的键只是在javafx中的字符

时间:2014-02-10 06:14:33

标签: javafx javafx-2

如何知道Key Board中的按键只是javafx中的字符。 我想处理以下条件

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

// some Code
}

ISCHARACTERKEY()仅包括A-Z或a-z。 是Javafx提供内置方法的ISCHARACTERKEY()类型吗?

1 个答案:

答案 0 :(得分: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()是您的方法..

帕特里克