Reputation: 1215
I am trying to get user input's text without any textfield with JavaFX (not with console) but I am stuck with the code. Basically what I would like to do is during the login page user will receive a random string of OTP automatically generated from a token USB by pressing a button in the USB and the string will be automatically received with the login page when the form is active without any textfield involved.
The code for the presenter that I can come up now is like this:
public class LoginBoxPresenterFinal implements Initializable {
...
public void initialize(URL arg0, ResourceBundle arg1) {
....
Scanner reader = new Scanner(System.in);
String otp = reader.nextLine();
System.out.println(otp);
}}
This code is wrong actually as the Scanner will only receive the input in the Eclipse's console and it will not show the login page until i type something in the console and press Enter button.
Any help is appreciated. Thanks
Upvotes: 1
Views: 736
Reputation: 10859
i can help you Sir.
Stage s = new Stage(StageStyle.DECORATED);//suppose this is your stage
s.initModality(Modality.APPLICATION_MODAL);
p = new Pane();
p.setStyle("-fx-background-color: yellow");
s.setScene(new Scene(p,150,150));
s.show();
s.addEventFilter(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if(t.getCode().equals(KeyCode.ENTER)){
//this is when the user clicks enter meaning he is done or something
//like that
}else{
//here the code detect keycodes other than enter to get them
KeyCode kc = t.getCode();
//now store kc somewhere and use it.
}
t.consume();//the consume here is because i do not want
//the TextField to hear about the keypressed.
//remove it if you want it to be notified
}
});
This is how the code works since you do not want the TextField
to interrupt
call add an eventfliter on your Stage
and the Stage will be the first to be notified of any key press by ur USB keyboard. That is all, very easy
Hope it helps
Upvotes: 2