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@jahbini.org http://www.celarien.com/ http://www.jahbini.info/ On Apr 27, 2006, at 6:00 AM, chuck-users- request@lists.cs.princeton.edu wrote:
Send chuck-users mailing list submissions to chuck-users@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@lists.cs.princeton.edu
You can reach the person managing the list at chuck-users-owner@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
Date: April 26, 2006 10:22:12 AM HST To: "ChucK Users Mailing List" Subject: [chuck-users] KBHit and arrow keys. Reply-To: ChucK Users Mailing List 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@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Jim Hinds;
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.
Ah, yes, I did some more testing and now it's clear. Thanks. I was under the impression that "kb.getchar()" would be updated every iteration of the "while" loop and that the top value would be cleared at that time and instead calling it already clears it from the buffer so you need to be carefull how you do that or it'll be gone forever. I didn't expect this to work; if ( ( kb.getchar() == 224) && ( kb.getchar() == 72) ) <<<"up">>>; But it does. Considdering what you wrote that's all very understandable, realy, but in that case it might be nice if something along the lines of the example I posted previously would be in the documentation somewhere because that's a bit tricky and this stuff covers almost half the keyboard. So, should I add it to the Wiki? If so, what would be a apropriate place? Thanks again for your the explanation, Yours, Kas.
Hey, Thanks for your discussion about this. Please post your findings on the wiki. I would be more than happy to merge this into the manual. If you have some LaTeX skills you could also do it yourself and send me a patch. Nice work. --art On 28-Apr-06, at 4:14 PM, Kassen wrote:
Jim Hinds;
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.
Ah, yes, I did some more testing and now it's clear. Thanks.
I was under the impression that "kb.getchar()" would be updated every iteration of the "while" loop and that the top value would be cleared at that time and instead calling it already clears it from the buffer so you need to be carefull how you do that or it'll be gone forever.
I didn't expect this to work; if ( ( kb.getchar() == 224) && ( kb.getchar() == 72) ) <<<"up">>>; But it does.
Considdering what you wrote that's all very understandable, realy, but in that case it might be nice if something along the lines of the example I posted previously would be in the documentation somewhere because that's a bit tricky and this stuff covers almost half the keyboard.
So, should I add it to the Wiki? If so, what would be a apropriate place?
Thanks again for your the explanation,
Yours, Kas. _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
On 4/29/06, Adam Tindale
Hey,
Thanks for your discussion about this. Please post your findings on the wiki. I would be more than happy to merge this into the manual. If you have some LaTeX skills you could also do it yourself and send me a patch.
Ok, I added it to the Wiki with some explanation on what the problem is and how to solve it; it's under tutorials and compared to the pages made by the more experienced wiki users it looks a bit rough (no index yet for example) but it should be usefull. I don't speak LaTeX but do feel free to borrow from and/or edit what I did. It'd be nice if somebody could have a look at this and make sure I didn't make any huge oopses; it'd look bad if the examples on the wiki would have issues. Oh, and I removed all the double hard returns from my examples. I don't really like it this way, I think it's nicer if there's some space between conceptual chunks but I also think this is preferable to the wiki cutting it up into several block quotes. Couldn't find a way to have those blocks with double hard returns inside of them. Yours, Kas.
Oh, and I removed all the double hard returns from my examples. I don't really like it this way, I think it's nicer if there's some space between conceptual chunks but I also think this is preferable to the wiki cutting it up into several block quotes. Couldn't find a way to have those blocks with double hard returns inside of them.
Hi, I ran into this before. In the blank lines just put a space at the beginning and then the wiki knows that it is blank space. In vim (I think this works in vi as well) you can do :%s/^/\ /g and that will put a space at the beginning of each line in the text and then you are good to go. Ge pointed this out to me when I was cursing the wiki up and down. Now it is a lovely place:) Thanks for your page, it looks great. I will take a closer look at it later today to check for errors. --art
participants (3)
-
Adam Tindale
-
Jim Hinds
-
Kassen