Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

brian89

[java] key listener wont work

This topic is 5276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

why wont this keylistener work?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class keyTest extends JPanel
        implements KeyListener
{

        public static void main(String args[]) {
                JFrame frame=new JFrame("key test");
                keyTest kt=new keyTest();
                frame.getContentPane().add(kt);
                frame.setSize(500, 500);
                frame.setVisible(true);
        }
        public keyTest() {
                initKey();
        }
        public void initKey() {
                addKeyListener(this);
                System.out.println("keylistener added");
        }
        public void keyPressed(KeyEvent e) {
                System.out.println("pressed");
        }
        public void keyReleased(KeyEvent e) {
                System.out.println("released");
        }
        public void keyTyped(KeyEvent e) {
                System.out.println("typed");
        }
}

 

Share this post


Link to post
Share on other sites
Advertisement
when i press a key it doenst do anything, its supposed to print "pressed" "released" or "typed" when a key is pressed

Share this post


Link to post
Share on other sites
This works:


import javax.swing.*;
import java.awt.event.*;

public class keyTest extends JPanel implements KeyListener {

public static void main(String args[]) {
JFrame frame=new JFrame("key test");
keyTest kt=new keyTest();
frame.getContentPane().add(kt);
frame.addKeyListener(kt); //added this

frame.setSize(500, 500);
frame.setVisible(true);
}

public keyTest() {
initKey();
}

public void initKey() {
// this.addKeyListener(this);

System.out.println("keylistener added");
}

public void keyPressed(KeyEvent e) {
System.out.println("pressed");
}

public void keyReleased(KeyEvent e) {
System.out.println("released");
}

public void keyTyped(KeyEvent e) {
System.out.println("typed");
}
}



LizardCPP

[edited by - LizardCPP on June 10, 2004 4:04:17 PM]

Share this post


Link to post
Share on other sites
Well, I assume the reason was either A) the JPanel cannot recieve a keyListener or B) it cannot recieve focus to know it should be listening for keys, or C) the panel can do both, but you never focused on the panel.



capn_midnight | Captain Midnight | deviantArt
ACM | SIGGRAPH | Generation5
"I''m tired of all this nonsense about beauty being only skin-deep. That''s deep enough. What do you want - an adorable pancreas?" -Jean Kerr

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!