How to capture keystrokes?

337 views Asked by At

I'd like to make a simple Erlang console game. I need to control my "car" in that game by key strokes - key up or W key. Is there any way to do that? I read that wx library has such capability but I'm looking for something simple and in my opinion wx library is overkill. I want to grab an event not the character from stdin.

EDIT:

Ok, I've choosen ncurses and cecho as a Erlang library for that.

2

There are 2 answers

0
Pascal On BEST ANSWER

I know only 2 way to do this: gs and wx.

gs is a bit simpler but the first line of the documentation is a warning telling that gs is not recommended for new application, use wx instead :o)

wx isn't so complex, but its documentation is really poor, and simply refers to wxWidget one. at least there are some demo (demo, sudoku, xrc...) that helps to start to build something. And once you succeed to have your first panels and sizers working, it is really simple to get the key stroke or mouse events an react on them. My main difficulty was in displaying graphical things fast and smooth enough even if at the end it is only a few lines of code (a few 10s).

finally, the erlang console is meant to evaluate erlang expression, so there is no chance that you can capture simple event and control the display in that environment (at a reasonable effort at least).

0
Hynek -Pichi- Vychodil On

You can try study kernel's modules group and user_drv but they both has undocumented interface and can be subject of change in any future Erlang/OPT version. You can also made your own driver or NIF to handle termios settings and make your own implementation.

BTW I would definitely stick with wx.