TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ROSE
from: JARI LAAKSONEN
date: 1997-11-09 20:59:00
subject: Java

>>> Ok, in J++, how exactly do I get a simple program to
>>> determine which key has been pressed WITHOUT having to press the
>>> enter key each time?
>> java.awt.Component.keyDown() (JDK 1.0.2) or
>> java.awt.Component.processKeyEvent() (JDK 1.1)
> thanks, but can you show me how to use it in source... I'm not that
> good at it yet.,
Those events are of course sent to a component (Window, Frame etc.). If you 
are looking for getch() equivalent for Java to be used in text mode programs, 
then I'm sorry, I cannot help. I'm new at this, too :-)
However, here's a sample applet:
import java.applet.*;
import java.awt.*;
public class SimpleCard extends Applet
{
    Panel      card_stack;
    CardLayout cl;
    public void init()
    {
        setLayout (new BorderLayout());
        card_stack = new Panel();
        cl = new CardLayout();
        card_stack.setLayout (cl);
        add ("Center", card_stack);
        Panel card1 = new Panel();
        card1.setLayout (new FlowLayout());
        card1.add (new Label ("Card 1"));
        card1.add (new Button ("One"));
        card_stack.add ("One", card1);
        Panel card2 = new Panel();
        card2.setLayout (new FlowLayout());
        card2.add (new Label ("Card 2"));
        card2.add (new TextField ("Two", 30));
        card_stack.add ("Two", card2);
        Panel card3 = new Panel();
        card3.setLayout (new FlowLayout());
        card3.add (new Label ("Card 3"));
        card3.add (new Checkbox ("Three"));
        card_stack.add ("Three", card3);
        Panel button_bar = new Panel();
        button_bar.setLayout (new FlowLayout());
        button_bar.add (new Button ("First"));
        button_bar.add (new Button ("Previous"));
        button_bar.add (new Button ("Next"));
        button_bar.add (new Button ("Last"));
        add ("South", button_bar);
    }
    public boolean action (Event evt, Object arg)
    {
        if ("First".equals (arg))
        {
            cl.first (card_stack);
        }
        else if ("Previous".equals (arg))
        {
            cl.previous (card_stack);
        }
        else if ("Next".equals (arg))
        {
            cl.next (card_stack);
        }
        else if ("Last".equals (arg))
        {
            cl.last (card_stack);
        }
        return true;
    }
    public boolean keyDown (Event evt, int key)
    {
        if (key == 'f')
        {
            cl.first (card_stack);
        }
        else if (key == Event.LEFT)
        {
            cl.previous (card_stack);
        }
        else if (key == Event.RIGHT)
        {
            cl.next (card_stack);
        }
        else if (key == 'l')
        {
            cl.last (card_stack);
        }
        return true;
    }
}
        // Albert                      email: jla@to.icl.fi
--- GoldED/2 2.50+
---------------
* Origin: Albert's Point/2 in Finland, Europe (2:221/360.20)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.