diff --git a/commands.js b/commands.js index f343390..ed46575 100644 --- a/commands.js +++ b/commands.js @@ -56,7 +56,9 @@ export var keywords = { "volume": audioVolume, "envelope": audioEnvelope, "speak": audioSpeak, - "voice": audioVoice + "voice": audioVoice, + "readkey": readKey, + "getkey": getKey }; function expectParameters(...parameters) { @@ -803,4 +805,41 @@ export function audioVoice(pitch, rate) { audio.setVoice(getNumber(pitch), getNumber(rate)); basic.executeStatement(); -} \ No newline at end of file +} + +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(); + } +} diff --git a/docs/reference/io.md b/docs/reference/io.md index 4a4ec5d..cf2149d 100644 --- a/docs/reference/io.md +++ b/docs/reference/io.md @@ -307,4 +307,60 @@ Gets the colour of the pixel at the position specified by arguments `x` and `y`. 70 print saturation*100; "% saturation" 80 print luminance*100; "% luminance" - \ No newline at end of file + + +## `getkey` +``` +getkey k +``` + +Waits for a key and sets `k` to the its key code. + +
+Example 1: Show key codes +
+10 getkey x
+20 print x
+30 goto 10
+
+
+ +
+Example 2: Move turtle +
+10 cls
+20 print "Use W,A,S,D to move! Hit ESC to stop."
+30 left 90
+40 getkey x
+50 if x = "w"
+60 forward 10
+70 else if x = "s"
+80 backward 10
+90 else if x = "a"
+100 left 10
+110 else if x = "d"
+120 right 10
+130 end
+140 goto 40
+
+
+ +## `readkey` +``` +readkey k +``` + +Like `getkey`, but does not wait for a key. It returns what's already available or else an empty string (`""`). + +
+Example +
+10 readkey x
+20 if x != ""
+30 print x
+40 else
+50 print ".";
+60 end
+70 goto 10
+
+
diff --git a/hid.js b/hid.js index ca70c6b..17e8920 100644 --- a/hid.js +++ b/hid.js @@ -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: @@ -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); @@ -348,6 +368,8 @@ function dispatchInputEvent(event) { if (focusedInput != null) { focusedInput.readKey(event); + } else { + appendInputBufferKey(event.key); } } diff --git a/syntax.js b/syntax.js index 18bf72c..5c70c5d 100644 --- a/syntax.js +++ b/syntax.js @@ -9,7 +9,7 @@ const RE_NUMERIC_LITERAL_HEX = /(?