Search code examples
javaeclipsekey-bindings

how to key-bind ESC


My problem is that ESC does not do anything, even though it is the same code as for w, a and s which work, and when I push the button, w, a and s no longer work. What this code should do is display the JFrame with a JLabel that should move up, down, left and right when I press w, s, a and ESC respectively. this is the video I was using as a guide (I am not implying it is a bad video) https://www.youtube.com/watch?v=IyfB0u9g2x0&t=118s

Game(){
    
        frame = new JFrame("KeyBinding Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420,420);
        frame.setLayout(null);
        
        label = new JLabel();
        label.setBackground(Color.red);
        label.setBounds(100, 100, 100, 100);
        label.setOpaque(true);
        b = new JButton();
        b.setBackground(Color.red);
        b.setBounds(400, 400, 400, 400);
        b.setOpaque(true);
        upAction = new UpAction();
        downAction = new DownAction();
        leftAction = new LeftAction();
        rightAction = new RightAction();
        
        label.getInputMap().put(KeyStroke.getKeyStroke('w'), "upAction");
        label.getActionMap().put("upAction", upAction);
        label.getInputMap().put(KeyStroke.getKeyStroke('s'), "downAction");
        label.getActionMap().put("downAction", downAction);
        label.getInputMap().put(KeyStroke.getKeyStroke('a'), "leftAction");
        label.getActionMap().put("leftAction", leftAction);
        label.getInputMap().put(KeyStroke.getKeyStroke("ESC"), "rightAction");
        label.getActionMap().put("rightAction", rightAction);
        frame.add(b);
        frame.add(label);
        frame.setVisible(true);
    }
    
    public class UpAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX(), label.getY()-10);
        }       
    }
    public class DownAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX(), label.getY()+10);   
        }       
    }
    public class LeftAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX()-10, label.getY());   
        }       
    }
    public class RightAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX()+10, label.getY());
        }       
    }
}

This is where I run it

public class Main{
    public static void main(String[] args ){
        Game game = new Game();
    }
}

Solution

  • label.getInputMap().put(KeyStroke.getKeyStroke("ESC"), "rightAction");
    

    The label on your keyboard means nothing to Java.

    What is important are the variables defined in the KeyEvent class:

    VK_ESCAPE
    

    Therefore to create the KeyStroke you use:

    label.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "rightAction");
    

    but still when i click on the button, the keys stop working

    You are using the default InputMap, which will only work when the component has focus. There are 3 InputMaps. Read the Swing tutorial on How to Use Key Bindings.

    You should use the WHEN_ANCESTOR_OF_COMPONENT input map. And the key bindings should be added to the content pane of the frame, not the label.