JavaFX KeyEvent ⌨️

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024

Комментарии • 29

  • @BroCodez
    @BroCodez  3 года назад +9

    package application;

    import java.io.IOException;
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.fxml.FXMLLoader;
    import javafx.stage.Stage;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.input.KeyEvent;
    public class Main extends Application {

    @Override
    public void start(Stage stage) throws IOException {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("Scene.fxml"));
    Parent root = loader.load();
    Controller controller = loader.getController();
    Scene scene = new Scene(root);

    scene.setOnKeyPressed(new EventHandler() {
    @Override
    public void handle(KeyEvent event) {

    switch(event.getCode()) {

    case W:
    controller.moveUp();
    break;
    case S:
    controller.moveDown();
    break;
    case A:
    controller.moveLeft();
    break;
    case D:
    controller.moveRight();
    break;
    default:
    break;
    }
    }
    });
    stage.setScene(scene);
    stage.show();
    }
    public static void main(String[] args) {
    launch(args);
    }
    }
    package application;
    public class Controller{

    public void moveUp() {

    System.out.println("MOVIN' UP!");
    }

    public void moveDown() {

    System.out.println("MOVIN' DOWN!");
    }

    public void moveLeft() {

    System.out.println("MOVIN' LEFT!");
    }

    public void moveRight() {

    System.out.println("MOVIN' RIGHT!");
    }
    }

  • @HocineFerradj
    @HocineFerradj 4 месяца назад +4

    May Allah bless you and your knowledge

  • @sug_madic7683
    @sug_madic7683 3 года назад +6

    Bro discord
    Can you make one discord server

  • @DetCoAnimeFan
    @DetCoAnimeFan 3 года назад +3

    first yes

  • @ethanalkout949
    @ethanalkout949 3 года назад +1

    Remember me when you get famous xD

  • @davidupton2017
    @davidupton2017 Год назад +1

    I was losing my mind trying to get a node to take the focus so it would pick up key events. This solution is much better and seems more useful. Thank you.

  • @pauste2459
    @pauste2459 2 года назад +1

    Hey, I did everything like you, but it kept telling me: Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "application.Controller.moveDown()" because "this.val$controller" is null ... How can i fix that?

    • @jorgeadrianpagessanchez7260
      @jorgeadrianpagessanchez7260 2 года назад +1

      same here

    • @jorgeadrianpagessanchez7260
      @jorgeadrianpagessanchez7260 2 года назад +3

      we didint linked Controller in scenebuilder

    • @benderbg
      @benderbg Год назад +5

      I had the same issue. Then I added/removed controller from Scene builder and it stopped bugging. A thing to note since its not explained in any of the videos is when you add a Scene.fxml file and call it with root object (which Bro's doing in all of his FX videos) it needs to have some content for us to be able to attach controller to it and generate fxml code inside. So make sure to add any pane (stage) to it at least and then add a controller (application.Controller under hierarchy section). Then save scene builder and reopen fxml to reflect changes. After that you can compile the code. This part is really annoying with Java. It might help someone else in 2023.

    • @user-kw8zv5qs4p
      @user-kw8zv5qs4p 10 месяцев назад

      Yes, in all cases it must be linked ٍScene
      @@benderbg

  • @learnwithmammad
    @learnwithmammad 3 года назад +1

    I check RUclips every day for you

  • @vatsalshah7473
    @vatsalshah7473 2 года назад

    how to make key events with number keys?
    Stuck on it. Need help!!!

  • @mooven1687
    @mooven1687 4 месяца назад

    you saved my life bro i love you

  • @potatolover0017
    @potatolover0017 3 месяца назад

    U r my hero like literally

  • @andresulloa5965
    @andresulloa5965 3 года назад

    thank you very much, i finally found what i've been looking for, you're the best

  • @EpicSylock
    @EpicSylock 2 года назад

    What if i wanted to press 2 keys down? Lets say Ctrl + O?

    • @andreray1795
      @andreray1795 Год назад +2

      scene.setOnKeyReleased(event -> {
      if(event.getCode().equals(KeyCode.O)) {
      if(event.isControlDown()) {
      doAnything;
      }
      }
      })

  • @Fisicamente
    @Fisicamente 3 года назад

    Aweasome!!
    Thank you a lot!

  • @michaelaffare
    @michaelaffare 2 года назад

    this is great. been looking for this for days. Thanks for sharing

  • @khusanbekgafurov
    @khusanbekgafurov Год назад

    Thanks, awesome content.

  • @yerusg4949
    @yerusg4949 3 года назад

    Nice

  • @sethg7336
    @sethg7336 3 года назад

    Thank you Bro

  • @imampower3409
    @imampower3409 3 года назад

    Nice video man :)

  • @ethanalkout949
    @ethanalkout949 3 года назад

    Awesome efforts 👍🔥

  • @vedantdeshmukh4949
    @vedantdeshmukh4949 3 года назад

    Hey bro 🔥 you are amazing 🔥

    • @BroCodez
      @BroCodez  3 года назад +1

      thanks for watching Vedant!