0

I have a background thread that reads bytes in an infinite loop and sends them to main through a channel. I'm looking for the equivalent of C's getch().

I found this:

let byte = io::stdin().bytes().next().expect("no byte read").ok().unwrap();

This waits for an Enter which I don't want. It also prints the inputted character, which is also unwanted.

After I press Enter, I get all the characters I pressed one by one and finally instead of just a 10 (new line), I get 10 followed by 13. Why?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Neo
  • 3,534
  • 2
  • 20
  • 32
  • Isn't waiting for keyboard input and echoing the character something that already happens in `getche`? Are you sure that you want an equivalent to this function? – E_net4 Jul 06 '17 at 21:40
  • You also seem to have some misconceptions: 12 is the character code for new page, not new line (10). And you are likely in a windows system, which produces the CR+LF combination on enter. In another side note, you can call `unwrap` on a `Result`, you don't have to call `ok` beforehand. – E_net4 Jul 06 '17 at 21:43
  • @E_net4 Oops I meant `getch()`. I do want to wait for keyboard input but I don't want to print it. – Neo Jul 06 '17 at 21:44
  • `getch()` is not standard in C, but it's come from ncurses lib. You want a crate that handle terminal I/O. https://github.com/jeaye/ncurses-rs, https://github.com/ihalila/pancurses, https://github.com/gyscos/Cursive, etc... – Stargateur Jul 06 '17 at 21:45
  • @Stargateur I see how I can use ncurses but isn't there a simpler solution? – Neo Jul 06 '17 at 21:53
  • @Neo When I look, what you ask: "it waits for an enter which I don't want", https://stackoverflow.com/a/30013230/7076153, "It also prints the inputted char, which is also unwanted", clearly std will not handle that, this is about terminal configuration. By the way, ASCII code 13 is just a `'\r'`, read about line break in windows. – Stargateur Jul 06 '17 at 21:55
  • @Stargateur This exact answer is the reason I do it in a different thread. I don't mind blocking (until any char is typed), but I don't want it to wait for an **enter**. Isn't that possible? Also about printing, I prefer it not to print but if it's inevitable I can live with it. (However I'm 100% sure it's technically possible as `getch()` does it.) – Neo Jul 06 '17 at 22:03
  • @Neo the answer to without press enter is again ncurses, https://stackoverflow.com/questions/26321592/how-can-i-read-one-character-from-stdin-without-having-to-hit-enter, please learn to search a little ! – Stargateur Jul 06 '17 at 22:07

0 Answers0