home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-1dot1 / KeyDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.2 KB  |  111 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.*;
  19. import java.awt.event.*;
  20.  
  21. public class KeyDemo extends Applet 
  22.                      implements KeyListener,
  23.                 ActionListener {
  24.     TextArea displayArea;
  25.     TextField typingArea;
  26.  
  27.     public void init() {
  28.     Button button = new Button("Clear");
  29.     button.addActionListener(this);
  30.  
  31.     typingArea = new TextField(20);
  32.     typingArea.addKeyListener(this);
  33.  
  34.     displayArea = new TextArea(5, 20);
  35.     displayArea.setEditable(false);
  36.  
  37.     setLayout(new BorderLayout());
  38.     add("Center", displayArea);
  39.     add("North", typingArea);
  40.     add("South", button);
  41.     }
  42.  
  43.     /** Handle the key typed event from the text field. */
  44.     public void keyTyped(KeyEvent e) {
  45.     displayInfo(e, "KEY TYPED: ");
  46.     }
  47.  
  48.     /** Handle the key pressed event from the text field. */
  49.     public void keyPressed(KeyEvent e) {
  50.     displayInfo(e, "KEY PRESSED: ");
  51.     }
  52.  
  53.     /** Handle the key released event from the text field. */
  54.     public void keyReleased(KeyEvent e) {
  55.     displayInfo(e, "KEY RELEASED: ");
  56.     }
  57.  
  58.     /** Handle the button click. */
  59.     public void actionPerformed(ActionEvent e) {
  60.     //Clear the text components.
  61.     displayArea.setText("");
  62.     typingArea.setText("");
  63.  
  64.     //Return the focus to the typing area.
  65.     typingArea.requestFocus();
  66.     }
  67.  
  68.     /*
  69.      * We have to jump through some hoops to avoid
  70.      * trying to print non-printing characters 
  71.      * such as Shift.  (Not only do they not print, 
  72.      * but if you put them in a String, the characters
  73.      * afterward won't show up in the text area.)
  74.      */
  75.     protected void displayInfo(KeyEvent e, String s){
  76.     String charString, keyCodeString, modString, tmpString;
  77.  
  78.     char c = e.getKeyChar();
  79.     int keyCode = e.getKeyCode();
  80.     int modifiers = e.getModifiers();
  81.  
  82.     if (Character.isISOControl(c)) {
  83.         charString = "key character = (an unprintable control character)";
  84.     } else {
  85.         charString = "key character = '" + c + "'";
  86.     }
  87.  
  88.     keyCodeString = "key code = " + keyCode
  89.             + " ("
  90.             + KeyEvent.getKeyText(keyCode)
  91.             + ")";
  92.  
  93.     modString = "modifiers = " + modifiers;
  94.     tmpString = KeyEvent.getKeyModifiersText(modifiers);
  95.     if (tmpString.length() > 0) {
  96.         modString += " (" + tmpString + ")";
  97.     } else {
  98.         modString += " (no modifiers)";
  99.     }
  100.  
  101.     displayArea.append(s
  102.                            + "\n    "
  103.                        + charString 
  104.                        + "\n    "
  105.                    + keyCodeString
  106.                            + "\n    "
  107.                    + modString
  108.                    + "\n");
  109.     }
  110. }
  111.