Jump to content
  • Advertisement
Sign in to follow this  
afarnen

java KeyListener problem [solved--mods feel free to delete post]

This topic is 3748 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

I wrote this program to test KeyListener. When I run this program, I only get the number 0 for every key I press. Additionally, it doesn't seem to work with the arrow keys. Why is it doing this? What should I do to use the arrow keys in my applet? Also, is there a way for it to detect multiple keys at once? I know it's been done before. It works in Runescape.
import java.applet.Applet;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.Graphics;

public class game extends Applet implements KeyListener
{
  String str = "";
  
  public void init()
  {
    addKeyListener(this);
  }
  
  public void start()
  {
    str = "";
  }
  
  public void stop()
  {
  }
  
  public void destroy()
  {
  }
  
  public void keyPressed(KeyEvent e)
  {
  }
  
  public void keyReleased(KeyEvent e)
  {
  }
  
  public void keyTyped(KeyEvent e)
  {
    str += Integer.toString(e.getKeyCode());
    repaint();
  }
  
  public void paint(Graphics g)
  {
    g.drawString(str, 20, 20);
  }
}

EDIT: Why is it that as soon as I'm about ready to give up, I find the solution. Sorry for such a waste of a post. The problem was that I should have been using keyPressed, instead of keyTyped. keyTyped only gives the keyChar, not the keyCode, turns out.

Share this post


Link to post
Share on other sites
Advertisement
I had this problem too. That's when I moved my code from "keyTyped" to "keyPressed" and now it works for me. That's the only recommendation I can give, sorry if I'm not much help.

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
I had this problem too. That's when I moved my code from "keyTyped" to "keyPressed" and now it works for me. That's the only recommendation I can give, sorry if I'm not much help.


Thanks, turns out that's what fixed it. I figured that out on my own right before you posted.

Share this post


Link to post
Share on other sites
1) we never delete posts unless they are duplicate of existing posts (double ou triple posts) OR they are spam.

2) I suggest you to never delete your posts too.

3) I also suggest you to not mark any post 'solved'. This is a technical forum, not a Q/A forum. Everyone is encouraged to discuss both your problem and other related problems in the same thread. We can also discuss the merits of the proposed solution, and perhaps propose a better solution. Tagging your "question" as "solved" simply means 'guys, you don't even have to come here - I don't really care what you'll say', which is pretty rude IMHO.

Now, have fun! :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!