+/TECS

git clone https://git.julia.blue/+/TECS

P1n3appl3Finish project 126e68fc9

v1.0
3.3 KiB114 linesraw
1/**
2 * A library for handling user input from the keyboard.
3 */
4class Keyboard {
5    static String buffer;
6
7    /** Initializes the keyboard. */
8    function void init() {
9        var int i;
10        let buffer = String.new(128);   // Max input size
11        let i = 0;
12        while(i < 128){
13            do buffer.appendChar(32);   // Fill buffer
14            let i = i + 1;
15        }
16        return;
17    }
18
19    /**
20     * Returns the character of the currently pressed key on the keyboard;
21     * if no key is currently pressed, returns 0.
22     *
23     * Recognizes all ASCII characters, as well as the following keys:
24     * new line = 128 = String.newline()
25     * backspace = 129 = String.backspace()
26     * left arrow = 130
27     * up arrow = 131
28     * right arrow = 132
29     * down arrow = 133
30     * home = 134
31     * End = 135
32     * page up = 136
33     * page down = 137
34     * insert = 138
35     * delete = 139
36     * ESC = 140
37     * F1 - F12 = 141 - 152
38     */
39    function char keyPressed() {
40        var char temp;
41        let temp = Memory.peek(24576);
42        if(temp > 152){
43            return 0;
44        }
45        return temp;
46    }
47
48    /**
49     * Waits until a key is pressed on the keyboard and released,
50     * then echoes the key to the screen, and returns the character
51     * of the pressed key.
52     */
53    function char readChar() {
54        var char key;
55        do Output.printChar(95);    //underscore
56        while(Keyboard.keyPressed() = 0){}
57        let key = Keyboard.keyPressed();
58        while(~(Keyboard.keyPressed() = 0)){}
59        do Output.backSpace();
60        if(~(key = 129)){   //backspace
61            do Output.printChar(key);
62        }
63        return key;
64    }
65
66    /**
67     * Displays the message on the screen, reads from the keyboard the entered
68     * text until a newline character is detected, echoes the text to the screen,
69     * and returns its value. Also handles user backspaces.
70     */
71    function String readLine(String message) {
72        var String result;
73        var char temp;
74        var int pos;
75        let pos = 0;
76        let temp = 129;
77        do Output.printString(message);
78        while(~(temp = 128)){   //enter
79            if(temp = 129){     //backspace
80                if(pos > 0){
81                    do Output.backSpace();
82                    let pos = pos - 1;
83                }
84            }
85            else{
86                do buffer.setCharAt(pos, temp);
87                let pos = pos + 1;
88            }
89            let temp = Keyboard.readChar();
90        }
91        let result = String.new(pos);
92        let temp = 0;
93        while(temp < pos){
94            do result.appendChar(buffer.charAt(temp));   // Copy buffer to result
95            let temp = temp + 1;
96        }
97        return result;
98    }
99
100    /**
101     * Displays the message on the screen, reads from the keyboard the entered
102     * text until a newline character is detected, echoes the text to the screen,
103     * and returns its integer value (until the first non-digit character in the
104     * entered text is detected). Also handles user backspaces.
105     */
106    function int readInt(String message) {
107        var String temp;
108        var int result;
109        let temp = Keyboard.readLine(message);
110        let result = String.intValue(temp);
111        do temp.dispose();
112        return result;
113    }
114}