Results 1 to 4 of 4
Thread: Odd behaviour with key listener
- 02-24-2012, 08:04 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Odd behaviour with key listener
Hi guys
Ok, this is my key listener:
But the output is as follows: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); } }
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
- 02-24-2012, 08:21 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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); } } } }
- 02-25-2012, 02:30 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Odd behaviour with key listener
That's not the solution: check your line #15; that semicolon doesn't belong there.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-25-2012, 03:00 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Similar Threads
-
Action-Listener with multiple Arguments? (Button Listener, specifically)
By Kevinw778 in forum AWT / SwingReplies: 2Last Post: 12-11-2011, 10:44 PM -
StringWorker different behaviour
By _SAS in forum Threads and SynchronizationReplies: 5Last Post: 08-06-2011, 09:18 PM -
Strange behaviour
By imadabh in forum Threads and SynchronizationReplies: 1Last Post: 05-11-2011, 03:31 PM -
Strange JVM behaviour
By pjpr in forum Advanced JavaReplies: 13Last Post: 01-03-2011, 07:39 PM -
BoxLayout Behaviour
By PetalumaBoy in forum AWT / SwingReplies: 4Last Post: 06-10-2009, 01:27 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks