Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

KeyListener in Controller

843806Jun 9 2008 — edited Jun 10 2008
Hellow!

I am dealing with keyboard listener in MVC pattern.

I would like to make a controller with ActionListner and KeyListener.
public class ArtillaryTankController implements ActionListener, KeyListener {

    public void actionPerformed( ActionEvent e) {
        ....
    }

    public void keyPressed(KeyEvent e) {
	System.out.println("Key Pressed");

	if (e.getKeyCode() == KeyEvent.VK_KP_LEFT)
		System.out.println("Left_Arrow_Key Pressed");

	if (e.getKeyCode() == KeyEvent.VK_KP_RIGHT)
		System.out.println("Right_Arrow_Key Pressed");		
    }
	
    public void keyTyped(KeyEvent e) {
	System.out.println("Key Typed");	
    }
	
    public void keyReleased(KeyEvent e) {
        System.out.println("Key Released");
    } 

}

public class ArtillaryTankWorldPanel extends JPanel {
    ....
}

public class ArtillaryTankFrame extends JFrame {
    ....
    _artillaryTankWorldPanel =new ArtillaryTankWorldPanel(_frameWidth, _frameHeight);
      
    final ArtillaryTankController controller = new ArtillaryTankController(_artillaryTankWorldPanel);
    ....

    _artillaryTankWorldPanel.addKeyListener(controller);

    ....
}
The ActionListeners works fine, but the KeyListener does not work. I can't see any message.

Please, guide me to find the right way.

Thanks for reading.
Sincerely yours
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 8 2008
Added on Jun 9 2008
2 comments
757 views