KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtKeyboard


1
2 package Jt;
3 import java.util.*;
4 import java.lang.reflect.*;
5 import java.beans.*;
6 import java.io.*;
7
8 /**
9   * Class used to receive input from
10   * the keyboard.
11   */

12
13
14 public class JtKeyboard extends JtObject {
15
16   String JavaDoc command = null;
17   Process JavaDoc process = null;
18   int status = 0;
19   String JavaDoc stdout;
20   String JavaDoc line = null;
21
22   private byte readln ()[] throws IOException {
23   byte buffer[] ;
24   int i;
25   byte ch;
26
27     buffer = new byte[1024];
28     buffer[0] = '\0';
29
30     i = 0;
31     while ((ch = (byte) System.in.read ()) >= 0) {
32         if (i >= 1024)
33         break;
34         buffer[i++] = ch;
35         if (ch == '\n')
36         break;
37     }
38         if (i == 0)
39           line = null;
40         else
41           line = new String JavaDoc (buffer, 0, i);
42         //System.out.println ("Line:" + i + ":" + line);
43
return buffer;
44
45   }
46
47
48
49   // activate: activate
50

51   void activate () {
52   byte buffer[];
53
54     try {
55       buffer = readln ();
56     } catch (IOException ex) {
57       handleException (ex);
58       //line = null;
59
return;
60     }
61     //line = new String (buffer);
62
//System.out.println ("Line:" + line);
63
}
64
65
66   /**
67    * Process object messages.
68    * <ul>
69    * <li> JtACTIVATE - Read input from the keyboard
70    * </ul>
71    */

72    
73   public Object JavaDoc processMessage (Object JavaDoc message) {
74
75    String JavaDoc msgid = null;
76    byte buffer[];
77    File file;
78    JtMessage e = (JtMessage) message;
79
80      if (e == null)
81     return null;
82
83      msgid = (String JavaDoc) e.getMsgId ();
84
85      if (msgid == null)
86     return null;
87
88      // Remove this object
89
if (msgid.equals ("JtREMOVE")) {
90        return (null);
91      }
92
93      if (msgid.equals ("JtACTIVATE")) {
94     activate ();
95     return (line);
96      }
97      handleError ("JtKeyboard.processMessage: invalid message id:" + msgid);
98
99      return null;
100   }
101
102
103   /**
104    * Unit tests the messages processed by JtKeyBoard.
105    */

106
107   public static void main(String JavaDoc[] args) {
108
109     JtObject main = new JtObject ();
110     JtMessage msg;
111     String JavaDoc oline;
112
113  
114     
115     // main.setObjTrace (1);
116

117     main.createObject ("Jt.JtKeyboard", "keyboard");
118     msg = new JtMessage ("JtACTIVATE");
119     System.out.println ("Press any key to continue ...");
120     oline = (String JavaDoc) main.sendMessage ("keyboard", msg);
121     System.out.println ("Keyboard input:" + oline);
122     main.removeObject ("keyboard");
123
124     
125  
126   }
127 }
128
129
130
Popular Tags