'KeyListener does not handle keystrokes java

I'm creating a game with a level constructor.According to the idea, the player creates a level with the mouse, places the robot, cargo and loading location, and then moves the robot across the created field with the keys. There were no problems with the mouse, but for some reason the keyboard does not want to react.

The handler is in the MyPanel method, I tried attaching it to both the panel and the frame, but nothing helps(

private Sokoban(){
    game = new Game(ROWS,COLS);

    setPictures();
    myToolBar();
    myPanel();

    myFrame();
}

private void myPanel(){
    panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (Coord coord : Game.getCoords())
                g.drawImage((Image) game.getPicture(coord).image, coord.x * IMAGE_SIZE, coord.y * IMAGE_SIZE, this);
        }
    };
    panel.setFocusable(true);
    panel.requestFocusInWindow();
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            int x = e.getX()/IMAGE_SIZE;
            int y = e.getY()/IMAGE_SIZE;
            if(e.getButton() == MouseEvent.BUTTON1)
                game.mouseClicked(new Coord(x,y),clickMode);
            if(e.getButton() == MouseEvent.BUTTON3)
                game.mouseClicked(new Coord(x,y),Picture.EMPTY);
            panel.repaint();
        }
    });


    panel.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            super.keyTyped(e);
            if(e.getKeyCode() == KeyEvent.VK_A)
                game.moveRobot(BorderLayout.WEST);
            if(e.getKeyCode() == KeyEvent.VK_D)
                game.moveRobot(BorderLayout.EAST);
            if(e.getKeyCode() == KeyEvent.VK_W)
                game.moveRobot(BorderLayout.NORTH);
            if(e.getKeyCode() == KeyEvent.VK_S)
                game.moveRobot(BorderLayout.SOUTH);
            panel.repaint();
        }
    });




    panel.setPreferredSize(new Dimension(
            Game.getSize().x * IMAGE_SIZE,
            Game.getSize().y * IMAGE_SIZE));
    add(panel,BorderLayout.SOUTH);

}

private void myFrame(){
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setTitle("Sokoban");
    setResizable(false);
    setVisible(true);
    pack();
    setLocationRelativeTo(null);
}

private void myToolBar(){
    JToolBar toolBar = new JToolBar();
    JButton cargo = new JButton("Cargo");
    JButton reception = new JButton("Reception");
    JButton robot = new JButton("Robot");
    JButton grid = new JButton("Grid");
    cargo.addActionListener(new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            clickMode = Picture.CARGO;
            panel.setFocusable(true);
        }
    });
    reception.addActionListener(new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            clickMode = Picture.RECEPTION;
            panel.setFocusable(true);
        }
    });
    robot.addActionListener(new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            clickMode = Picture.ROBOT;
            panel.setFocusable(true);
        }
    });
    grid.addActionListener(new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            clickMode = Picture.GRID;
            panel.setFocusable(true);
        }
    });
    toolBar.add(grid);
    toolBar.add(cargo);
    toolBar.add(reception);
    toolBar.add(robot);
    toolBar.addSeparator();
    add(toolBar,BorderLayout.NORTH);
}

} '''



Solution 1:[1]

When you use keyTyped(KeyEvent e), the e.getKeyCode() is not set. You can use:

e.getKeyChar() == 'a'

or change listener type to keyPressed(KeyEvent e) and then do comparison like:

e.getKeyCode() == KeyEvent.VK_A

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tchudyk