java baba
java baba

Reputation: 2259

how know pressed key is character only in javafx

how know pressed key from Key Board is character only in javafx. i want to handle following condition

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

// some Code
}

ISCHARACTERKEY() include A-Z or a-z only. is Javafx provide ISCHARACTERKEY() type of inbuilt method?

Upvotes: 0

Views: 3183

Answers (1)

Patrick
Patrick

Reputation: 4572

Yes, it does:

/*
 * 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() is your method..

Patrick

Upvotes: 3

Related Questions