Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make that when I press space it does an action in my CLI in dart

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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!!!!!');
    }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading