Home>

The event source for keyboard events is generally related to the component,When a component is active,Keyboard events occur when a key on the keyboard is pressed, released, or struck.The interface for keyboard events is keylistener, and the method for registering a keyboard event monitor is addkeylistener (monitor). There are three keylistener interfaces:

keypressed (keyevent e):a key on the keyboard was pressed; keyreleased (keyevent e):a key on the keyboard was pressed,Release again keytyped (keyevent e):A combination of keypressed and keyreleased methods.

The class that manages keyboard events is keyevent, which provides methods:

public int getkeycode (), get the key code of the push,The key code table is defined in the keyevent class.

[Example] The applet has a button and a text area.Button as an event source for keyboard events,And monitor it.When the program runs,First click the button.Let the button activate.When entering English letters later,The entered letters are displayed in the text area.When the letters are displayed,The letters are separated by spaces.When it is over 10 letters,Newline display.

import java.applet. *
import java.awt. *;
import java.awt.event. *;
public class example6_10 extends applet implements keylistener {
  int count=0;
  button button=new button ();
  textarea text=new textarea (5,20);
  public void init () {
    button.addkeylistener (this);
    add (button);add (text);
  }
  public void keypressed (keyevent e) {
    int t=e.getkeycode ();
    if (t>= keyevent.vk_a&&t<= keyevent.vk_z) {
      text.append ((char) t + "");
      count ++;
      if (count%10 == 0)
        text.append ("\ n");
    }
  }
  public void keytyped (keyevent e) ()
  public void keyreleased (keyevent e) ()
}
  • Previous SQL server 2008 database optimization commonly used scripts
  • Next C and C ++ mixed programming issues