Trouble getting Keyboard input with KeyListener

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrjohn
    New Member
    • May 2009
    • 31

    Trouble getting Keyboard input with KeyListener

    Hey, I'm trying to make a program that will recognize when certain keys are released, so that I can encorperate it into a game I'm making. Unfortunately, it doesn't seem to be working. When I run the program and press keys, nothing happens. Any ideas?

    [code="java"]import java.awt.Dimens ion;
    import java.awt.Graphi cs;
    import java.awt.Graphi cs2D;
    import java.awt.Image;
    import java.awt.event. KeyEvent;
    import java.awt.event. KeyListener;

    import javax.swing.Ima geIcon;
    import javax.swing.JFr ame;
    import javax.swing.JPa nel;

    @SuppressWarnin gs("serial")
    public class lab extends JPanel implements KeyListener
    {

    Image tiles;
    Dimension size;

    public lab()
    {
    addKeyListener( this);
    requestFocus();
    size = new Dimension();
    tiles = new ImageIcon(this. getClass().getR esource("../stars.png")).ge tImage();
    size.width = tiles.getWidth( null);
    size.height = tiles.getHeight (null);
    setPreferredSiz e(size);
    }
    public void paint(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    g2d.drawImage(t iles, 0, 0, null);
    }

    public void keyReleased(Key Event e)
    {
    System.out.prin tln("TEST");
    if(e.getKeyCode () == KeyEvent.VK_UP)
    {
    System.out.prin tln("UP");
    }
    else
    {
    System.out.prin tln(e.getKeyCha r());
    }
    }

    public static void main(String[] args) {

    JFrame frame = new JFrame("Test");
    frame.add(new lab());
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
    frame.pack();
    frame.setLocati onRelativeTo(nu ll);
    frame.setVisibl e(true);
    }
    @Override
    public void keyPressed(KeyE vent e)
    {
    System.out.prin tln("KEY PRESSED");
    }
    @Override
    public void keyTyped(KeyEve nt e)
    {
    System.out.prin tln("KEY TYPED");
    }
    }[/code]
  • jx2
    New Member
    • Feb 2007
    • 228

    #2
    I know its frustrating, it seems that you can not set form on focus;
    anyway
    I had the same problem and i found a simple solution(not sure if the best solution lol)
    You can use textfield and listen to his key listener!
    its simple and it works!
    u dont have to change much in fact u need to ad textfield
    set it to:
    textfield.setvi sible(false) and textfield.reque stfocus()

    regards
    jan

    Comment

    • mrjohn
      New Member
      • May 2009
      • 31

      #3
      Ah... well, after some monkeying around and looking at examples on the web, I found the solution. I'm sorry, I probably should have posted it on this thread when I found it.

      To fix it, all I had to do was replace line #52 with something like the following code:
      [CODE="java"]
      lab l = new lab();
      frame.addKeyLis tener(l);
      frame.add(l);[/CODE]

      Comment

      Working...