JavaFX KeyEvent

0

KeyEvent Key_Released or Key_Pressed Event handler with JavaFX

This type of Event occurs when a key is typed, pressed or released.

  • keyEvent.getCode() method is used to returns the associated keycode for this event.
  • isAltDown() method return true if ALT key is down in this event otherwise false.
  • isControlDown() method return true if Ctrl key is down in this event otherwise false.
  • isShiftDown() method return true if Shift key is down in this event otherwise false.

If you are beginners Then read basic JavaFX Event tutorials.

Example for key-pressed type KeyEvent.

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

public class Javafxtuts extends Application {

  public static void main(String[] args) {
     Application.launch(args);
  }

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      Label label =new Label();
	//creating a new button.
      Button button = new Button();
      button.setText("Press a key");
	//register a handler for button.
      label.setText("Please presse a Key or a Key combination.");
      button.setOnKeyPressed(new EventHandler() {

          @Override
          public void handle(KeyEvent keyEvent) {
              String text="";
              //Check if alt key is pressed or  not
              if(keyEvent.isAltDown()){
              text="ALT+";
              }
               //Check if ctrl key is pressed or  not
              if(keyEvent.isControlDown()){
              text="CTRl+";}
              if(keyEvent.isShiftDown()){
              text="SHIFT+";}
              if(keyEvent.isMetaDown()){
              text="META+";}
               //change the text of label
              label.setText(text+""+keyEvent.getCode()+" Key is entered.");
             
          }

       });
      BorderPane root = new BorderPane();
      root.setTop(button);
      root.setCenter(label);
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Example for key-released type KeyEvent.

It is simillar to the Key-pressed but differece is to set Key released event setOnKeyReleased() method.


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

public class Javafxtuts extends Application {

  public static void main(String[] args) {
     Application.launch(args);
  }

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      Label label =new Label();
	//creating a new button.
      Button button = new Button();
      button.setText("Press a key");
	//register a handler for button.
      label.setText("Please presse a Key And release it.");
      button.setOnKeyReleased(new EventHandler() {
		
          @Override
          public void handle(KeyEvent keyReleasedEvent) {
              //change the text of label
              label.setText(keyReleasedEvent.getCode()+" Key is Released.");  
          }  
    });
      BorderPane root = new BorderPane();
      root.setTop(button);
      root.setCenter(label);
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();
  }
}
Share.

Leave A Reply