title: KeyListener does not handle keystrokes java
body: <p>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.</p>
  <p>The handler is in the MyPanel method, I tried attaching it to both the panel and the frame, but nothing helps(</p>
  <pre><code>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(&quot;Sokoban&quot;);
      setResizable(false);
      setVisible(true);
      pack();
      setLocationRelativeTo(null);
  }
  
  private void myToolBar(){
      JToolBar toolBar = new JToolBar();
      JButton cargo = new JButton(&quot;Cargo&quot;);
      JButton reception = new JButton(&quot;Reception&quot;);
      JButton robot = new JButton(&quot;Robot&quot;);
      JButton grid = new JButton(&quot;Grid&quot;);
      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);
  }
  </code></pre>
  <p>}
  '''</p>