text stringlengths 0 897 |
|---|
while true // press Control-C to exit |
k = key.get |
print "char(" + k.code + "): " + k |
end while |
``` |
{pageBreak} |
D> You probably noticed that the Mini Micro code editor indents lines for you. You don't need to press the Tab key within the `while` loop, and you don't need to press the Delete key before typing `end while` — in fact if you do, it deletes the line break and puts you back on the previous line. Just type `end while` ... |
D> Pro tip: you can also close any open `while`, `for`, or `if` block automatically by holding the Shift key when you press Return or Enter at the end of a line! This inserts a blank line and the block closer, then positions the cursor on the blank line, ready to add code to the body of the loop. |
Again press the Close button (or hit Control-W) to exit the editor, and `run` the program. Now try various keys on the keyboard. It should print out the character code, as well as the printable character, if any. You may notice that when you press the Return key, it reports as `char(13)`, and when we print that we g... |
This little program is something you might want to run again later, when you're trying to remember exactly what `key.get` returns for some special key. So you'll want to save it, so you can reload it later. Time to get organized! |
## Organizing Your Files |
If you enter `pwd` right now, you'll probably find that your current working directory is `/usr/`, the root directory of the user disk. You certainly could just save your program here, but as I hope you will be creating lots and lots of programs in the future, that could get pretty cluttered. It's generally a good id... |
{i:"`makedir`;`save`"} |
```terminal |
]cd |
]file.makedir "learnToCode" |
]cd "learnToCode" |
]save "keyGet" |
7 lines saved to /usr/learnToCode/keyGet.ms |
``` |
That `cd` command on line 1 isn't really necessary if you're already in the `/usr/` directory, but it doesn't hurt; without any parameter, `cd` always returns you there. Then the `file.makedir` command creates a new directory, and the next `cd` on line 3 changes your current directory to that one. Finally, the `save`... |
Let's just verify that it really is there. |
```terminal |
]dir |
/usr/learnToCode : |
keyGet.ms 152 2020-07-18 14:07:18 |
]view "keyGet.ms" |
// What exactly does key.get return? |
print "Press any key, or Control-C to exit." |
while true |
k = key.get |
print "char(" + k.code + "): " + k |
end while |
``` |
{i:"`view`"} |
Pretty neat, right? The `dir` command, without any parameters, lists the contents of the current directory, along with the size and date/time of each file. The `view` command can list a program, to verify what's there. (It can also preview images and sounds, but we'll get to those later!) |
Notice that a program on disk and a program in memory are not the same thing. You saw this yesterday; to run the demos you had to first `load` the program into memory, and then `run` it. To create a new program on disk is basically the same thing in reverse: first you `edit` to create the program, and then `save` it. |
So we used the `view` command to list a program on disk. How do you list the program in memory? Of course you can `edit` it and see it that way, but you can also use the `source` command. |
```terminal |
]source |
// What exactly does key.get return? |
print "Press any key, or Control-C to exit." |
while true |
k = key.get |
print "char(" + k.code + "): " + k |
end while |
``` |
There's our program, still in memory! |
D> The `source` and `edit` commands both operate on the program in memory. The `view` command always displays a file on disk. |
But we're done with the keyGet program for now. Let's clear it out of memory using the `reset` command. |
{i:"`reset`"} |
```terminal |
]reset |
Program source cleared and reset |
``` |
If you try `source` now, it doesn't print anything; our current program is empty. Likewise, when you `edit`, you'll be in a blank editor. Do that, and type in this new program: |
{caption:"Keyboard Dash"} |
```miniscript |
print "Keyboard Dash!" |
print "Press the , and . keys as fast as you can" |
print "to race to the finish line." |
print |
print "Ready..."; wait |
print "Set..."; wait |
print "GO!" |
lastKey = "" |
count = 0 |
startTime = time |
while count < 25 |
k = key.get |
if k == lastKey then continue |
if k != "." and k != "," then continue |
count = count + 1 |
print count |
end while |
elapsed = time - startTime |
print "DONE!" |
print "You finished in " + elapsed + " seconds. Great job!" |
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.