KeyListener isn't working?

给你一囗甜甜゛ 提交于 2019-12-11 11:05:01

问题


Ok, so I have this key listener for my game and it isn't working but when I made a new project just to test does it work at all for me it did? So here's some code of my game:

public class GameKeyListener implements KeyListener {

private void pressMainWeapon() {
     Constants.MAIN_WEAPON_PRESSED = Constants.MAIN_WEAPON_PRESSED ? true : false;
    }

    private void pressSecondaryWeapon() {
     Constants.SECONDARY_WEAPON_PRESSED = Constants.SECONDARY_WEAPON_PRESSED ? true : false;
    }

    private void pressSkillsButton() {
        Constants.SKILLS_BUTTON_PRESSED = Constants.SKILLS_BUTTON_PRESSED ? true : false;
    }

       private void gameKeyPressed(int keyCode) {

              switch (keyCode) {      

                 case KeyEvent.VK_Q:
                     pressMainWeapon();
                    break;

                 case KeyEvent.VK_E:
                     pressSecondaryWeapon();
                     break;

                 case KeyEvent.VK_TAB:
                     pressSkillsButton();
                     break;

                 case KeyEvent.VK_W:
                     Constants.CHARACTER_Y -= 5;
                     break;

                 case KeyEvent.VK_A:
                     Constants.CHARACTER_X -= 5;
                     break;

                 case KeyEvent.VK_S:
                     Constants.CHARACTER_Y += 5;
                     break;

                 case KeyEvent.VK_D:
                     Constants.CHARACTER_X += 5;
                     break;

              }
           }

    private void gameKeyReleased(int keyCode) {  

    }

    private void gameKeyTyped(char keyChar) {  

    }

    @Override
    public void keyPressed(KeyEvent e) {
        gameKeyPressed(e.getKeyCode());
        System.out.println(e.getKeyChar());
    }

    @Override
    public void keyReleased(KeyEvent e) {
        gameKeyReleased(e.getKeyCode());
    }

    @Override
    public void keyTyped(KeyEvent e) {
        gameKeyTyped(e.getKeyChar());
    }

and

public class GraphicsMain {

static JFrame f = new JFrame("Gurillia");

private static GameCanvas gc = new GameCanvas();

public static void startGraphics() throws IOException {
    f.setVisible(true);     
    f.setIgnoreRepaint(true);

    f.setContentPane(gc);
    gc.setPreferredSize(new Dimension(Constants.GAME_WIDTH, Constants.GAME_HEIGHT));        

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setResizable(false);

    f.addKeyListener(new GameKeyListener());
}

but this did work:

public class testkeylistener implements KeyListener {

@Override
public void keyPressed(KeyEvent arg0) {
    System.out.println(arg0.getKeyChar());
}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

public static void main(String args[]) {
    JFrame f = new JFrame("test");
    f.setSize(1000,1000);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.pack();
    f.addKeyListener(new testkeylistener());
}

So what is the problem?


回答1:


The problem is you work with KeyListeners while Swing was designed to use KeyBindings. The difference might be due to different focus behavior.

See for example this question where a KeyListener only occasionally worked




回答2:


Try adding the listener to the child (canvas), not the JFrame itself. The key is fired on the element which has currently focus.

JFrames almost never have focus.




回答3:


The KeyListener should be added to the canvas, and to setFocusable because required Focus



来源:https://stackoverflow.com/questions/11211472/keylistener-isnt-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!