By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,784 Members | 3,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,784 IT Pros & Developers. It's quick & easy.

Trouble getting Keyboard input with KeyListener

mrjohn
P: 32
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?

Expand|Select|Wrap|Line Numbers
  1. import java.awt.Dimension;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7.  
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11.  
  12. @SuppressWarnings("serial")
  13. public class lab extends JPanel implements KeyListener 
  14. {
  15.  
  16.    Image tiles;
  17.    Dimension size;
  18.  
  19.     public lab()
  20.     {
  21.         addKeyListener(this);
  22.         requestFocus();
  23.         size = new Dimension();
  24.         tiles = new ImageIcon(this.getClass().getResource("../stars.png")).getImage();
  25.         size.width = tiles.getWidth(null);
  26.         size.height = tiles.getHeight(null);
  27.         setPreferredSize(size);
  28.     }
  29.     public void paint(Graphics g) {
  30.  
  31.         Graphics2D g2d = (Graphics2D) g;
  32.  
  33.         g2d.drawImage(tiles, 0, 0, null);
  34.     }
  35.  
  36.     public void keyReleased(KeyEvent e)
  37.     {
  38.         System.out.println("TEST");
  39.         if(e.getKeyCode() == KeyEvent.VK_UP)
  40.         {
  41.             System.out.println("UP");
  42.         }
  43.         else
  44.         {
  45.             System.out.println(e.getKeyChar());
  46.         }
  47.     }
  48.  
  49.     public static void main(String[] args) {
  50.  
  51.         JFrame frame = new JFrame("Test");
  52.         frame.add(new lab());
  53.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.         frame.pack();
  55.         frame.setLocationRelativeTo(null);
  56.         frame.setVisible(true);
  57.     }
  58.     @Override
  59.     public void keyPressed(KeyEvent e)
  60.     {
  61.         System.out.println("KEY PRESSED");
  62.     }
  63.     @Override
  64.     public void keyTyped(KeyEvent e)
  65.     {
  66.         System.out.println("KEY TYPED");
  67.     }
  68. }
Nov 2 '09 #1
Share this Question
Share on Google+
2 Replies


100+
P: 228
jx2
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.setvisible(false) and textfield.requestfocus()

regards
jan
Nov 5 '09 #2

mrjohn
P: 32
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:
Expand|Select|Wrap|Line Numbers
  1. lab l = new lab();
  2. frame.addKeyListener(l);
  3. frame.add(l);
Nov 5 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.