🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #3180

Re: Help: No respond to my Keypress Action

#3180
From: Dean Henkel
Date: Mon, 13 Apr 1998 00:00
93 lines
3031 bytes
liubin I tried your code, seems to work.   Here is your example with
necessary modifications to compile it.

import java.awt.*;
import java.awt.event.*;
public class mainFrame extends Frame {
  public mainFrame() {//constructor;
      //{{REGISTER_LISTENERS
      //super();
          SymKey aSymKey = new SymKey();
          this.addKeyListener(aSymKey);
         //}}
    }
    class SymKey extends java.awt.event.KeyAdapter
     {
      public void keyPressed(java.awt.event.KeyEvent event)
      {
           Object object = event.getSource();
           if (object == mainFrame.this)
            mainFrame_KeyPress(event);
      }
     }

     void mainFrame_KeyPress(java.awt.event.KeyEvent event)
     {
          // to do: code goes here.
          int nKeyCode = event.getKeyCode();
          String sKeyText = event.getKeyText(nKeyCode);
          System.out.println("Key Press : " + sKeyText);
     }

     public static void main(String s[]) {
  Frame frame = new mainFrame ();
  frame.addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent e) {System.exit(0);}
     });

  frame.setSize(WIDTH, HEIGHT);
  frame.setVisible(true);

     }
}


Dean

liubin wrote:

> Hi, everyone,
>
>     I met a problem with keypress respond. See the code below:
>
> public class mainFrame extends Frame {
>     ...
>     public mainFrame() {//constructor;
>         ...
>         //{{REGISTER_LISTENERS
>           SymKey aSymKey = new SymKey();
>           this.addKeyListener(aSymKey);
>          //}}
>     }
>     ...
>     class SymKey extends java.awt.event.KeyAdapter
>      {
>       public void keyPressed(java.awt.event.KeyEvent event)
>       {
>            Object object = event.getSource();
>            if (object == mainFrame.this)
>             mainFrame_KeyPress(event);
>       }
>      }
>
>      void mainFrame_KeyPress(java.awt.event.KeyEvent event)
>      {
>           // to do: code goes here.
>           int nKeyCode = event.getKeyCode();
>           String sKeyText = event.getKeyText(nKeyCode);
>           System.out.println("Key Press : " + sKeyText);
>      }
> }
> // end of All.
>
>  Then, I press some key, just 'A','B', 'C', but there is no respond at
> all !
> No output at all !
>
>   Are there some mistakes in my code ?  If the class Frame and its
> subclass can not respond to key actions ?
>
>     Somebody help !
>
>   Thanks.
Technical Details
Message-ID: <35323566.D2EB3985@us.ibm.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <352D80F1.E8F9B206@sky.whnet.edu.cn>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,45d5dc9d1c5edba9
X-Google-Attributes: gid1089d0,public
From: Dean Henkel <henkel@us.ibm.com>
Subject: Re: Help: No respond to my Keypress Action
Date: 1998/04/13
Message-ID: <35323566.D2EB3985@us.ibm.com>
X-Deja-AN: 343623856
Content-Transfer-Encoding: 7bit
References: <352D80F1.E8F9B206@sky.whnet.edu.cn>
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api