Skip to main content
GameDev.net gamedev.net
🔒 Locked

First key pressed (C++ Console)

Started by fatnickc Jun 1, 2005 at 7:57 AM 12 replies 3k views
Original Post
fatnickc
fatnickc
Having not done much (any) C++ console programming for a while, I thought it would be nice to make a simple game to get used to it again - something more fun than 3D or whatever. And that's where I still am now - well, about 10 lines of code down. I coudldn't think of how to get a keypress from the user without them pressing enter after it, using cout. So I used getch(), and let the user just press enter to make getch() happy and let the program carry on. Now, I face my problem. How can I stop a loop (incrementing x until enter is pressed) to stop when enter is pressed? No idea myself. I've tried whatever I've thought of, but nothing works. Hmmmmm..
---------------------------------Did I hear someone say peanuts?
Andrew Russell
Andrew Russell
Should work. '\n' is enter, IIRC, otherwise try '\r'. The break keyword is what you're after - breaks out of the inner-most loop (for, while, do) or case statement.

while(timer > 0){    if(getch() == '\n')        break;    timer.tick();}
fatnickc
fatnickc
Thanks.
I'm going to just increment a score value rather than using time, as it makes it more comparable.
---------------------------------Did I hear someone say peanuts?
crucifer
crucifer
for a console game,
getch() is your friend ... but not as much as kbhit() ...

kbhit() tells you WHEN a key is pressed ... if a key is pressed, THEN you call getch() ...

this way, your game never waits for no good reason ...
fatnickc
fatnickc
Errmm.. This isn't working at all.
Here's what I have :
while(times>0){if(getch()=='\n')   break;times++;}


It compiles, but times never gets greater than 0.

And crucifier, where is kbhit() written out? Can't find it in any of the headers I could possibly include.

[Edited by - fatnickc on June 1, 2005 12:29:58 PM]
---------------------------------Did I hear someone say peanuts?
chad_420
chad_420
Im fairly certain kbhit() is in conio.h
Derilect101
Derilect101
well isn't time initailized to 0? If so you break the loop instantly and never enter it since its already equal to 0 and not greater than.
fatnickc
fatnickc
Yes, but the loop ISN'T being broken immediately, is it? The break; is inside an if statement, so it will only be broken if I press return/enter. (see EDIT)

And kbhit() is in conio.h, but only like this :
 int	kbhit (void);

It doesn't actually say what kbhit does.

EDIT: Woops, didn't read that right. You're right, I should just change it to
if(times>=0){...}


However, the kbhit problem still stands.

EDIT #2: Nope, It's definately a problem with getch(). Even if I change it to
while(1==1){if(getch()=='\n')   break;cout << "going";}

going is never being printed to the screen. If, however, I put going above the if statement, going is printed once only.


[Edited by - fatnickc on June 2, 2005 4:24:22 AM]
---------------------------------Did I hear someone say peanuts?
eq
eq
getch doesn't return until a key is hit!
while(1==1){if(getch()=='\n')  // getch is called, then the program waits until a key is hit, when a key is hit getch returns the ascii code for the key.   break;cout << "going";}


Solution: Use _kbhit()
What you probably want is something like this:
while(1==1){if (_kbhit() != 0){ // Returns immediatly, if a key has been pressed since the last call to getch, it returns non-zero  cout << "Key pressed! Reading key";  if(getch()=='\n')  // getch is called, since kbhit told us that there is a key pressed it will return immediatly with the ascii code for the pressed key.     break;}  cout << "going";}
fatnickc
fatnickc
The nearest thing to _kbhit() in my conio.h is int _conio_kbhit(); .
However, I can't use that because that is the only thing said about it. There is nothing where the actual function is written out.
In conio_mingw.h, however, _conio.h is, everything aprt from the actual function being written!
---------------------------------Did I hear someone say peanuts?
Genjix
Genjix
Quote:
Original post by fatnickc
The nearest thing to _kbhit() in my conio.h is int _conio_kbhit(); .
However, I can't use that because that is the only thing said about it. There is nothing where the actual function is written out.
In conio_mingw.h, however, _conio.h is, everything aprt from the actual function being written!


out of interest what is the recommended ansi function? how would i do this in linux (unistd seems to have some stuff but i didn't look much).
fatnickc
fatnickc
No idea. Still need help on finding that _kbhit() function - the bit where we are shown what it actually does. Dev-C++ will obviously kick up a fight if it isn't there, and it does.

[Edited by - fatnickc on June 3, 2005 12:11:43 PM]
---------------------------------Did I hear someone say peanuts?
fatnickc
fatnickc
Can't find a function definition on google either.
---------------------------------Did I hear someone say peanuts?
Oluseyi
Oluseyi
Quote:
Original post by Genjix
out of interest what is the recommended ansi function?
There isn't any. C and C++ don't have any inherent notion of a "console," which is why their I/O routines are implemented in terms of streams and file descriptors. Consoles are platform-specific; your best bet is to use a portable console library like some curses variant.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.