Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Odd behaviour with key listener

  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Odd behaviour with key listener

    Hi guys

    Ok, this is my key listener:

    Java Code:
     public class move extends KeyAdapter
        {
            int pos;
            public void keyPressed (KeyEvent k)
            {
               
                if(k.getKeyCode() ==  KeyEvent.VK_UP)
                {
                   pos++;
                }
                 if (k.getKeyCode() == KeyEvent.VK_DOWN);
                {
                    pos--;
    
                }
                 System.out.println(pos);
            }
    
        }
    But the output is as follows:

    0
    0
    0
    -1
    -2
    -3

    So when I press the up arrow, the value of pos is 0, but when I press down it correctly decreases the value of pos. I'm not sure why. Anybody got any hints?

    Thanks

  2. #2
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Odd behaviour with key listener

    Ok, sorry for wasting your time. I figured it out. Wish I could mark this solved
    Java Code:
     public class move extends KeyAdapter
        {
            int pos;
            public void keyPressed (KeyEvent k)
            {
               
                if(k.getKeyCode() ==  KeyEvent.VK_UP)
                {
                   pos++;
    
                   System.out.println(pos);
                }
            else
                {
                    if(k.getKeyCode() == KeyEvent.VK_DOWN);
                {
                    pos--;
                    System.out.println(pos);
    
                }
                    
                }
                            
            }
        }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Odd behaviour with key listener

    That's not the solution: check your line #15; that semicolon doesn't belong there.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Odd behaviour with key listener

    Quote Originally Posted by JosAH View Post
    That's not the solution: check your line #15; that semicolon doesn't belong there.

    kind regards,

    Jos
    Gees, thanks a lot JosAH. I never noticed that. Thank you.

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  2. StringWorker different behaviour
    By _SAS in forum Threads and Synchronization
    Replies: 5
    Last Post: 08-06-2011, 09:18 PM
  3. Strange behaviour
    By imadabh in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-11-2011, 03:31 PM
  4. Strange JVM behaviour
    By pjpr in forum Advanced Java
    Replies: 13
    Last Post: 01-03-2011, 07:39 PM
  5. BoxLayout Behaviour
    By PetalumaBoy in forum AWT / Swing
    Replies: 4
    Last Post: 06-10-2009, 01:27 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
  •