[erlang-questions] Read only 1 character from STDin without requiring Return

qp quantumpotato@REDACTED
Tue Mar 14 16:03:12 CET 2017


I see. I was wanting to get gamepad input to control a game. I naively
assumed this was serial, Daniel's reply suggested the gamepad might be a
HID device which I haven't checked yet.

"how are you NOT going to mess that up?" coincides with the response on
http://stackoverflow.com/questions/42750491/read-a-character-input-from-erlang-without-requiring-the-return-key-pressed-from/42755503#42755503
"you are entering a world of pain you never knew existed"

So for now, I'm running a Cowboy server and using a c program to send
characters pressed through cURL

#include<stdio.h>
#include<stdlib.h>

int main(void){
  system ("clear");
  int c;
  /* use system call to make terminal send all keystrokes directly to stdin
*/
  system ("/bin/stty raw");
  while((c=getchar())!= '.') {
    char command[100];
    sprintf(command, "curl localhost:8080/info?%c" ,c);
    system(command);
    printf("\r\n");

    /* type a period to break out of the loop, since CTRL-D won't work raw
*/
  }
  /* use system call to set terminal behaviour to more normal behaviour */
  system ("/bin/stty cooked");
  system ("clear");
  system ("echo ok\n");
  return 0;
}


On Tue, Mar 14, 2017 at 1:16 AM, Richard A. O'Keefe <ok@REDACTED>
wrote:

>
> > On 13/03/2017, at 7:19 AM, qp <quantumpotato@REDACTED> wrote:
> >
> > How do read 1 character at a time without requiring the enter press?
>
> The obvious question is "why do you want to do this"?
>
> It's actually nowhere as simple an issue as you might think.
> Seriously, it's not clear what "read 1 character at a time"
> MEANS.  Do you mean
>  - read a keycode?
>  - read a byte?
>  - read a possibly multibyte character?
> and
>  - if the user pushes a an error or function key,
>    sending an ESC [ .... <char> sequence, do you
>    want just the ESC (and if so what happens to everything
>    after it) or the whole thing
>  - how are you going to tell the difference between ESC [ ...
>    sent from a special key and an ESC sent from the ESC key
>    (I've seen two approaches: don't try to distinguish, and
>    use a timeout)
> and
>  + do you want the character to be echoed
>  - or not
>  + do you want some interrupt characters to be
>    heeded still
>  - or not
> and
>  - if there are multiple characters already buffered
>    (from a time when the terminal was in a "cooked" mode, perhaps)
>    and you switch to whatever character-at-a-time mode you want,
>    what is to happen to those characters?
> and above all,
>  * given that Erlang uses GNU readline or something very like
>    it, even when calling io:get_chars/2, how are you NOT going
>    to mess that up?
>
> Having said that,
> http://erlang.org/pipermail/erlang-questions/2009-November/047298.html
> tells us that Joe Armstrong wrote an emacs-like editor in Erlang.
> <quote>
> I wrote a simple emacs editor years ago -
> it's in the widgets subdirectory of
> http://www.sics.se/~joe/ex11/download/release-2.5.tgz
>
> The only tricky part was not the emacs logic,
> but the screen display
> and catching the keystrokes and mouse events.
> </quote>
>
> That link is dead, but https://github.com/baryluk/ex11
> should work.
>
> It's not *much* of an emacs: ^A ^E ^D ^B ^F PgUp PgDn
> arrow keys and inserting plain characters.  Call it
> proof-of-concept.
>
> An alternative would be to use a separate program to do this.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170314/e0e44717/attachment.htm>


More information about the erlang-questions mailing list