HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Detecting keyboard input

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
inputkeyboarddetecting

Problem

I want to use JavaFX as a solution to Java's issues with buffered input making things like 'press any key to continue' not easy.

I plan to use an event listener to detect keyboard input. But before I start down that road I want to know if this is on the right track.

Is the code below a good way to use JavaFX to implement a shell-based program?

  • Is this the minimum JavaFX code required to do shell?



  • Am I missing something that's gonna bite me down the road (like some kind of object de-referencing: I am very new to JavaFX so I don't have a clue!)



  • Is it OK to consider the start method as effectively now my main method?



import java.util.Scanner;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;

public class TextBased extends Application {
    public static void main(String argv[]) {
        Application.launch(argv);
    }   

@Override
public void start(Stage primaryStage) {
    System.out.println("Hello world");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    Platform.exit();
    }   
}

Solution

There's not a lot to review here, the only thing that's jumping at me is that this:

@Override
public void start(Stage primaryStage) {
    System.out.println("Hello world");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    Platform.exit();
    }


Should be indented like this:

@Override
public void start(Stage primaryStage) {
    System.out.println("Hello world");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    Platform.exit();
}



Is this the minimum JavaFX code required to do shell?

Well technically printing "Hello world" and waiting for the ENTER key is not needed if what you want is the minimum code. But then, what would your program be doing :-)


Am I missing something that's gonna bite me down the road (like some kind of object de-referencing: I am very new to JavaFX so I don't have a clue!)

I know nothing of JavaFX, but if garbage collection is anywhere like .net, you have nothing to worry about because AFAICT you're not creating any object that's holding on to a reference that would prevent its garbage collection.


Is it OK to consider the start method as effectively now my main method?

Probably. But it's not immediately clear what happens to argv.

Code Snippets

@Override
public void start(Stage primaryStage) {
    System.out.println("Hello world");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    Platform.exit();
    }
@Override
public void start(Stage primaryStage) {
    System.out.println("Hello world");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    Platform.exit();
}

Context

StackExchange Code Review Q#114325, answer score: 7

Revisions (0)

No revisions yet.