I want to change the Sk.inputfun()
for Skulpt to let the user type in a <textarea>
and hit the ENTER
key to submit. I need to pass in a single function to Skulpt that gets user input, however, I can't find any way to do this other than prompt()
.
The only way I could think to do this was to delay the return using setTimeout()
until an event triggered the enter key button and changed a flag.
var enterPressed = false;
$("#output").keyup(function(e){
if((e.keyCode || e.which) == 13) { //Enter keycode
enterPressed = true;
}
});
and then to wait for the change I had:
Sk.configure({
inputfun: function (prompt) {
function checkFlag() {
if(enterPressed) {
enterPressed = false
return $('#output').val();
} else {
window.setTimeout(checkFlag, 100);
}
}
return checkFlag();
},
inputfunTakesPrompt: true,
output: outf,
read: builtinRead
});
However this dosen't work for a few reasons. a) The timeout function can't return a value, and b) Timeout dosen't even delay the return function, and it simply returns nothing.
I have tried using .done()
as well, but couldn't get it to work.
There doesn't seem to be any way to do this that I could find online, any help would be much appreciated!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…