JavaFX KeyEvent ⌨️

preview_player
Показать описание
JavaFX KeyEvent tutorial example explained

#javafx #keyevent #keys
Рекомендации по теме
Комментарии
Автор

package application;

import java.io.IOException;

import
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
Parent root = loader.load();
Controller controller = loader.getController();
Scene scene = new Scene(root);

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

@Override
public void handle(KeyEvent event) {

switch(event.getCode()) {

case W:


case S:


case A:


case D:


default:

}
}
});

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!");
}

}

BroCodez
Автор

May Allah bless you and your knowledge

HocineFerradj
Автор

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.

DuptoUpto
Автор

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

andresulloa
Автор

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

michaelaffare
Автор

Bro discord
Can you make one discord server

sug_madic
Автор

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

vatsalshah
Автор

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

pauste
Автор

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

EpicSylock
join shbcf.ru