/* 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 KeyMapping 

7.     extends JApplet 

8.

9.   JTextField tf ; 

10.   JTextArea ta ; 

11.   JScrollPane sp ; 

12.  

13.    

14.   public void init() 

15.   { 

16.     Keymap km = 

17.      tf.addKeymap( "SEMMsKeys",  tf.getKeymap() ); 

18.     KeyStroke key = 

19.       KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ); 

20.     km.addActionForKeyStroke( key, new Erase() ); 

21.     key = KeyStroke.getKeyStroke 

22.       ( KeyEvent.VK_ENTER, InputEvent.CTRL_MASK ); 

23.     km.addActionForKeyStroke( key, new Add() ); 

24.  

25.     tf.setKeymap( km ); 

26.  

27.     Container cp = getContentPane(); 

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

29.     cp.add ( tf ) ; 

30.     cp.add ( sp ) ; 

31.     tf.requestFocus (); 

32.   } 

33.  

34.   class Add extends AbstractAction 

35.   { 

36.     public void actionPerformed( ActionEvent ev ) 

37.     { 

38.       ta.append( tf.getText() + "\n" ); 

39.       tf.setText( "" ); 

40.     } 

41.     public Add() 

42.     { 

43.       super("semms-add-action"); 

44.     } 

45.   } 

46.   class Erase extends TextAction 

47.   { 

48.     public void actionPerformed( ActionEvent ev ) 

49.     { 

50.       getTextComponent( ev ).setText( "" ); 

51.       Toolkit.getDefaultToolkit().beep(); 

52.     } 

53.     public Erase() 

54.     { 

55.       super("semms-erase-action"); 

56.     } 

57.   } 

58.  

59.   public KeyMapping() 

60.   { 

61.     tf = new JTextField ( 20 ); 

62.     ta = new JTextArea ( 10,40 ) ; 

63.     sp = new JScrollPane ( ta ) ; 

64.   } 

65.

66.