Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export var keywords = {
"volume": audioVolume,
"envelope": audioEnvelope,
"speak": audioSpeak,
"voice": audioVoice
"voice": audioVoice,
"readkey": readKey,
"getkey": getKey
};

function expectParameters(...parameters) {
Expand Down Expand Up @@ -803,4 +805,41 @@ export function audioVoice(pitch, rate) {
audio.setVoice(getNumber(pitch), getNumber(rate));

basic.executeStatement();
}
}

export function readKey(identifier) {
expectParameters(identifier);

basic.setStore(identifier, hid.getInputBufferKey());

basic.executeStatement();
}

export function getKey(identifier) {
expectParameters(identifier);

var key;
var waitKey = function() {
basic.setDelayTimeout(
setTimeout(function() {
if (basic.running) {
key = hid.getInputBufferKey();
if (key === "") {
waitKey();
} else {
basic.setStore(identifier, key);
basic.executeStatement();
}
}
}, 10)
);
};

key = hid.getInputBufferKey();
if (key !== "") {
basic.setStore(identifier, key);
basic.executeStatement();
} else {
waitKey();
}
}
58 changes: 57 additions & 1 deletion docs/reference/io.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,60 @@ Gets the colour of the pixel at the position specified by arguments `x` and `y`.
<code>70 print saturation*100; "% saturation"</code>
<code>80 print luminance*100; "% luminance"</code>
</pre>
</details>
</details>

## `getkey`
```
getkey k
```

Waits for a key and sets `k` to the its key code.

<details>
<summary>Example 1: Show key codes</summary>
<pre>
<code>10 getkey x</code>
<code>20 print x</code>
<code>30 goto 10</code>
</pre>
</details>

<details>
<summary>Example 2: Move turtle</summary>
<pre>
<code>10 cls</code>
<code>20 print "Use W,A,S,D to move! Hit ESC to stop."</code>
<code>30 left 90</code>
<code>40 getkey x</code>
<code>50 if x = "w"</code>
<code>60 forward 10</code>
<code>70 else if x = "s"</code>
<code>80 backward 10</code>
<code>90 else if x = "a"</code>
<code>100 left 10</code>
<code>110 else if x = "d"</code>
<code>120 right 10</code>
<code>130 end</code>
<code>140 goto 40</code>
</pre>
</details>

## `readkey`
```
readkey k
```

Like `getkey`, but does not wait for a key. It returns what's already available or else an empty string (`""`).

<details>
<summary>Example</summary>
<pre>
<code>10 readkey x</code>
<code>20 if x != ""</code>
<code>30 print x</code>
<code>40 else</code>
<code>50 print ".";</code>
<code>60 end</code>
<code>70 goto 10</code>
</pre>
</details>
22 changes: 22 additions & 0 deletions hid.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ export const inputFormats = {
TEXT: 1
};

const inputBuffer = [];

function clearInputBuffer() {
inputBuffer.length = 0;
}

function appendInputBufferKey(key) {
inputBuffer.push(key);
}

export function peekInputBufferKey() {
return inputBuffer.length !== 0 ? inputBuffer[0] : "";
}

export function getInputBufferKey() {
return inputBuffer.shift() || "";
}

function getCaretFormatColourInsensitive(format) {
switch (format) {
case inputFormats.PROGRAM:
Expand Down Expand Up @@ -305,6 +323,8 @@ export function startInput(format = inputFormats.TEXT, relativeRow = term.scroll
}

export function startProgramInput(lineValue = "", immediateEdit = true, relativeRow = term.scrollDelta + term.row, rendering = true) {
clearInputBuffer();

hidInput.value = lineValue;

var newProgramInput = new Input(inputFormats.PROGRAM, relativeRow, lineValue);
Expand Down Expand Up @@ -348,6 +368,8 @@ function dispatchInputEvent(event) {

if (focusedInput != null) {
focusedInput.readKey(event);
} else {
appendInputBufferKey(event.key);
}
}

Expand Down
3 changes: 2 additions & 1 deletion syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const RE_NUMERIC_LITERAL_HEX = /(?<![a-z_])0(?:x|X)[0-9a-fA-F]+/;
const RE_NUMERIC_LITERAL_BIN = /(?<![a-z_])0(?:b|B)[01]+/;
const RE_NUMERIC_LITERAL_OCT = /(?<![a-z_])0(?:o|O)[0-7]+/;
const RE_NUMERIC_LITERAL_SCI = /(?:(?<=div|mod|and|or|xor|not)|(?<![a-z_][a-z0-9_]*))(?:[0-9]+\.?[0-9]*|[0-9]*\.?[0-9]+)(?:[eE][+-]?[0-9]+)?(?!\.)/;
const RE_KEYWORD = /(?<![a-z_])(?<![a-z_][0-9]+)(?:print|input|goto|gosub|return|if|else|end|forward|for|to|step|next|break|continue|stop|repeat|while|until|loop|deg|rad|gon|turn|pos|cls|delay|bg|fg|move|draw|plot|stroke|fill|text|copy|restore|frame|getpixel|dim|push|pop|insert|remove|show|hide|forward|backward|left|right|penup|pendown|angle|note|play|rest|quiet|bpm|volume|envelope|speak|voice)/i;
const RE_KEYWORD = /(?<![a-z_])(?<![a-z_][0-9]+)(?:print|input|goto|gosub|return|if|else|end|forward|for|to|step|next|break|continue|stop|repeat|while|until|loop|deg|rad|gon|turn|pos|cls|delay|bg|fg|move|draw|plot|stroke|fill|text|copy|restore|frame|getpixel|dim|push|pop|insert|remove|show|hide|forward|backward|left|right|penup|pendown|angle|note|play|rest|quiet|bpm|volume|envelope|speak|voice|readkey|getkey)/i;
const RE_FUNCTION_NAME = /(?<![a-z_])(?<![a-z_][0-9]+)(?:sin|cos|tan|asin|acos|atan|log|ln|sqrt|round|floor|ceil|abs|asc|bin\$|oct\$|hex\$|bin|oct|hex|len|last|lower\$|upper\$|trim\$|ltrim\$|rtrim\$|chr\$)/i;
const RE_CONSTANT = /(?<![a-z0-9_])(?:pi|e|phi|epoch|random|col|row|key|heading)(?![a-z0-9_])/i;
const RE_OPERATOR = /\+|-|\*|\/|\^|(?<![a-z_])(?:div|mod)(?![a-z_])|&|\||~|;/i;
Expand Down Expand Up @@ -86,6 +86,7 @@ const KEYWORD_COLOURS = {
"restore": {background: "purple", foreground: "white"},
"frame": {background: "purple", foreground: "white"},
"getpixel": {background: "purple", foreground: "white"},
"getkey": {background: "purple", foreground: "white"},
"dim": {background: "yellow", foreground: "black"},
"push": {background: "yellow", foreground: "black"},
"pop": {background: "yellow", foreground: "black"},
Expand Down