0

I have a code running on the IntelliJ terminal, but I am required to exit the code by pressing the ESC key.

The whole program is designed to run on the command line but I understand that there is no way to implement what I want through the command line. I also understand that the best approach is to use KeyBindings, but I don't quite understand how to incorporate it in my code, so I am using the following KeyEvent to check if the key pressed was ESC and if so, then close/exit the program.

public class Pizzeria extends JFrame{

    Pizzeria(PizzaFactory f) {
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent ke) { 
                if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {    
                    System.out.println("Escaping...");      //does not print; is the IF statement wrong?
                    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                }
            }
        });
    }

    public static void main(String[] args) {
        Pizzeria pizzaPatterns = new Pizzeria(new PizzaFactory());

        System.out.println("Hello and welcome!");
        // rest of my code, all CLI based

    }

The problem is, this does not work when I press the escape key from the terminal. How do I fix this?

EDIT

I set the visibility to true before implementing the "close when ESC is pressed" thing. And it works, but only when the window is in focus. So I am still unsure how to make it interact with my CLI.

Pizzeria(PizzaFactory f) {
        this.factory = f;
        setVisible(true);
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent ke) {  // handler
                if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    System.out.println("escaped ?");
                    System.exit(0);
                }
            }
        });
    }
Rachayita Giri
  • 487
  • 5
  • 17
  • *"The problem is, this does not work when I press the escape key from the terminal. How do I fix this?"* By ***not*** trying to mix GUI and command line apps. – Andrew Thompson Nov 13 '19 at 06:32
  • IntelliJ’s terminal is under the control of IntelliJ. You are not supposed to mess around with it. Besides that, a window that you never open will not receive keyboard input. Further, even if it did, `setDefaultCloseOperation` only said, as the method name suggests, what should happen *when* the window is closed. It doesn’t close it, so it won’t have any immediate effect. – Holger Nov 13 '19 at 09:33
  • @DebadattaMishra No, it did not. In fact, I tried the other answers from that thread too. @Holger the `dispose()` function is not working either. @AndrewThompson, how then, do I exit my program on pressing ESC? – Rachayita Giri Nov 13 '19 at 14:41
  • I have edited my post to show some progress. It works with the escape key but only when I am in the window and not on terminal/IntelliJ terminal. – Rachayita Giri Nov 13 '19 at 16:00
  • As stated in https://stackoverflow.com/questions/7250463/how-to-listen-esc-key-on-terminal and linked threads it is not possible to read non-character keys from the terminal with the standard API – MDK Nov 22 '19 at 18:31

0 Answers0