Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Last active April 17, 2025 05:16
Show Gist options
  • Save Da9el00/5f698b3839f00ab3e0f28118edd6c947 to your computer and use it in GitHub Desktop.
Save Da9el00/5f698b3839f00ab3e0f28118edd6c947 to your computer and use it in GitHub Desktop.
JavaFX - Move a square with multiple keyboard inputs
package sample;
import javafx.animation.AnimationTimer;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Rectangle;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
private BooleanProperty wPressed = new SimpleBooleanProperty();
private BooleanProperty aPressed = new SimpleBooleanProperty();
private BooleanProperty sPressed = new SimpleBooleanProperty();
private BooleanProperty dPressed = new SimpleBooleanProperty();
private BooleanBinding keyPressed = wPressed.or(aPressed).or(sPressed).or(dPressed);
private int movementVariable = 2;
@FXML
private Rectangle shape1;
@FXML
private AnchorPane scene;
@FXML
void start(ActionEvent event) {
shape1.setLayoutY(200);
shape1.setLayoutX(280);
}
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long timestamp) {
if(wPressed.get()) {
shape1.setLayoutY(shape1.getLayoutY() - movementVariable);
}
if(sPressed.get()){
shape1.setLayoutY(shape1.getLayoutY() + movementVariable);
}
if(aPressed.get()){
shape1.setLayoutX(shape1.getLayoutX() - movementVariable);
}
if(dPressed.get()){
shape1.setLayoutX(shape1.getLayoutX() + movementVariable);
}
}
};
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
movementSetup();
keyPressed.addListener(((observableValue, aBoolean, t1) -> {
if(!aBoolean){
timer.start();
} else {
timer.stop();
}
}));
}
public void movementSetup(){
scene.setOnKeyPressed(e -> {
if(e.getCode() == KeyCode.W) {
wPressed.set(true);
}
if(e.getCode() == KeyCode.A) {
aPressed.set(true);
}
if(e.getCode() == KeyCode.S) {
sPressed.set(true);
}
if(e.getCode() == KeyCode.D) {
dPressed.set(true);
}
});
scene.setOnKeyReleased(e ->{
if(e.getCode() == KeyCode.W) {
wPressed.set(false);
}
if(e.getCode() == KeyCode.A) {
aPressed.set(false);
}
if(e.getCode() == KeyCode.S) {
sPressed.set(false);
}
if(e.getCode() == KeyCode.D) {
dPressed.set(false);
}
});
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<AnchorPane fx:id="scene" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Rectangle fx:id="shape1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="40.0" layoutX="280.0" layoutY="200.0" stroke="BLACK" strokeType="INSIDE" width="40.0" />
<Button layoutX="278.0" layoutY="26.0" mnemonicParsing="false" onAction="#start" text="Reset" />
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment