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

593 Views Asked by At

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?

1

There are 1 best solutions below

2
Soup in Boots On BEST ANSWER

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.