I am creating a dart cli that allows to display one or more colors at random but what I would like to do is that when we press the "space" key it displays a new color.
Code in nodeJS that I want to reproduce in dart. Here the module used is Terminal-kit
term.grabInput(true)
function checkKeyPress(name) {
//Si la touche espace est appuyée, on relance le programme
if (name === ' ') {
term.grabInput(false)
term.removeListener('key', checkKeyPress)
//Faire attendre 0.1 secondes
setTimeout(() => {
colornew()
}, 80)
}
if (name === 'ESCAPE') {
term.grabInput(false)
term.removeListener('key', checkKeyPress)
setTimeout(() => {
console.clear()
process.stdout.write('\x1Bc');
return main()
}, 80)
}
}
term.addListener('key', checkKeyPress)
}
colornew()
CodePudding user response:
You can do it by disabling lineMode
on stdin
which makes it so your terminal are sending data to your data program right away instead of waiting for the next line.
I would also recommend disabling echoMode
so you can't see the space
in the terminal.
So something like this:
import 'dart:io';
final int spaceChar = ' '.codeUnitAt(0);
void main() async {
stdin.echoMode = false;
stdin.lineMode = false;
await for (final event in stdin) {
if (event.first == spaceChar) {
print('SPACE!!!!!');
}
}
}