/** * A library for handling user input from the keyboard. */ class Keyboard { static String buffer; /** Initializes the keyboard. */ function void init() { var int i; let buffer = String.new(128); // Max input size let i = 0; while(i < 128){ do buffer.appendChar(32); // Fill buffer let i = i + 1; } 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() { var char temp; let temp = Memory.peek(24576); if(temp > 152){ return 0; } return temp; } /** * 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 key; do Output.printChar(95); //underscore while(Keyboard.keyPressed() = 0){} let key = Keyboard.keyPressed(); while(Keyboard.keyPressed() ~= 0){} do Output.backSpace(); if(key ~= 129){ //backspace 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 String result; var char temp; var int pos; let pos = 0; let temp = 129; do Output.printString(message); while(temp ~= 128){ //enter if(temp = 129){ //backspace if(pos > 0){ do Output.backSpace(); let pos = pos - 1; } } else{ do buffer.setCharAt(pos, temp); let pos = pos + 1; } let temp = Keyboard.readChar(); } let result = String.new(pos); let temp = 0; while(temp < pos){ do result.appendChar(buffer.charAt(temp)); // Copy buffer to result let temp = temp + 1; } return result; } /** * 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 temp; var int result; let temp = Keyboard.readLine(message); let result = String.intValue(temp); do temp.dispose(); return result; } }