[chuck-users] KBHit and arrow keys.

Kassen signal.automatique at gmail.com
Wed Apr 26 16:22:12 EDT 2006

Hello all,

The new keyboard input is great but getting the arrow and F1-F10 and
so on keys to work is kinda tricky, I found. I cooked up a sorta kinda
maybe solution to this that I thought might be useful to others as
well. See below.

use the up and down keys to increase or decrease "bla", then print
bla's current value. This won't win any beauty contests but it does
work. I'm not at all sure why it needs two temporary variables but I
can't get it to work without them. If I don't use temporary variables
it seems that I can only use one test on kb.getchar() before it
disappears(??) and one test isn't enough for anything beyond the most
simple of triggers. The same method could be used to get function keys
to work.

I hope this is of use to some. This works on my IBM- compatible laptop
running XP, no refunds if it turns out Mac's use some entirely
different way of reading the keyboard; I have no idea how those work.
If it's found useful I could add it to the Wiki?


// the keyboard input
KBHit kb;

//some setting that's of great importance somewhere
int bla;

// time-loop
while( true )
    // wait on kbhit event
    kb => now;

    // potentially more than 1 key at a time
    while( kb.more() )
    //store number to avoid it getting lost after one check
    kb.getchar() => int K;

    //arrow keys are encoded in two numbers with the first being 224
    if (K == 224 )

                //wait for the second number
            	kb => now;

                //the same "anti number disapear" trick
            	kb.getchar() => int L;

               //do intuitively sensible stuff to "up"...
            	if(L == 72) 1 +=> bla;

               //........and "down"......
            	if(L == 80) 1 -=> bla;

               //...and make the result go somewhere.
            	<<<"bla is", bla>>>;
        //do something else to normal keys

More information about the chuck-users mailing list