.
This commit is contained in:
Executable
+102
@@ -0,0 +1,102 @@
|
||||
// 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 {
|
||||
static Array mem;
|
||||
/** Initializes the keyboard. */
|
||||
function void init() {
|
||||
let mem = 0;
|
||||
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 mem[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 char a, b;
|
||||
do Output.printChar(0);
|
||||
while ((b = 0) | (a > 0)){
|
||||
let a = Keyboard.keyPressed();
|
||||
if (a > 0){
|
||||
let b = a;
|
||||
}
|
||||
}
|
||||
do Output.printChar(String.backSpace());
|
||||
do Output.printChar(b);
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 String s, b;
|
||||
var char c;
|
||||
let s = String.new(100);
|
||||
do Output.printString(message);
|
||||
while (true){
|
||||
let c = Keyboard.readChar();
|
||||
if (c = 128){
|
||||
return s;
|
||||
}
|
||||
else{
|
||||
if (c = 129){
|
||||
do s.eraseLastChar();
|
||||
}
|
||||
else{
|
||||
do s.appendChar(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 starting a;
|
||||
var int b;
|
||||
let a = Main.readLine_stolen(message);
|
||||
let b = String.intValue(a);
|
||||
do String.dispose(a);
|
||||
return b;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user