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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

@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 :-)
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 aaach
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
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.