[chuck-users] KBHit and arrow keys

Jim Hinds jahbini at jahbini.info
Thu Apr 27 13:17:34 EDT 2006


The behavior of KBHit is kind of standard for ketting data from some  
asynchronous source.  Since the chuck program (or any user prog) runs  
at some unknown speed it is possible for several keys to be pressed  
before you get control.

In that case the boolean (KBHit) returns true.   Your input routine  
would then process all the characters in the input buffer.  That's  
why kb.getchar returns (and removes) the first char in the input  
buffer.  When kb.getchar returns zero, you know that you have  
processed all of the input.   This allows KBHit to get re-enabled so  
that it will indicate when new stuff is available.

This isn't unique to the keyboard, but is pretty much the way all  
interrupt driven I/O works.  So, be happy: what you thought were  
warts in your program were really elegant solutions to a universal  
problem.  Or something like that.

Jim Hinds
jahbini at jahbini.org
http://www.celarien.com/
http://www.jahbini.info/



On Apr 27, 2006, at 6:00 AM, chuck-users- 
request at lists.cs.princeton.edu wrote:

> Send chuck-users mailing list submissions to
> 	chuck-users at lists.cs.princeton.edu
>
> To subscribe or unsubscribe via the World Wide Web, visit
> 	https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
> or, via email, send a message with subject or body 'help' to
> 	chuck-users-request at lists.cs.princeton.edu
>
> You can reach the person managing the list at
> 	chuck-users-owner at lists.cs.princeton.edu
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of chuck-users digest..."
> Today's Topics:
>
>    1. KBHit and arrow keys. (Kassen)
>
> From: Kassen <signal.automatique at gmail.com>
> Date: April 26, 2006 10:22:12 AM HST
> To: "ChucK Users Mailing List" <chuck-users at lists.cs.princeton.edu>
> Subject: [chuck-users] KBHit and arrow keys.
> Reply-To: ChucK Users Mailing List <chuck- 
> users at lists.cs.princeton.edu>
>
>
> 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?
>
> Yours,
> Kas.
>
>
> ---------------------------------------------------------------------- 
> --------
> // 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>>>;
>         	}
>      else
>      	{
>         //do something else to normal keys
>      	<<<K>>>;
>      	}
>     }
> }
> -------------------------------------------------
>
>
>
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20060427/a994b373/attachment.htm 


More information about the chuck-users mailing list