Results 1 to 4 of 4
  1. #1
    briman0094 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Random keyReleased() in KeyListener

    I'm making a game in Java, and in the main code, i have a class that extends JComponent, and I add a custom KeyListener to it which passes all top-level key events to all of the entities that are active in the game. It works fine for the most part; all of the entities receive and respond correctly to the key events. The only problem is that sometimes I will get random keyReleased events followed directly by keyPressed events, almost like the component loses communication with the keyboard for a split second. This happens mostly with the Space key and the Left key, although it will occasionally happen with the Right key also. Any help is appreciated.

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    Suggest you post an SSCCE that demonstrates the problem

  3. #3
    briman0094 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    class RunCode implements Runnable
    {
        public void run()
        {
            JFrame frame = new JFrame("Game");
            CustomComponent gc = new CustomComponent();
            gc.addKeyListener(new GameListener());
            frame.add(gc);
            frame.setVisible(true);
        }
    }
    
    class GameListener implements ActionListener
    {
        public void keyTyped(KeyEvent e)
            {
                //
            }
    
            public void keyPressed(KeyEvent e)
            {
                if (!sending1)
                {
                    sending1 = true;
                    //System.out.println("Key pressed: " + e.getKeyText(e.getKeyCode()));
                    synchronized (en)
                    {
                        for (Entity i : en)
                        {
                            //System.out.println("Passing " + e.toString() + " to " + i.toString());
                            i.keyPressed(e);
                        }
    
                        if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
                        {
                            System.exit(0);
                        }
                    }
                    sending1 = false;
                }
            }
    
            public void keyReleased(KeyEvent e)
            {
                if (!sending2)
                {
                    sending2 = true;
                    //System.out.println("Key pressed: " + e.getKeyText(e.getKeyCode()));
                    synchronized (en)
                    {
                        for (Entity i : en)
                        {
                            //System.out.println("Passing " + e.toString() + " to " + i.toString());
                            i.keyReleased(e);
                        }
                    }
                    sending2 = false;
                }
            }
    }
    Where en is an ArrayList of type Entity
    and sending1 and sending2 are to prevent sending two commands at the same time (to prevent lag).
    Last edited by briman0094; 03-21-2011 at 10:44 PM.

  4. #4
    briman0094 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    SOLVED. Found out this is something with Linux and Java; linux passes a keyRelease() and keyPress() when it does the hold-repeat keyboard thing. Turning this off in System Preferences fixes the bug.

Similar Threads

  1. Replies: 4
    Last Post: 02-19-2011, 04:32 AM
  2. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 10:46 PM
  3. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •