Why Is It So Hard to Detect Keyup Events on Linux?
Very interesting, in a different context I had to tackle a pretty similar problem with Redtamarin [0]<p>Traditionally under the CLI you will manage key input wih a readline() command or something similar to kbhit() and depending on your needs you&#x27;ll use getchar() then track if either a CR or LF is entered for the &quot;end of command&quot;, also EOF.<p>This is blocking, so nothing else can happen, and depending on how you do it, you can only read single byte chars and not mutlibyte chars (like CJK input)<p>something like<p><pre><code>    while( run )
    {
        i=kbhit();

        if( i != 0 )
        {
            key = String.fromCharCode( getchar() );

            if( (key == &quot;\n&quot;) || (key == &quot;\r&quot;) )
            {
                run = false;
            }
            else
            {
                buffer += key;
            }

            i = 0;
        }
    }
</code></pre>
another way to do it<p><pre><code>    while( run )
    {
        b = fgetc( stdin );

        if( b == EOF )
        {
            if( kbuffer.length == 0 )
            {
                return null;
            }

            run = false;
        }
        else if( b == LF )
        {
            run = false;
        }
        else
        {
            kbuffer.writeByte( b );
        }
    }
</code></pre>
which has 2 greats advantages, being able to read multiple bytes input (thanks to fgetc() which read the raw byte) and detect EOF (CTRL+D under POSIX, CTRL+Z under WIN32), but still blocking forever, using fgetc() the detection of EOF is done automatically for you (while getchar() getc() etc. do not detect that EOF)<p>now because Redtamarin is based on AVM2 and AS3, there is one part of the API which try to reimplement the Flash API with such things like KeyboardEvent that should be able to be non-blocking but still for a CLI environment<p>That KeyboardEvent should detect keyUp or keyDown but yeah it is hard to detect and if you try to do that for multipel platforms liek Windows &#x2F; macOS &#x2F; Linux it gets nigthmarish<p>In a little experiment [1] I found out you can do &quot;stupid things&quot; that actually work, like spawning a child worker (AVM2 uses pthread), blocking on the user input (like above) and then send back a message to your main worker and so receive a &quot;key event&quot;, all that allow to listen for input asynchronously<p>But then, what about making the difference between keyUp and keyDown ?
I decided to ignore the keyUp because in fact it does not really matters on the CLI or I least I don&#x27;t see any use to it, for a GUI yes I can see the use cases, but for a CLI? not so much<p>Purely on the CLI (no X Server) you don&#x27;t really listen for key events you read the stdin stream,
the only special events are signals like SIGINT SIGHUP etc. or special kind of signals like EOF.<p>The other things you can alterate is the buffering of that stdin and the raw mode&#x2F;cooked mode and echo off&#x2F;on.<p>So for a use case like navigating something using the &#x27;w&#x27;, &#x27;a&#x27;, &#x27;s&#x27;, &#x27;d&#x27; keys you just need to go async to listen for chars input (which key is pressed) and probably a mix of timeout on the last key pressed and a &quot;diff&quot; between the &quot;prev key&quot; and &quot;last key&quot;.<p>If last key pressed is &#x27;w&#x27; then go up, if you keep receiving a &#x27;w&#x27; key you keep going up,
if prev key was &#x27;w&#x27; and the last key is different you change direction.<p>And to detect when to stop to go up if last key pressed was &#x27;w&#x27; you just keep the time when this key was pressed, if 1 second elapsed and no more key events are received you stop going up, ergo you don&#x27;t need to detect keyUp, but maybe I&#x27;m missing something.<p><pre><code>  [0]: https:&#x2F;&#x2F;github.com&#x2F;Corsaair&#x2F;redtamarin
  [1]: https:&#x2F;&#x2F;twitter.com&#x2F;redtamarin&#x2F;status&#x2F;900794336031510530</code></pre>