Guest Rollstoy Posted February 28, 2001 Share Posted February 28, 2001 Sorry to disturb this forum, but I did not get an answer on the general forum, so I try here! I want to code a program that reacts directly to keystrokes, without puffering! I am reading and reading, but nowhere do I find the right method to switch puffering off! To clarify: I want the program to react to keystrokes immediately and not to puffer input until the "Enter" key is pressed! How do I code this!?! I am so frustrated that I cannot figure out something as simple as this!!!!! Regards and thank you in advance, Thomm 0 Quote Link to comment Share on other sites More sharing options...
BeWary Posted February 28, 2001 Share Posted February 28, 2001 I've done this before, but off-hand I don't remember if it required any special handling. It may require some Assembler commands. I'll check my old programs when I get home tonight and let you know. BeWary ------------------ "Liberty or Death?" Make it "Victory or Pretty Damned Badly Wounded", and I'm yours. - a prospective recruit during the American Revolution. 0 Quote Link to comment Share on other sites More sharing options...
BeWary Posted February 28, 2001 Share Posted February 28, 2001 Ok, I just found the function I think you need to use. It's called getch() or getchar(). It will return the first key in the input stream, so if you put it in a loop, you should be able to achieve your goal of not requiring an <enter> key press. However, I don't know if non-buffered is the correct word to use. I think the buffer is used regardless, as it is a part of the BIOS sub-system. Getch() simply returns a character from that buffer. My copy of Borland is warning not to use this function in Win32 GUI apps, but there's probably something similar available for Win32. Good luck, BeWary ------------------ "Liberty or Death?" Make it "Victory or Pretty Damned Badly Wounded", and I'm yours. - a prospective recruit during the American Revolution. 0 Quote Link to comment Share on other sites More sharing options...
Guest Rollstoy Posted March 1, 2001 Share Posted March 1, 2001 First of all, thank you, BeWary!!!! I am really glad that somebody found time to answer me! So I tried the following under Linux/X-Windows. #include<cstdio> // getchar() #include<iostream> ... cin.sync_with_stdio(); ... while ((i=getchar())!=EOF) cout << i; But STILL I have to press Enter to flush the input buffer! Is this related to X-Windows? Like the way you mentioned for Windows Apps? I searched numerous book in the universtity library but could not find any solution! Still waiting for help ... Thomm [This message has been edited by Rollstoy (edited 03-01-2001).] 0 Quote Link to comment Share on other sites More sharing options...
Guest Rollstoy Posted March 1, 2001 Share Posted March 1, 2001 Some traces lead to ... cin.setbuf(0,0); // or cin.pubsetbuf(0,0); to disable buffering (set it to zero length). But neither will compile ... 0 Quote Link to comment Share on other sites More sharing options...
BeWary Posted March 1, 2001 Share Posted March 1, 2001 I suggest you start out simple and then start calling other methods. For example, are you sure you need to call sync_with_stdio()? Why are you using EOF in your while loop? I've not seen that associated with keyboard input. With the disabling buffering methods, what is the error you're getting when compiling? Perhaps you haven't #included the right files? I've had that happen to me sometimes too. I think some methods are only made to be run on certain platforms. I'll look tonight at my old programs and see how I got it to work. I didn't have a chance last night. BeWary ------------------ "Liberty or Death?" Make it "Victory or Pretty Damned Badly Wounded", and I'm yours. - a prospective recruit during the American Revolution. 0 Quote Link to comment Share on other sites More sharing options...
Kraut Posted March 2, 2001 Share Posted March 2, 2001 Rolls ... try the "endl" stream operator. Itflushes the output buffer (or something like that). If you use getch() you have to put a endl into the stream for your output to show up. while ((i=getchar())!=EOF) cout << i << endl; MK [This message has been edited by Kraut (edited 03-01-2001).] 0 Quote Link to comment Share on other sites More sharing options...
BeWary Posted March 2, 2001 Share Posted March 2, 2001 Ok, I just found a program I wrote that immediately responds to keyboard input. Here's a sample of the code: #include <conio.h> char ch; ch=getch(); while(ch!='q') { cout <<ch; ... or whatever you want to do . . . ch=getch(); } Don't know if that'll work in XWindows or Linux, but it works in DOS/Win. BeWary ------------------ "Liberty or Death?" Make it "Victory or Pretty Damned Badly Wounded", and I'm yours. - a prospective recruit during the American Revolution. 0 Quote Link to comment Share on other sites More sharing options...
KAding Posted March 7, 2001 Share Posted March 7, 2001 I don't know C++ but I do know some C. At least in C wasn't there a 'getche()' function that does exactly that? I can't really remember, but you might as well try . KAding 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.