-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to check whether a key has been pressed without blocking the flow of the program? #42
Comments
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);
}); |
I would need the same feature. Running a periodic timer next to a game loop but the Timer.periodic(Duration(seconds: 1), (timer) {
print(timer); // fails
});
while(true) {
console.readKey();
} |
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 |
Some function similar to the C# function Keyboard.IsKeyPressed(...).
If not. How would you go about, for example. printing a message every second until a key is pressed?
The text was updated successfully, but these errors were encountered: