Search code examples
javaswingkey-bindings

Trouble with Key Bindings


I am creating a Pong clone, and I am trying to use KeyBindings. This works:

getInputMap().put(KeyStroke.getKeyStroke("F2"),"leftup");
        getActionMap().put("leftup", new AbstractAction() {
            private static final long serialVersionUID = -7625435800213244316L;

            public void actionPerformed(ActionEvent e) {
                System.out.println("Yay");
            }
        });

But not this:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, Event.ACTION_EVENT),"leftup");
        getActionMap().put("leftup", new AbstractAction() {
            private static final long serialVersionUID = -7625435800213244316L;

            public void actionPerformed(ActionEvent e) {
                System.out.println("Yay");
            }
        });

Solution

  • According to the Java docs, there's no overload of getKeyStroke that fits KeyEvent, Event.

    Pretty sure

    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, Event.ACTION_EVENT),"leftup");
    

    should be

    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W),"leftup");