aboutsummaryrefslogtreecommitdiff
path: root/jackos/Keyboard.jack
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2018-01-20 15:41:49 +0100
committerYuchen Pei <me@ypei.me>2018-01-20 15:41:49 +0100
commitd3a0cc3a8ba6dfeb64d3faeffdeb6845b60e5840 (patch)
treed58df9ec2480e2a9ec6240f9c797f83d1a0b1056 /jackos/Keyboard.jack
parent3571f998b28fbc8d9250ba04c983935f10a16c15 (diff)
rearranged the dir for github
- removed tools and pdfs - rearranged the projects dirs - added md files - other minor changes
Diffstat (limited to 'jackos/Keyboard.jack')
-rw-r--r--jackos/Keyboard.jack97
1 files changed, 97 insertions, 0 deletions
diff --git a/jackos/Keyboard.jack b/jackos/Keyboard.jack
new file mode 100644
index 0000000..a35704a
--- /dev/null
+++ b/jackos/Keyboard.jack
@@ -0,0 +1,97 @@
+// This file is part of www.nand2tetris.org
+// and the book "The Elements of Computing Systems"
+// by Nisan and Schocken, MIT Press.
+// File name: projects/12/Keyboard.jack
+
+/**
+ * A library for handling user input from the keyboard.
+ */
+class Keyboard {
+
+ /** Initializes the keyboard. */
+ function void init() {
+ return;
+ }
+
+ /**
+ * Returns the character of the currently pressed key on the keyboard;
+ * if no key is currently pressed, returns 0.
+ *
+ * Recognizes all ASCII characters, as well as the following keys:
+ * new line = 128 = String.newline()
+ * backspace = 129 = String.backspace()
+ * left arrow = 130
+ * up arrow = 131
+ * right arrow = 132
+ * down arrow = 133
+ * home = 134
+ * End = 135
+ * page up = 136
+ * page down = 137
+ * insert = 138
+ * delete = 139
+ * ESC = 140
+ * F1 - F12 = 141 - 152
+ */
+ function char keyPressed() {
+ return Memory.peek(24576);
+ }
+
+ /**
+ * Waits until a key is pressed on the keyboard and released,
+ * then echoes the key to the screen, and returns the character
+ * of the pressed key.
+ */
+ function char readChar() {
+ var int key, key1;
+ let key = 0;
+ while (key = 0) {
+ let key = Memory.peek(24576);
+ }
+ let key1 = key;
+ while (key1 = key) {
+ let key1 = Memory.peek(24576);
+ }
+ if ((key > 31) & (key < 127) | (key = 128) | (key = 129)) {
+ do Output.printChar(key);
+ }
+ return key;
+ }
+
+ /**
+ * Displays the message on the screen, reads from the keyboard the entered
+ * text until a newline character is detected, echoes the text to the screen,
+ * and returns its value. Also handles user backspaces.
+ */
+ function String readLine(String message) {
+ var int c;
+ var String s;
+ let s = String.new(140);
+ do 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;
+ }
+
+ /**
+ * Displays the message on the screen, reads from the keyboard the entered
+ * text until a newline character is detected, echoes the text to the screen,
+ * and returns its integer value (until the first non-digit character in the
+ * entered text is detected). Also handles user backspaces.
+ */
+ function int readInt(String message) {
+ var String s;
+ do Output.printString(message);
+ let s = Keyboard.readLine("");
+ return s.intValue();
+ }
+}