Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read character-wise on the standard input in Erlang

I'm trying to code a basic video-game and would like the input to be entered from the keyboard. Therefore, I need to read the characters on the standard input as they are produced. Because of the buffering, io:get_chars, io:fread will return only after the return key is pressed.

  1. Is it possible to access the characters in the standard input as they are produced?
  2. How should I do it?

The point of the project is not to make a real-life game, it is just a way of learning about Erlang. Therefore performance is not an issue.

EDIT: This project seems to offer the feature I am looking for. If I am not mistaken, though, a part of the code is written in C and sends the characters to the Erlang part via message passing. Is there an alternative, native to Erlang, to this approach, or is this the only approach that would work?

like image 458
Daiwen Avatar asked Nov 01 '22 10:11

Daiwen


1 Answers

Generally speaking, reading from STDIN is not the way you want to go when it comes to processing keyboard input for a game. You want to be able to read the state of keys at any given moment.

Take a look at wx (and the reference manual)

It's an Erlang interface to the wxWidgets library, and includes interfaces for handling various inputs, including keyboards.

like image 111
Soup d'Campbells Avatar answered Nov 15 '22 19:11

Soup d'Campbells