java beginer help
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]
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.
yeah - use a key listener
the one you want to implement is keyPressed.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement