neokeys 0.1.1 copy "neokeys: ^0.1.1" to clipboard
neokeys: ^0.1.1 copied to clipboard

Get characters from a terminal keyboard, similar to getch in curses.

neokeys #

Get characters from a terminal keyboard, similar to getch in curses library.

On pub.dev Code coverage Github action status Dartdocs Style guide

Purpose #

Dart's terminal support is fairly basic, with a Stdin class and little more, particularly for input (see examples/without_neokeys.dart)

As a result creating something representing a rendering loop quite difficult:

import 'dart:io';

void main() {
  stdin
    ..echoMode = false
    ..lineMode = false;
  _exampleOfWaitingForQ();
}

void _exampleOfWaitingForQ() async {
  const qKey = 0x71;

  final buffer = <List<int>>[];

  bool isHit(int keyCode) {
    return buffer.any((keys) => keys.length == 1 && keys.first == keyCode);
  }

  stdin.listen((keys) {
    if (completer.isCompleted) {
      return;
    }

    buffer.add(keys);
  });

  const frames = Duration(milliseconds: 1000 ~/ 30);
  await for (final _ in Stream<void>.periodic(frames)) {
    if (isHit(qKey)) {
      stdin
        ..echoMode = true
        ..lineMode = true;
      return;
    }
  }
}

Usage #

The same code as above in purpose, using neokeys:

import 'dart:io';

import 'package:neokeys/neokeys.dart';

void main() {
  stdin
    ..echoMode = false
    ..lineMode = false;
  _exampleOfWaitingForQ();
}

void _exampleOfWaitingForQ() async {
  // Creates a smart listener with buffering.
  final input = stdin.neokeys();

  const frames = Duration(milliseconds: 1000 ~/ 30);
  await for (final _ in Stream<void>.periodic(frames)) {
    // Fully typed API with convenience methods.
    if (input.isPressed(Key.q)) {
      stdin
        ..echoMode = true
        ..lineMode = true;
      return;
    }
  }
}

Contributing #

This package welcomes new issues and pull requests.

Changes or requests that do not match the following criteria will be rejected:

  1. Common decency as described by the Contributor Covenant.
  2. Making this library brittle.
  3. Adding platform-specific functionality.
  4. A somewhat arbitrary bar of "complexity", everything should be easy to use.
1
likes
140
pub points
13%
popularity

Publisher

verified publisherneodart.dev

Get characters from a terminal keyboard, similar to getch in curses.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

meta

More

Packages that depend on neokeys