Read only 1 character from STDin without requiring Return

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
qp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Read only 1 character from STDin without requiring Return

qp
I'm looking at the `io` module http://erlang.org/doc/man/io.html#read-1

and finding that reading input uses a Prompt and waits for the user to press Enter (Return?).

How do read 1 character at a time without requiring the enter press?




I tried


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
qp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Read only 1 character from STDin without requiring Return

qp
Sorry, I meant to say

I tried using `/bin/stty raw`

    read() ->
      Res = os:cmd("/bin/stty raw"),
      io:format(Res),
      stty: stdin isn't a terminal


but get stdin isn't a terminal error

On Sun, Mar 12, 2017 at 2:19 PM, qp <[hidden email]> wrote:
I'm looking at the `io` module http://erlang.org/doc/man/io.html#read-1

and finding that reading input uses a Prompt and waits for the user to press Enter (Return?).

How do read 1 character at a time without requiring the enter press?




I tried



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Read only 1 character from STDin without requiring Return

Daniel Goertzen-3
In reply to this post by qp
Are you sure the gamepad is a serial device?  I would expect it to be a HID that goes through the OS's game controller driver plumbing.  Maybe tinkering with libSDL in C would be a good place to experiment, and then that could be make into a port program for use with Erlang.

On Sun, Mar 12, 2017 at 1:19 PM qp <[hidden email]> wrote:
I'm looking at the `io` module http://erlang.org/doc/man/io.html#read-1

and finding that reading input uses a Prompt and waits for the user to press Enter (Return?).

How do read 1 character at a time without requiring the enter press?




I tried

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Read only 1 character from STDin without requiring Return

Richard A. O'Keefe-2
In reply to this post by qp

> On 13/03/2017, at 7:19 AM, qp <[hidden email]> 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.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
qp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Read only 1 character from STDin without requiring Return

qp
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 <[hidden email]> wrote:

> On 13/03/2017, at 7:19 AM, qp <[hidden email]> 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.



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Read only 1 character from STDin without requiring Return

Tony Rogvall-2

> On 14 mar 2017, at 16:03, qp <[hidden email]> wrote:
>
> 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.

If that is the case then you might use
https://github.com/tonyrog/hid

:-)

/Tony

>
> "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 <[hidden email]> wrote:
>
> > On 13/03/2017, at 7:19 AM, qp <[hidden email]> 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.
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

signature.asc (817 bytes) Download Attachment
Loading...