1
\$\begingroup\$
public class Testing extends JPanel{

    public void Action() {
        Timer t = new Timer();
        t.schedule(new TimerTask() {

            public void run() {
                if(move2==true) {
                    switch(direction2) {
                    case 1 : x-=2;
                    break;
                    case 2 : x+=2;
                    break; 
                    case 3 : y-=2;
                    break;
                    case 4 : y+=2;
                    }
                }
                repaint();
            }


        }, 10, 20);
    }

    public void paint(Graphics g)
    {   
        g.clearRect(0, 0, 526, 690);
        g.drawLine(x, y, x, y-30);
        g.drawLine(x+1, y, x+1, y-30);
        g.drawLine(x-1, y, x-1, y-30);

    }
    static boolean move2 = false;
    static int x = 200;
    static int y = 100;
    static int direction2 = 0;
    public static void main(String[] args) {
    JFrame f = new JFrame();
    Testing t = new Testing();
    f.setVisible(true);
    f.setSize(526,690);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setFocusable(true);
    f.add(t);
    t.Action();
    f.addKeyListener(new KeyListener() {

        public void keyTyped(KeyEvent e) {

        }
        public void keyPressed(KeyEvent e) {
            System.out.println("did");
            switch(e.getKeyCode()) {
            case KeyEvent.VK_A:
                move2 = true;
                direction2 = 1;

                break;
            case KeyEvent.VK_D:

                move2 = true;
                direction2 = 2;
                break;
            case KeyEvent.VK_W:

                move2 = true;
                direction2 = 3;
                break;
            case KeyEvent.VK_S:

                move2 = true;
                direction2 = 4;
                break;
            }

        }

        public void keyReleased(KeyEvent e) {
            switch(e.getKeyCode()){
            case KeyEvent.VK_A:move2 = false;
            break;
            case KeyEvent.VK_D:move2=false;
            break;
            case KeyEvent.VK_W:move2=false;
            break;
            case KeyEvent.VK_S:move2=false;
            }
        }
    });
    }

}
\$\endgroup\$
1
\$\begingroup\$

The problem is not your code it is rather your Keyboard...

...most modern keyboards don't support enough key presses, aka as N-Key-Rollover (http://blog.controlspace.org/2010/08/n-key-rollover-what-it-is-and-how-to.html) so your event are 'swallowed'

N-key rollover relates to the ability of a keyboard to correctly recognize multiple key presses at once (in the case, the ability to press as many keys as you want)...

enter image description here

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.