neokeys 0.1.1
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.
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:
- Common decency as described by the Contributor Covenant.
- Making this library brittle.
- Adding platform-specific functionality.
- A somewhat arbitrary bar of "complexity", everything should be easy to use.