Get console user input as typed, char by char

前端 未结 2 1584
粉色の甜心
粉色の甜心 2020-12-20 12:30

I have a console application in Elixir. I need to interpret user’s input on by keypress basis. For instance, I need to treat “q” as a command to end the session, without use

2条回答
  •  感动是毒
    2020-12-20 13:10

    Your program doesn't get the keys because on Linux, the terminal is by default in cooked mode, which buffers all keypresses until Return is pressed.

    You need to switch your terminal to raw mode, which sends the keypresses to the application as soon as they happen. There's no cross-platform to do this.

    For unix-like systems there's ncurses, which has an elixir binding that you should check out: https://github.com/jfreeze/ex_ncurses. It even has an example to do what you want.

提交回复
热议问题