1

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.

Hitokiri
  • 3,607
  • 1
  • 9
  • 29
Mr No Name
  • 46
  • 3
  • 3
    First of all, there's no "C/C++" language, there are the two ***very*** different languages C and C++. There's also no standard C or C++ header file `conio.h`, it's originally a DOS specific header which still exists in Windows only. – Some programmer dude May 14 '20 at 21:05
  • You are mistaken. There no such header file called ``, in the C++ standard. This is an operating system-specific header file that provides functionality that's specific to your operating system only. There's nothing analogous in the C++ standard. – Sam Varshavchik May 14 '20 at 21:06
  • In order to read a key without pressing enter, you will need to use OS specific API. If you are using a GUI system, you may be able to respond to messages about the key press or release. Some OS will allow a callback function when key state changes. All depends on your Operating System and maybe framework. – Thomas Matthews May 14 '20 at 21:28
  • Regardless of the non-standardness of `getch()` and `conio.h`, you can look at how the Flutter tooling (which is written in Dart) handles triggers a hot-reload when pressing `r`. – jamesdlin May 14 '20 at 21:58

2 Answers2

1

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();
}
julemand101
  • 28,470
  • 5
  • 52
  • 48
0

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!

Mr No Name
  • 46
  • 3