slang-users mailing list

[2021 Date Index] [2021 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Getting Keyboard Input


Duke Normandin <dukeofpurl@xxxxxxx> wrote:
> I've got the following in my code:
>
> variable c = 0;
> c = SLang_getkey ();

SLang_getkey is part of the C api.  This and related functions are not
part of the interpreter interface.  I have a module that wraps these
functions but I have never distribued it.

> But I'm euchered with this:
>
> rline_getkey is undefined
> ./tempConv.sl:28:<top-level>:Undefined Name
>
> Do I have to import/require something?
> I thought this function was a built-in?

It is built-in as part of the slsh readline interface.  This interface
provides editing capabilities, history, tab completion, etc.  It is
only exposed to the interpreter after `slsh_readline_init' has been
called. See slsh/scripts/svnsh and slsh/lib/sldb.sl for examples of
its use. However, I do not think that this what you are looking for.

Reading a single key is actually quite complicated and involves a lot
of low-level interaction with the terminal interface.  See the
code in src/slutty.c that is part of the slang source code for an
illustration of this.

A poor man's work-around is to let the operating system do much of the
heavy lifting via `stty` command.  The following example illustrates
this approach:

private define getkey ()
{
   () = system ("stty raw -echo");
   variable ch;
   if (-1 == read (fileno(stdin), &ch, 1))
     ch = -1;
   else ch = ch[0];

   () = system ("stty sane");
   return ch;
}

define slsh_main ()
{
   forever
     {
	() = fputs ("Press any key, 'q' to quit:", stdout);
	() = fflush (stdout);

	variable ch = getkey ();
	if ((ch == 'q') || (ch == -1))
	  break;

	() = fprintf (stdout, "\nYou pressed %d\n", ch);
     }
}

Happy Holidays,
--John
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


[2021 date index] [2021 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]