1

I am writing a simple java snake-like game, and I ran into a problem even before I actually got to making the game. I can't seem to get input from the keyboard for some reason. My current code is:

public class GameWindow extends JFrame{    


private SnakeCanvas snakeCanvas;


public GameWindow(StartWindow sw) {
    getContentPane().addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            JOptionPane.showMessageDialog(null, "Key Pressed!");
        }
    });


    getContentPane().setBackground(Color.BLACK);

    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.setUndecorated(true);
    this.setVisible(true);
    getContentPane().setLayout(null);

    snakeCanvas = new SnakeCanvas();
    snakeCanvas.setBounds(78, 72, 290, 195);
    getContentPane().add(snakeCanvas);
    snakeCanvas.setVisible(true);
    snakeCanvas.repaint();

}


}

(a SnakeCanvas extends JPanel and has no other components on it)

I've tried also adding a key listener to the snakeCanvas and still no effect.. I've also tried to play with the focusable and the focus Traversal stuff but that also didn't do anything... Can anyone please explain to me what I'm doing wrong?

tenorsax
  • 21,123
  • 9
  • 60
  • 107
Bob
  • 2,586
  • 7
  • 20
  • 33

2 Answers2

3

Make sure you have set the components you want to receive keyboard events is focusable (setFocusable) & has focus (requestFocus)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2
  1. KeyListener isn't proper listener for Swing JComponents, required focus in the window

  2. you have to setFocusable for container

  3. right and correct way is usage of KeyBindings, for example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @MadProgrammer nothing against your person, you are poster with excelent knowledges, but and again with b** s***, please to stop suggesting KeyListener, works for AWT Component on Windows platform, final code workaround is always longer than quite robust code for KeyBindings :-) – mKorbel Aug 03 '12 at 10:34
  • I don't recall mentioning anything about the key listener? I agree, key bindings are generally a better solution, but one has to weigh the requirements against solution. Is it easier to write a bunch of if statements or a bunch of action classes? Given my experience with the unreliabilty of key listeners, where I can, I would personally use key bindings, but that wasn't the question. I get in trouble for expanding beyond the question I get in trouble for not :( – MadProgrammer Aug 03 '12 at 20:05
  • @MadProgrammer [aaach](http://stackoverflow.com/questions/11716785/change-the-size-of-jtextfield-on-keypress/11717326#11717326) – mKorbel Aug 03 '12 at 20:38
  • One thing that KeyBindings don't handle well (in my experience) is multiple, simultaneous key presses (or at least it would violate the domain of responsibility between Actions, unless your using a single Action for all your bindings, then what's the difference). Your "evidence" is to an unrelated question, that's completely out of context to this question. Yes, in that situation, I'd agree, KeyListener is a bad choice, in this situation I'm not so sure, in fact, if you really want to do this properly, it might be more worthwhile to look at the KeyboardFocusManager – MadProgrammer Aug 03 '12 at 20:53
  • My response to your previous answer was one of clarification, so I could learn why you'd chosen that path, not one of critism at your choice. I simply wanted to get into your head & understand your response – MadProgrammer Aug 03 '12 at 20:56
  • answer is too simple (my view) bad practicies made bad GUI ..., once time I got two downvotes for KeyListener in (editable) JComboBox'es popup, in this time I was sure that is best of Listener in compare with robust KeyBindings or low_level AWTEventListener, coins has always two sides advantage from KeyBindings that are manageable, all keyboard shotcuts are (built_in for JComponents) based on KeyBindings, then most or us don't know how to change that or stop these events – mKorbel Aug 03 '12 at 21:15