text stringlengths 0 897 |
|---|
{i:"programming language"} |
MiniScript is a programming *language*. It has a certain syntax: a way of typing in numbers, strings, loops, branches, and so on so that the computer can understand them. It has a system of data types (strings, numbers, lists, maps, and functions), rules about how it looks up identifiers, and so on. |
{i:"programming environment"} |
Mini Micro is a programming *environment*. The environment provides a context for using the language. You've used MiniScript in at least two other environments already: the Try-It! page on the web, and command-line MiniScript. It's possible you've encountered (or will someday encounter) MiniScript in other contexts ... |
You already saw all this when you moved from the Try-It! page to command-line MiniScript; only the latter, for example, can access files. But Mini Micro takes this much further. As you explored the demos, you saw all sorts of fancy things going on: animated graphics and text, sound, music, etc. But, all of that is d... |
{pageBreak} |
A> **Chapter Review** |
A> - You installed Mini Micro on your own computer. |
A> - You used Mini Micro commands to load, run, edit, and save programs. |
A> - You explored the nifty demos that come with Mini Micro, to get a good idea of what it can do. |
A> - You learned about the various places you can find helpful information about Mini Micro. |
A> - You reviewed the difference between programming language and programming environment. |
{gap:100} |
{width:"25%"} |
 |
{chapterHead: "Day 16: Key Module; MiniScript Files", startingPageNum:187} |
{width: "50%"} |
 |
Q> Experience is the name everyone gives to their mistakes. |
Q>— Oscar Wilde (poet and playwright, 1854-1900) |
A> **Chapter Objectives** |
A> - Practice editing, saving, and managing your Mini Micro programs. |
A> - Learn how to use the `key` module to interact more directly with the keyboard. |
A> - Discover different ways to access files on the host computer from Mini Micro, and vice versa. |
Fire up Mini Micro. Today's going to be a fun day! |
## The `key` module |
Before today, the only way of getting input from the user has been with the `input` function. That displays a prompt, then waits for the user to type as much as they like, until they press the Return/Enter key; then it returns what they typed as a string. That's fine for many kinds of input, but it has limitations: |
- No way to enter special keys like Shift, Control, Escape, or F1. |
- No way for the computer to react to a single keypress (other than Return). |
- No way to tell if the user is currently pressing a key. |
Those limitations are difficult to avoid in general-purpose enviroments like the Try-It! page and command-line MiniScript. But Mini Micro, being a virtual computer, can do much more! With that preamble we introduce `key`, a module of methods for reading the keyboard. |
{i:"Mini Micro, `key` module;`key` module;keyboard"} |
{caption:"Methods in Mini Micro's `key` module."} |
| `key.available` | returns 1 if a key is in the buffer, 0 if not | |
| `key.get` | return a key from the key buffer, or the next key pressed | |
| `key.clear` | clear the key buffer | |
| `key.pressed(k)` | return 1 if key *k* is currently down | |
| `key.keyNames` | return all names usable with `key.pressed` | |
| `key.axis(h)` | return value of analog axis *h* | |
Remember that by "module" we really just mean a map containing some functions and other values. Don't take my word for it — check for yourself! |
```terminal |
]key |
{"available": FUNCTION(), "clear": FUNCTION(), "get": FUNCTION(), "p |
ressed": FUNCTION(keyName="space"), "axis": FUNCTION(axisName="Horiz |
ontal"), "keyNames": FUNCTION()} |
``` |
This just dumps the contents of the `key` module to the screen; it's not pretty, but it's good enough for you to verify what there. So let's try it! Enter: |
```terminal |
]foo = key.get |
``` |
After entering this method call, Mini Micro doesn't show the usual "]" prompt with blinking cursor... instead it looks like it's frozen. It's actually just waiting for you to press a key! Go ahead and press your favorite letter. (I'm pressing "m" here.) As soon as you do, the prompt and cursor return. Now if you e... |
That brings up the first good use of `key.get`: waiting for the user to be ready. But now our examples are going to get long enough to be worth using the code editor rather than the REPL (Read-Eval-Print Loop). So enter `edit` at the prompt to launch the Mini Micro code editor. Then type in the following. |
{caption:"Press any key!"} |
```miniscript |
print "Press any key to begin!" |
key.get |
print "OK, here we go..." |
wait |
print "And we're done!" |
``` |
After typing that in, click the close button in the top-left corner, or press Control-W, which does the same thing. The editor disappears and you're back to the REPL, at a fresh prompt right after `edit`. Now enter `run`, and you should see the program wait for you to indicate you're ready by pressing any key, and th... |
```terminal |
]run |
Press any key to begin! |
OK, here we go... |
And we're done! |
``` |
Of course you can't really press *any* key here; `key.get` does not return modifier keys like Shift and Control, nor function keys like F1. It does, however, return values for the Escape key, Return, Delete, Backspace, and arrow keys. Here's a fun way to test exactly what `key.get` returns. Use `edit` again, delete ... |
{caption:"`key.get` explorer"} |
```miniscript |
// What exactly does key.get return? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.