[英]how know pressed key is character only in javafx
如何知道键盘上的按下键仅是javafx中的字符。 我想处理以下情况
if(event.isControlDown() && event.getCode().ISCHARACTERKEY()){
// some Code
}
ISCHARACTERKEY()仅包含AZ或az。 Javafx是否提供ISCHARACTERKEY()类型的内置方法?
是的,它确实:
/*
* 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()
是您的方法。
帕特里克
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.