Giter VIP home page Giter VIP logo

Comments (4)

JoCat avatar JoCat commented on June 20, 2024

I set myself a similar task (to catch the TAB keystroke) and found this library. But the only problem is that, as it turned out, it works synchronously.
So far I've come to this solution:

  • Disable echoMode and lineMode (in general, only lineMode should be disabled in this case, but the dart API documentation states the following "On Windows this mode can only be disabled if echoMode is disabled as well", so I had to disable both modes)
  • Get BroadcastStream from stdin
  • Make 2 subscriptions, one as before through transform(utf8.decoder).transform(LineSplitter()) and the second already for direct listening from the stream and when you receive a press on tab (keyCode=9) perform any actions, otherwise write to stdout stdout.writeCharCode(keyCode[0]) (remember I had to disable echoMode)
    image

from dart_console.

JoCat avatar JoCat commented on June 20, 2024

UPD

I wrote a helper class

import 'dart:convert';

import 'dart:io';

class Console {
  Console(Function(List<int>) onKeyListener, Function(String) onLineListener) {
    stdin.echoMode = false;
    stdin.lineMode = false;

    stdin.asBroadcastStream()
      ..listen(onKeyListener)
      ..transform(Utf8Decoder())
          .transform(LineSplitter())
          .listen(onLineListener);
  }
}

there was a little more code, but I cut out the excess

use:

Console((charCodes) {
  // On key press event
  stdout.write(charCodes);
}, (line) {
  // On line print event
  stdout.write(line);
});

from dart_console.

xErik avatar xErik commented on June 20, 2024

I would need the same feature.

Running a periodic timer next to a game loop but the readKey() blocks exectution.

Timer.periodic(Duration(seconds: 1), (timer) {
  print(timer); // fails
});

while(true) {
  console.readKey();
}

from dart_console.

bsutton avatar bsutton commented on June 20, 2024

stdin is a stream with a listen method, if you combine this with placing stdin into non-line mode I think you might have a solution.

I've not tried it but something like

 stdin.lineMode = false;
stdin.listen((data) -> print(data));

remember to place stdin back into lineMode before you exit

from dart_console.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.