"Andrew An(Tae-Yoon)" <[EMAIL PROTECTED]> writes:

> I tested a few java program,including this program.
> 
> import java.awt.*;
> import java.awt.event.*;
> import java.awt.event.KeyEvent;
> 
> class KeyEventTest{
>       public static void main(String[] args){
>               KeyEventTest tester = new KeyEventTest();
>               Frame f = new Frame();
>               
>               f.add(tester.display,"Center");
>               f.add(tester.input,"West");
>               f.setSize(400,200);
>               f.setVisible(true);
>               
>       }
>       
>       int posX = 0,posY = 0;
>       
>       String inputString = "";
>       
>       TextField input = new TextField(){
>               public Dimension getPreferredSize(){
>                       return new Dimension(0,0);
>               }
>        };
>       
>       Canvas display = new Canvas(){
>               public void paint(Graphics g){
>                       g.drawString(inputString,posX,posY);
>               }
>        };
>       
>       KeyEventTest(){
>               display.addKeyListener(kl);
>               display.addMouseListener(ml);
>               display.addFocusListener(fl);
>        }
>       
>       MouseListener ml = new MouseAdapter(){
>               public void mousePressed(MouseEvent ev){
>                       inputString = "";
>                       posX = ev.getX();
>                       posY = ev.getY();
>                       input.requestFocus();
>                       display.repaint();
>               }
>       };
>       
>       FocusListener fl = new FocusAdapter(){
>               public void focusGained(FocusEvent ev){
>                               input.requestFocus();
>                       }
>        };
>       
>       KeyListener kl = new KeyAdapter(){
>               public void KeyTyped(KeyEvent ev){

                public void keyTyped(KeyEvent ev){
                             
>                               System.out.println("in the process of
> key handling.");//debug.
>                               inputString += ev.getKeyChar();
>                               display.repaint();
>                       }
>        };
>       
>  }
>               
> key Event is never transmited to  kl.
> I use jdk1.1.5 and Linux Redhat 5.1.

kl is a KeyListener for the Canvas display, but you traverse the focus
to the (not visible) TextField input with your 'input.requestFocus()'
statements. TextField input does not invoke kl's keyTyped method because
kl belongs to display.
If you replace all occurrencies of 'input.requestFocuse' with
'display.requestFocuse' it will work.

        Jürgen.


-- 
Juergen Kreileder, Universitaet Dortmund, Lehrstuhl Informatik V
Baroper Strasse 301, D-44221 Dortmund, Germany
Phone: ++49 231/755-5806, Fax: ++49 231/755-5802

Reply via email to