public class KeyDemoFrame extends JFrame implements KeyListener
1: // Fig. 11.36: KeyDemoFrame.java
2: // Demonstrating keystroke events.
3: import java.awt.Color;
4: import java.awt.event.KeyListener;
5: import java.awt.event.KeyEvent;
6: import javax.swing.JFrame;
7: import javax.swing.JTextArea;
8:
9: public class KeyDemoFrame extends JFrame implements KeyListener
10: {
11: private String line1 = ""; // first line of textarea
12: private String line2 = ""; // second line of textarea
13: private String line3 = ""; // third line of textarea
14: private JTextArea textArea; // textarea to display output
15:
16: // KeyDemoFrame constructor
17: public KeyDemoFrame()
18: {
19: super( "Demonstrating Keystroke Events" );
20:
21: textArea = new JTextArea( 10, 15 ); // set up JTextArea
22: textArea.setText( "Press any key on the keyboard..." );
23: textArea.setEnabled( false ); // disable textarea
24: textArea.setDisabledTextColor( Color.BLACK ); // set text color
25: add( textArea ); // add textarea to JFrame
26:
27: addKeyListener( this ); // allow frame to process key events
28: } // end KeyDemoFrame constructor
29:
30: // handle press of any key
31: public void keyPressed( KeyEvent event )
32: {
33: line1 = String.format( "Key pressed: %s",
34: event.getKeyText( event.getKeyCode() ) ); // output pressed key
35: setLines2and3( event ); // set output lines two and three
36: } // end method keyPressed
37:
38: // handle release of any key
39: public void keyReleased( KeyEvent event )
40: {
41: line1 = String.format( "Key released: %s",
42: event.getKeyText( event.getKeyCode() ) ); // output released key
43: setLines2and3( event ); // set output lines two and three
44: } // end method keyReleased
45:
46: // handle press of an action key
47: public void keyTyped( KeyEvent event )
48: {
49: line1 = String.format( "Key typed: %s", event.getKeyChar() );
50: setLines2and3( event ); // set output lines two and three
51: } // end method keyTyped
52:
53: // set second and third lines of output
54: private void setLines2and3( KeyEvent event )
55: {
56: line2 = String.format( "This key is %san action key",
57: ( event.isActionKey() ? "" : "not " ) );
58:
59: String temp = event.getKeyModifiersText( event.getModifiers() );
60:
61: line3 = String.format( "Modifier keys pressed: %s",
62: ( temp.equals( "" ) ? "none" : temp ) ); // output modifiers
63:
64: textArea.setText( String.format( "%s\n%s\n%s\n",
65: line1, line2, line3 ) ); // output three lines of text
66: } // end method setLines2and3
67: } // end class KeyDemoFrame
68:
69:
70: /**************************************************************************
71: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
72: * Pearson Education, Inc. All Rights Reserved. *
73: * *
74: * DISCLAIMER: The authors and publisher of this book have used their *
75: * best efforts in preparing the book. These efforts include the *
76: * development, research, and testing of the theories and programs *
77: * to determine their effectiveness. The authors and publisher make *
78: * no warranty of any kind, expressed or implied, with regard to these *
79: * programs or to the documentation contained in these books. The authors *
80: * and publisher shall not be liable in any event for incidental or *
81: * consequential damages in connection with, or arising out of, the *
82: * furnishing, performance, or use of these programs. *
83: *************************************************************************/