ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've Googled for the last couple of days. I can't find a way of totally clearing the keyboard buffer without hanging the machine up.
I'm using Allegro 4 with C++. My simple (console) grid type game requires moving in one of four directions. (like checkers I suppose) However if the direction key is held a little too long, the player continues to move independently the next time through the loop after the game pieces have moved.
I have tried clear_keybuf() (Allegro specific) but it doesn't clear all the keys. I've tried using keypressed() (Again Allegro specific)to read the buffer so I can disregard all until the buffer is empty, but after it's empty it stops waiting on a key hit.
I've looked at flush (C++) but really can't get enough good examples to understand how to use correctly or if it's even applicable to what I'm trying to do.
Any suggestions you may have would be greatly appreciated.
Thanks in advance
Ace
Last edited by Ace Blackwell; 02-28-2015 at 10:33 AM.
Thanks for the research dugan but as I understood this one, it deals more with strings. It also appears you have to know the length in advance. I was thinking of more like the
while ((c = getchar()) != '\n' && c != EOF);
listed at the bottom of the web page, but because the inputs are more likely to be a repetitive keystroke or some random chars there will not be a newline char to stop the loop.
genss, from what I can tell read() is designed for file reading. I suppose you could change your input to be from file to keyboard somehow,but not sure if the keyboard buffer includes EOF
NevermTeve. That looks like some hardcore coding for my abilities, but will investigate. I may have led us a stray. Reading the buffer isn't important to me. I just want to empty (ignore it) until I'm ready for the next player move. I was thinking if i could "getch" until empty, that might work. the only problem is once empty the program stops. I have seen some talk of nonblocking and polling, maybe that's a direction to take. I'll look into it.
In Unix, this thing is called 'teletype' or 'tty' or 'terminal, and it is treated as a file, so 'read' is perfectly ok. Still you should check if your standard input is a terminal, see 'isatty'
By default, your standard input is in 'blocking mode' and works line-buffered. For games, one would prefer non-buffered mode (read keystrokes individually), and also might want to switch to non-blocking mode.
Well I got it solved. It appears Allegro has a way to clear the buffer. Which ironically their clear_keybuf() command doesn't do very well. There is a command called keypressed(). It doesn't wait for a key hit, it just reads whats in the buffer. It returns Ascii code for any keys and FALSE if empty. Allegro also has a readkey() function that does require a key input and will wait if there aren't any. I put the two in a while loop.
While(keypressed()) // cycles until no keys are left and then moves on
readkey(); // "gets" the keys as read thus emptying the buffer.
Sorry for the trouble as I had the solution at my finger tips the whole time. Thanks for all the input
[Edit] I did have one quick question, if you use a non buffered mode, would the program only see one key? The first one hit and ignore any repeats / random keystrokes? That might be a better long term way of game programming.
Ace
Last edited by Ace Blackwell; 02-28-2015 at 12:04 PM.
Reason: One more thing....
genss, from what I can tell read() is designed for file reading. I suppose you could change your input to be from file to keyboard somehow,but not sure if the keyboard buffer includes EOF
file descriptor 0 is stdin (1 stdout, 2 stderr)
i think it's the same on windows too (it's standard POSIX)
Well I have to apologize to the Allegro team as I've misspoken lol. I ended up being a victim of my own programming novice. Allegro clear_keybuf() seems to work fine for my application.
The realization came when trying to make my work around into it's own function. I noticed that every time I copied my new "clear_kbuffer()" function, the editor would not highlight unless i preceded it with the "int". What I didn't realize was I was just redeclaring it everywhere I posted it. So once I did the initial declaration at the top of the file, I just needed to use the name like any other function. At that time I understood that by using Allegro's "void clear_keybuf();" I was just declaring it all over the place and not actually invoking it. So I went back and removed the "void"s and they worked fine.
I thought it was odd that I could use keypressed() with readkey(); and clear the buffer but that Allegro hadn't thought to do that. LOL.
It's like I've often said. If I think I have discovered an easy solution. It's probably wrong"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.