/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.swing.*; 

2. import javax.swing.text.*; 

3. import java.awt.*; 

4. import java.awt.event.*; 

5.  

6. public class KeysSw 

7.     extends JApplet 

8.       implements KeyListener 

9.

10.   JTextField tf ; 

11.   JTextArea ta, ta2 ; 

12.   JScrollPane sp, sp2 ; 

13.  

14.   public void keyTyped ( KeyEvent ev ) 

15.   { 

16.     ta.append ( ev + "\n" ) ; 

17.  

18.     char ch =  ev.getKeyChar() ; 

19.      if ( Character.isDigit( ch ) ) 

20.     { 

21.       ev.consume(); 

22.       return

23.     } 

24.     ta2.append ( "Character typed : " + ch + "\n" ) ; 

25.   } 

26.  

27.   public void keyPressed ( KeyEvent ev ) 

28.   { 

29.     ta.append ( ev  + "\n" ) ; 

30.     ta2.append ( "================\n" ) ; 

31.  

32.     int cd =  ev.getKeyCode() ; 

33.  

34.     if ( cd == KeyEvent.VK_F1

35.       ta2.append ( "Function key 1 pressed\n" ) ; 

36.  

37.     if ( cd == KeyEvent.VK_HOME

38.       ta2.append ( "Home key pressed\n" ) ; 

39.  

40.     ta2.append ( "Key code : " + cd + "\t" ) ; 

41.     ta2.append ( "Control key down ? : " 

42.                + ev.isControlDown() +  "\n"  ) ; 

43.     ta2.append ( "Alt key down ? : " 

44.                + ev.isAltDown() +  "\n"  ) ; 

45.   } 

46.  

47.   public void keyReleased ( KeyEvent ev ) 

48.   { 

49.     ta.append ( ev  + "\n\n" ) ; 

50.   } 

51.  

52.   public void init() 

53.   { 

54.     Container cp = getContentPane(); 

55.     cp.setLayout( new FlowLayout() ); 

56.     cp.add ( tf ) ; 

57.     cp.add ( sp ) ; 

58.     cp.add ( sp2 ) ; 

59.     Font f = ta.getFont(); 

60.     Font nw = 

61.      new Font( "Monospaced", Font.PLAIN, f.getSize() ); 

62.     ta.setFont( nw ); 

63.     tf.addKeyListener( this ); 

64.     tf.requestFocus(); 

65.   } 

66.   public KeysSw() 

67.   { 

68.     tf = new JTextField ( 20 ); 

69.     ta = new JTextArea ( 10,65 ) ; 

70.     sp = new JScrollPane ( ta ) ; 

71.     ta2 = new JTextArea ( 15,30 ) ; 

72.     sp2 = new JScrollPane ( ta2 ) ; 

73.   } 

74.

75.