Jump to content
  • Advertisement
Sign in to follow this  
wakedup

java beginer help

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

im trying to make a turtle graphics program that allows the user to press the arrow keys on the num pad by means of a keyboard reader from terminal io and that key press is translated into a line in that direction. for instance 8 would draw a line upward and 4 would draw a line downward. everything works without syntax errors but the only way i know how to do it is if the user inputs 8 then enter. is there a way to code so that the instant the user presses a number on the numpad the line is drawn without having to press enter? [Edited by - wakedup on September 27, 2004 8:22:05 PM]

Share this post


Link to post
Share on other sites
Advertisement
Why do you need console input if you have graphical output? I would just use Java.AWT.KeyListener, but otherwise I believe the way to to it from console is to use Java.Io.Reader and call ready() and only call read() if ready is true.

Share this post


Link to post
Share on other sites
yeah - use a key listener

public class Myclass implements KeyListener
/* on some compilers its java.awt.KeyListener */
{
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
char key = e.getKeyChar();
/* do something with that */
}
}





the one you want to implement is keyPressed.

Share this post


Link to post
Share on other sites
Quote:
Original post by stormrunner
public class Myclass implements KeyListener
/* on some compilers its java.awt.KeyListener */


That shouldn't depend on the *compiler*. It may make a difference whether you're making an applet or a full application, though. And it will definitely have to be given the full label if you define your own KeyListener class for whatever reason.

And once you have created a KeyListener, you will need to register it with your main frame/panel/component/wtf-ever they're using in the latest version of Swing. That class will have a method (from the class it extends) with a name like addKeyListener(KeyListener k), which you will call (passing an instance of Myclass).

I think there are also 'adapter' classes provided that will save you from having to type the blank KeyTyped/KeyReleased method bodies.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!