diff options
Diffstat (limited to 'projects/12/Keyboard.jack')
| -rw-r--r-- | projects/12/Keyboard.jack | 59 | 
1 files changed, 59 insertions, 0 deletions
diff --git a/projects/12/Keyboard.jack b/projects/12/Keyboard.jack index 03e1031..ceb3438 100644 --- a/projects/12/Keyboard.jack +++ b/projects/12/Keyboard.jack @@ -10,6 +10,7 @@ class Keyboard {      /** Initializes the keyboard. */
      function void init() {
 +        return;
      } 
      /**
 @@ -33,6 +34,7 @@ class Keyboard {       * F1 - F12 = 141 - 152
       */
      function char keyPressed() {
 +        return Memory.peek(24576);
      }
      /**								
 @@ -41,6 +43,17 @@ class Keyboard {       * of the pressed key.
       */
      function char readChar() {
 +        var int key, key1;
 +        let key = 0;
 +        while (key = 0) {
 +            key = Memory.peek(24576);
 +        }
 +        key1 = key;
 +        while (key1 = key) {
 +            key1 = Memory.peek(24576);
 +        }
 +        Output.printChar(key);
 +        return key;
      }
      /**								
 @@ -49,6 +62,22 @@ class Keyboard {       * and returns its value. Also handles user backspaces.
       */
      function String readLine(String message) {
 +        var int c;
 +        var String s;
 +        let s = "";
 +        Output.printString(message);
 +        let c = Keyboard.readChar();
 +        while (~(c = 128)) {
 +            if (c = 129) {
 +                if (s.length() > 0) {
 +                    do s.eraseLastChar();
 +                }
 +            } else {
 +                do s.appendChar(c);
 +            }
 +            let c = Keyboard.readChar();
 +        }
 +        return s;
      }   
      /**								
 @@ -58,5 +87,35 @@ class Keyboard {       * entered text is detected). Also handles user backspaces. 
       */
      function int readInt(String message) {
 +        var int n, i;
 +        var char t;
 +        var String s;
 +        let n = 0;
 +        let neg = false;
 +        let s = Keyboard.readLine("");
 +        let i = 0;
 +        if (s.charAt(0) = 45) {
 +            let i = 1;
 +            let neg = true;
 +        }
 +        let t = s.charAt(i);
 +        if ((t < 48) | (t > 57)) {
 +            do Sys.error(3);
 +            Output.printString("Keyboard.readInt: the input data is not number!");
 +        }
 +        let done = false;
 +        while (~done) {
 +            if ((s.charAt(i) > 47) & (s.charAt(i) < 58)) {
 +                n = n * 10 + (t - 48);
 +            } else {
 +                done = true;
 +            }
 +            let i = i + 1;
 +            done = done | (i < s.length());
 +        }
 +        if (neg) {
 +            n = - n;
 +        }
 +        return n;
      }
  }
  | 
