6

I have searched for a long time for how to write a KeyEvent to allow my Button clicks by ENTER key. Note that I'm using JavaFX and FXML files.

The problem is that when set in the onKeyTyped text field in the FXML file, the FXML Files doesn't accept it. It says Handle method not found. It just accept ActionEvent method so I have tried this code:

 @FXML
 private void key (KeyEvent evt) throws IOException{ 
       if (evt.getCode() == KeyEvent.VK_ENTER){
       String az = text1.getText();
       //c.1
       if(az.contains("1")){ 
          String hh = text11.getText();
          Socket socket = null;
          InetSocketAddress isa = new InetSocketAddress (hh,80);  
       } 
    }
}

So please can anybody help me?

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
APro
  • 83
  • 1
  • 2
  • 9
  • 1
    I would be very suprised (and disappointed) if this was the default behaviour of buttons in JavaFX. I think you need to have a look through [Handling JavaFX Events](http://docs.oracle.com/javafx/2/events/jfxpub-events.htm) and [Using JavaFX UI Controls](http://docs.oracle.com/javafx/2/ui_controls/button.htm). I'd be very suprised if something like `button2.setOnAction(new EventHandler() {` didn't deal with both the key event and mouse event, just like Swing does... – MadProgrammer Jan 16 '15 at 11:31
  • Okay, so apparently [Enter] isn't the default key...Try something like [this](http://stackoverflow.com/questions/25758782/fire-buttons-onaction-with-enter-in-javafx) instead – MadProgrammer Jan 16 '15 at 11:35

1 Answers1

9

You have few issues with your code :

  1. You are using onKeyTyped instead of onKeyPressed. For more information visit this link

  2. You are most probably using java.awt.event.KeyEvent, which will not work with JavaFX events. Try to use javafx.scene.input.KeyEvent.

    The reason I came to this conclusion is because JavaFX doesn't support KeyEvent.VK_ENTER but instead have KeyCode.ENTER

A concrete example is shown below, you can use the same to convert it into FXML:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class ButtonExample extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane pane = new BorderPane();
        Button button = new Button("Press Me!");
        pane.setCenter(button);
        Scene scene = new Scene(pane, 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();

        button.setOnKeyPressed(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.ENTER) {
                    System.out.println("Enter Pressed");
                }
            }
        });
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
Community
  • 1
  • 1
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176