Reputation: 56
In the c/c++ languages, there is a function in the conio.h
header file called getch()
which lets you input only 1 character and doesn't echo it on the screen and once that character has been typed, it automatically goes to the next line of code without having to press enter.
I've tried using the stdin.readByteSync()
in dart but it doesn't give me the functionality that getch()
gives in c/c++. I'd like to know if there is a way to make a function or method in dart that behaves in the same manner as getch()
does in c/c++. Thank you.
Upvotes: 1
Views: 685
Reputation: 56
Thank you all for your contribution. However adding to the answer I got that went like this
import 'dart:io';
void main() {
print(getch());
}
int getch() {
stdin.echoMode = false;
stdin.lineMode = false;
return stdin.readByteSync();
}
I decided to add something to make it more like the getch() function in conio.h header file in c language. The code goes like this
import 'dart:io';
void main() {
print(getch());
}
String getch() {
stdin.echoMode = false;
stdin.lineMode = false;
int a = stdin.readByteSync();
return String.fromCharCode(a);
}
Although it only works on cmd, powershell and linux terminal and not on intelliJ, it is better than nothing. The most important thing is to get the foundation of dart for things like flutter and web. And with this little knowledge, I was put it into practice and make a simple and basic typing game in dart. The code is below:
import 'dart:io';
import 'dart:convert';
import 'dart:core';
void main() {
Stopwatch s = Stopwatch();
String sentence = 'In the famous battle of Thermopylae in 480 BC, one of the most famous battles in history, King Leonidas of Sparta said the phrase'
' Molon Labe which means \"come and take them\" in ancient greek to Xerxes I of Persia when the Persians asked the Spartans to lay'
' down their arms and surrender.';
List<String> sentenceSplit = sentence.split(' ');
int wordCount = sentenceSplit.length;
print('Welcome to this typing game. Type the words you see on the screen below\n\n$sentence\n\n');
for (int i=0; i<sentence.length; i++) {
if(i==1) {
s.start(); // start the timer after first letter is clicked
}
if(getch() == sentence[i]) {
stdout.write(sentence[i]);
}
else {
i--;
continue;
}
}
s.stop(); // stop the timer
int typingSpeed = wordCount ~/ (s.elapsed.inSeconds/60);
print('\n\nWord Count:\t$wordCount words');
print('Elapsed time:\t${s.elapsed.inSeconds} seconds');
print('Typing speed:\t$typingSpeed WPM');
}
String getch() {
stdin.echoMode = false;
stdin.lineMode = false;
int a = stdin.readByteSync();
return String.fromCharCode(a);
}
You can go ahead and advance to make it a way that when the user starts the game again, it should show a different text so they don't get used to it. But anyway, that is it for this question. It is officially closed. Although, if you have anymore to add, feel free to drop it here. Thank you!
Upvotes: 0
Reputation: 31289
You just need to set the following option to false: https://api.dart.dev/stable/2.8.2/dart-io/Stdin/lineMode.html
And if you are using Windows you also need to set the following to false first according to the documentation: https://api.dart.dev/stable/2.8.2/dart-io/Stdin/echoMode.html
A simple working example, which are just repeating what you type, can be made like this. It does not work inside IntelliJ but works from CMD, PowerShell and Linux bash:
import 'dart:convert';
import 'dart:io';
void main() {
stdin.echoMode = false;
stdin.lineMode = false;
stdin.transform(utf8.decoder).forEach((element) => print('Got: $element'));
}
By doing this we can also do you own suggestion and use stdin.readByteSync()
(just notice that if you get a UTF-8 input, a character can contain multiple bytes:
import 'dart:io';
void main() {
print(getch());
}
int getch() {
stdin.echoMode = false;
stdin.lineMode = false;
return stdin.readByteSync();
}
Upvotes: 1