This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes keyPressed method is called twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "keyPressed method is called twice" Watch "keyPressed method is called twice" New topic

keyPressed method is called twice

Rami Nassar

Joined: Feb 23, 2011
Posts: 21

I override method keyPressed on my swing dialog but its called twice, can any one give me a hint?!!

* Invoked when a key has been pressed.
* See the class description for {@link KeyEvent} for a definition of
* a key pressed event.
public void keyPressed(KeyEvent ke) {
if( ke.getKeyCode() == KeyEvent.VK_F2) {
} else if( ke.getKeyCode() == KeyEvent.VK_F3) {
} else if( ke.getKeyCode() == KeyEvent.VK_F4) {
} else if( ke.getKeyCode() == KeyEvent.VK_F5) {
} else if( ke.getKeyCode() == KeyEvent.VK_F6) {
} else if( ke.getKeyCode() == KeyEvent.VK_RIGHT) {
} else if( ke.getKeyCode() == KeyEvent.VK_LEFT) {
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
have you added the listener twice?
Rami Nassar

Joined: Feb 23, 2011
Posts: 21
Michael Dunn wrote:have you added the listener twice?

OMG.. yes I added it twice by mistake.. I remove it and its working fine.. thanks alot my friend :)
I agree. Here's the link:
subject: keyPressed method is called twice
It's not a secret anymore!